Search code examples
androidandroid-studiogoogle-cloud-vision

Getting "The Application Default Credentials are not available" error in android studio even though env variable is set


I'm currently having an issue in android studio. I have set my Google app credentials environment variable AND restarted android studio over and over and its giving me the same error.

I know I am using the correct service account JSON file for my app and I know the GOOGLE_APPLICATION_CREDENTIALS environment variable is set (The variable shows up in my android studio terminal AND my computer's terminal), I just have no clue why this isn't working.

Also even though the variable is set, when I do "System.getenv("GOOGLE_APPLICATON_CREDENTIALS")" it returns null. I don't knwo why this is either.

Here is the line of code throwing this error:

GoogleCredential credential = GoogleCredential.getApplicationDefault().fromStream(credentialsJSON).createScoped(VisionScopes.all());

My credentialsJSON comes from this line of code:

InputStream credentialsJSON = LabelApp.class.getClassLoader().getResourceAsStream("C:\\[path-to-service-acct-json]");

Solution

  • You can't just set GOOGLE_APPLICATON_CREDENTIALS on your PC and expect them to be known on your smartphone... for Android (which does not equal Java), better use the ML Kit for Firebase.

    See the ML Kit for Firebase Quickstart Android application.