50

Everything was working great... until I came back to work from a 3 month break and updated my Firebase from 9.8 to 10.0.1

Now all of my calls to TaskSnapshot are giving me an error.

Here is the example code that worked fine before:

OnSuccessListener<UploadTask.TaskSnapshot> successListener = new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        attachments.add(fileName + "*-*" + taskSnapshot.getDownloadUrl().toString());

        numberOfCallbacks++;
        if (numberOfFiles == numberOfCallbacks) {
            currentUpload = false;
            onClickSendAlert(sendingView);
        }
    }
};

The error that I now get is regarding taskSnapshot.getDownloadUrl().

Android Studio underlines that line in red and says:

This method should only be accessed from tests or within private scope

Can someone explain why this is happening? I have been researching all day for two days straight now and can't for the life of me figure this out (embarrassing).

For what it's worth, this code is used to upload a file to Firebase Storage, then when it is complete (OnSuccess), it gets the download URL and stores it in the Firebase Database. This worked great before I updated to 10.0.1. I get the same error on my download tasks in another module.

Here is a screenshot to give you a better visual of my situation:

enter image description here

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Ryan
  • 1,988
  • 4
  • 21
  • 34
  • 1
    I should add that as soon as I change Firebase and Google Play Service back to 9.8.0 all of the errors disappear. – Ryan Dec 12 '16 at 17:09
  • Using Android Studio, when I click on an occurrence of `TaskSnapshot.getDownloadUrl()` and hit Ctrl-B to see the decompiled class file, the declaration I see is `public android.net.Uri getDownloadUrl()`. Do you see that also? – Bob Snyder Dec 13 '16 at 21:16
  • I'm having this same issue. Going to downgrade to 9.8.0 and see if I can get things working. – dazza5000 Dec 20 '16 at 20:58
  • I ended up downgrading back to 9.8.0 as well and everything went back to normal. I hate that I don't have time to figure this out. Unless someone figures it out I will be stuck on 9.8.0. This project is almost complete anyways though so it shouldn't hurt much. – Ryan Dec 20 '16 at 22:30
  • 1
    I'm also getting this but I'm able to build, so I'm not sure why it's flagging as an error rather than a warning. Would like to know what correct method is. – Travis Christian Jan 10 '17 at 21:42
  • I am getting the same behavior and was curious if anyone has reported it to Firebase. It doesn't seem to affect compiling. – GregM Jan 20 '17 at 14:41
  • 1
    The firebase guidelines even use this method for their examples: https://firebase.google.com/docs/storage/android/upload-files . Must be an oversight. Not the best but: disable inspection – JacksOnF1re Jan 20 '17 at 16:53
  • 1
    For me this error appears with android studio 2.3, before the upgrade I was using 10.2.0 without problems. – Bronx Mar 03 '17 at 09:28

4 Answers4

64

The problem seems to be caused by an overzealous Lint check. Try something like this:

@SuppressWarnings("VisibleForTests") Uri downloadUrl = taskSnapshot.getDownloadUrl();

This trick worked for me. If the problem's related to this bug report, then it should be fixed in 2.4.

Rapunzel Van Winkle
  • 5,380
  • 6
  • 31
  • 48
  • For some reason this annotation didn't make the warning go away for me. – IgorGanapolsky Mar 09 '17 at 14:39
  • Sorry to hear that! I'm using Android Studio 2.3 with Firebase 10.2.0 and I just double-checked that I can make the warning reappear when I remove the annotation and disappear when I put the annotation back. – Rapunzel Van Winkle Mar 10 '17 at 05:44
  • If you click on the red-underlined text (getDownloadUrl) and press alt-Enter, does the context menu say "Inspection 'Constant and Resource Type Mismatches' options"? Or does it mention some other inspection? If you click the right arrow on the menu option, it will give you more options (like "suppress for statement") to make the warning go away (which may or not be advisable, depending on what the warning's about). – Rapunzel Van Winkle Mar 10 '17 at 05:55
  • I do not get that context menu from **alt-Enter**. However, I do make the error go away by putting the annotation in the right place. I wasn't putting it on the right block of code. – IgorGanapolsky Mar 10 '17 at 14:09
  • Thank you! I ended up taking such a long break from development that when I got back and upgraded to 10.2.0 the issue was gone :) I guess time can heal some things lol. – Ryan Mar 16 '17 at 20:53
  • If @SuppressWarnings("VisibleForTests") Uri downloadUrl = taskSnapshot.getDownloadUrl(); does not work then, change your firebase version back to 9.8.0 – Aayush Upadhyay Jul 19 '17 at 18:00
6

I was stuck in the same issue and suppressWarnings didn't work for me. To get the complete download Uri i used the following code:

ref.putFile(imagePath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                     Log.d("URL", uri.toString());
                    // This is the complete uri, you can store it to realtime database
                }
            });
        }
    });

Hope this helps someone.

Shivam Pokhriyal
  • 1,044
  • 11
  • 26
0

I had the same problem and it was gone when I have updated my Firebase version. I was using 10.0.1 and now I am using 11.0.0

0

Use

@VisibleForTesting
internal val something

or

@VisibleForTesting
internal fun foo()
Adam Kis
  • 1,404
  • 14
  • 17