generative-ai-android icon indicating copy to clipboard operation
generative-ai-android copied to clipboard

Show "Unresolved reference: apiKey"

Open amitthecoder opened this issue 2 years ago • 7 comments

When we import generativeai-android-sample app in Android Studio shows an "Unresolved reference: apiKey" error Screenshot 2024-03-26 202605

amitthecoder avatar Mar 26 '24 15:03 amitthecoder

Also when we create a new Gemini project in Android Studio shows the same error. We tried many methods but still showed an error

amitthecoder avatar Mar 26 '24 15:03 amitthecoder

@amitthecoder as mentioned in the Requirements section of the README, you should get an API Key from Google AI Studio and add that key to the local.properties file in this format:

apiKey=YOUR_API_KEY

And then perform a Sync your project with Gradle Files. (easiest way is clicking the button with an elephant icon on the top right corner)

thatfiredev avatar Mar 26 '24 15:03 thatfiredev

We already add this

amitthecoder avatar Mar 26 '24 15:03 amitthecoder

@amitthecoder I just tried this in my machine and it seems like you're right. I'll investigate this further, but in the meantime you can replace BuildConfig.apiKey with your API Key as a string literal, eg:

val generativeModel = GenerativeModel(
  modelName = "gemini-1.0-pro",
  apiKey = "<YOUR_API_KEY>",
  generationConfig = config
)

thatfiredev avatar Mar 26 '24 15:03 thatfiredev

@amitthecoder In the top bar menu, I selected Build > Rebuild project and the error went away. Could you try that ?

thatfiredev avatar Mar 26 '24 15:03 thatfiredev

Yes I tried

amitthecoder avatar Mar 26 '24 15:03 amitthecoder

Well, I had the same apiKey issue and after using the apiKey as raw string a new issue popped up: "unresolved reference" error in PhotoReasoningScreen.kt ->

None of the following candidates is applicable because of receiver type mismatch: public operator fun <T, R> DeepRecursiveFunction<TypeVariable(T), TypeVariable(R)>.invoke(value: TypeVariable(T)): TypeVariable(R) defined in kotlin.

This error message is based on the unmodified github code, macOS 12.7.4, Android Studio Koala | 2023.3.2 Canary 2 Build #AI-233.14475.28.2332.11606850, built on March 21, 2024 Runtime version: 17.0.10+0-17.0.10b1087.21-11572160 x86_64

I spend hours working with Gemini Advanced but ultimately gave up on Gemini because it tried to stir me to fix the Google sample code of which Gemini or I have any insights.

Please provide Android/Gemini sample code that can actually be used as an Android app sample for the public.

kds215 avatar Apr 04 '24 04:04 kds215

Yes I tried

are you still facing this issue @amitthecoder ?

rlazo avatar Jun 04 '24 19:06 rlazo

I'm using the string literal.

kds215 avatar Jun 05 '24 03:06 kds215

I'm using the string literal.

Just to be sure we are on the same page. Using the string literal works, but the plugin doesn't, right?

rlazo avatar Jun 05 '24 12:06 rlazo

Yes, the workaround (string literal) works fine but the BuildConfig.GEMINI_API_KEY method does not. -Klaus.

Here is my complete response:

No change; same issue with or without surrounding “” or ‘’ around the api key in the local.properties file.

   Unresolved reference: GEMINI_API_KEY

Following Android’s Gemini directions:

Q: how to incorporate properly the api key for the gemini-pro-vision model using BuildConfig.GEMINI_API_ KEY?

<Gemini_A start>: To securely incorporate your API key for the Gemini Pro Vision model using BuildConfig.GEMINI_API_KEY, follow these steps:

  1. Store your API key in local.properties: Create a file named local.properties in the root directory of your Android project. Add a line to this file with your API key: GEMINI_API_KEY="YOUR_ACTUAL_API_KEY"
  2. Use the Secrets Gradle Plugin: Add the plugin to your project-level build.gradle (or build.gradle.kts) file: plugins { // ... other plugins id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") version "2.0.1" apply false } Apply the plugin in your module-level build.gradle (or build.gradle.kts) file: plugins { // ... other plugins id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin") }
  3. Initialize the GenerativeModel: val generativeModel = GenerativeModel( modelName = "gemini-pro-vision", apiKey = BuildConfig.GEMINI_API_KEY ) Explanation: The Secrets Gradle Plugin reads the API key from your local.properties file and makes it available as a build configuration variable (BuildConfig.GEMINI_API_KEY). This approach keeps your API key out of your version control system, enhancing security. The GenerativeModel is initialized with the model name and the API key retrieved from BuildConfig. Important Notes: Never hardcode your API key directly in your code. This is a major security risk. Exclude local.properties from version control (e.g., add it to your .gitignore file). Consider using environment variables or a secure keystore for even stronger API key protection in production environments. By following these steps, you can securely use your API key with the Gemini Pro Vision model in your Android application. <Gemini_A end>

Running Android Studio build results (after clean/rebuild project):

e: file:///Users//DevTools/AndroidStudioProjects/navix/app/src/main/java/com//navix/ui/screens/camera/CameraViewModel.kt:27:34 Unresolved reference: GEMINI_API_KEY

using macOS Monterey Version 12.7.5:

Android Studio Jellyfish | 2023.3.1 Patch 1 Build #AI-233.14808.21.2331.11842104, built on May 14, 2024 Runtime version: 17.0.10+0-17.0.10b1087.21-11572160 x86_64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. macOS 12.7.5 GC: G1 Young Generation, G1 Old Generation Memory: 2048M Cores: 8 Metal Rendering is ON Registry: ide.experimental.ui=true

On Jun 5, 2024, at 8:59 AM, Rodrigo Lazo @.***> wrote:

I'm using the string literal.

Just to be sure we are on the same page. Using the string literal works, but the plugin doesn't, right?

— Reply to this email directly, view it on GitHub https://github.com/google-gemini/generative-ai-android/issues/96#issuecomment-2149806682, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHB65GOCG5IEDZGCCYZAXDTZF4DRJAVCNFSM6AAAAABFJDN4FKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNBZHAYDMNRYGI. You are receiving this because you commented.

kds215 avatar Jun 05 '24 19:06 kds215

follow-up:

after changing to and using api key name “apiKey” instead of GEMINI_API_KEY or GOOGLE_API_KEY, same issue:

e: file:///Users/<>/DevTools/AndroidStudioProjects/navix/app/src/main/java/com/<>/navix/ui/screens/camera/CameraViewModel.kt:25:34 file:///Users/%3C%3E/DevTools/AndroidStudioProjects/navix/app/src/main/java/com/%3C%3E/navix/ui/screens/camera/CameraViewModel.kt:25:34

Unresolved reference: apiKey

-Klaus.

On Jun 5, 2024, at 8:59 AM, Rodrigo Lazo @.***> wrote:

I'm using the string literal.

Just to be sure we are on the same page. Using the string literal works, but the plugin doesn't, right?

— Reply to this email directly, view it on GitHub https://github.com/google-gemini/generative-ai-android/issues/96#issuecomment-2149806682, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHB65GOCG5IEDZGCCYZAXDTZF4DRJAVCNFSM6AAAAABFJDN4FKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNBZHAYDMNRYGI. You are receiving this because you commented.

kds215 avatar Jun 05 '24 20:06 kds215

Thanks for the extra info @kds215 . I tried to repro locally using the sample app, plain vanilla now I'm curious of why the generated BuildConfig file does not contain the key. Since you are using Mac, could you run the following commands from the /Users/<>/DevTools/AndroidStudioProjects/navix/ directory?

./gradlew app:assemble
 find app -iname 'BuildConfig.java'

The first command will try to build the app, which requires it to generate the BuildConfig.java files first. The compilation will fail, since there's a Unresolved reference, but that's fine.

the second command will print the path to the generated BuildConfig.java files. Since there's one file per variant, you will probably get several matches. Don't worry, they should be identical.

Once you finish with those steps, could you could copy and paste the contents of any of the BuildConfig.java files in here? I want to make sure the file is created, and that the apiKey, or GEMINI_API_KEY fields is missing.

Thanks!

rlazo avatar Jun 06 '24 04:06 rlazo

Hi Rodrigo,

It worked (1st) with the apiKey=xyz defined in the "secrets.defaults.properties" file AND (2nd) making sure the BuildConfig class is from the local "com.xyz.navix” app and not from "com.google.ai.client.generativeai". The github BuildConfig documentation needs to be updated because it refers to the "local.properties" file as working sample.

Here is the final code: import com.xyz.navix.BuildConfig private val generativeModel by lazy { GenerativeModel( modelName = "gemini-pro-vision", apiKey = BuildConfig.apiKey // apiKey = myApiKey ) } Thanks. -Klaus.

On Jun 6, 2024, at 12:28 AM, Rodrigo Lazo @.***> wrote:

Thanks for the extra info @kds215 https://github.com/kds215 . I tried to repro locally using the sample app, plain vanilla now I'm curious of why the generated BuildConfig file does not contain the key. Since you are using Mac, could you run the following commands from the /Users/<>/DevTools/AndroidStudioProjects/navix/ directory?

./gradlew app:assemble find app -iname 'BuildConfig.java' The first command will try to build the app, which requires it to generate the BuildConfig.java files first. The compilation will fail, since there's a Unresolved reference, but that's fine.

the second command will print the path to the generated BuildConfig.java files. Since there's one file per variant, you will probably get several matches. Don't worry, they should be identical.

Once you finish with those steps, could you could copy and paste the contents of any of the BuildConfig.java files in here? I want to make sure the file is created, and that the apiKey, or GEMINI_API_KEY fields is missing.

Thanks!

— Reply to this email directly, view it on GitHub https://github.com/google-gemini/generative-ai-android/issues/96#issuecomment-2151388157, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHB65GLES46VRBOXGZJ2VFDZF7QPXAVCNFSM6AAAAABFJDN4FKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNJRGM4DQMJVG4. You are receiving this because you were mentioned.

kds215 avatar Jun 06 '24 04:06 kds215

Hey @kds215 great that it works now! A final question

Did you configure the plugin like shown in here ?

https://github.com/google/secrets-gradle-plugin/blob/a988fe0bdf8e205f3e1a71dd3179170920e88135/sample-app/build.gradle.kts#L47-L56

The secrets plugin, by default, looks at local.properties, but can be configured to use a different file, and the example uses the name you mention in your response.

Since the issue has been solved, I'll be closing the issue, but feel free to add your comment afterwards.

rlazo avatar Jun 06 '24 16:06 rlazo

nop, did not touch this secrets config section. This is still from the original “lensfriend” app example:

secrets { propertiesFileName = "secrets.properties" defaultPropertiesFileName = "secrets.defaults.properties" } and the original “secrets.defaults.properties” contained one line:

GEMINI_API_KEY=your_api_key

Only after I looked at this file, I tried my apiKey together with the proper BuildConfig and things kicked loose.

Thanks. -Klaus.

On Jun 6, 2024, at 12:32 PM, Rodrigo Lazo @.***> wrote:

Hey @kds215 https://github.com/kds215 great that it works now! A final question

Did you configure the plugin like shown in here ?

https://github.com/google/secrets-gradle-plugin/blob/a988fe0bdf8e205f3e1a71dd3179170920e88135/sample-app/build.gradle.kts#L47-L56 https://github.com/google/secrets-gradle-plugin/blob/a988fe0bdf8e205f3e1a71dd3179170920e88135/sample-app/build.gradle.kts#L47-L56 The secrets plugin, by default, looks at local.properties, but can be configured https://github.com/google/secrets-gradle-plugin/tree/main?tab=readme-ov-file#configuration-options to use a different file, and the example uses the name you mention in your response.

Since the issue has been solved, I'll be closing the issue, but feel free to add your comment afterwards.

— Reply to this email directly, view it on GitHub https://github.com/google-gemini/generative-ai-android/issues/96#issuecomment-2152948241, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHB65GN6Z4YLBAOEWZDXFMLZGCFLPAVCNFSM6AAAAABFJDN4FKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNJSHE2DQMRUGE. You are receiving this because you were mentioned.

kds215 avatar Jun 06 '24 19:06 kds215

Can anyone help me out. Like i am confused about what to import for the issue to get resolve.

River-unknown avatar Jun 14 '24 04:06 River-unknown

Can anyone help me out. Like i am confused about what to import for the issue to get resolve.

@River-unknown the BuildConfig class is a generated class, so if your app's package is org.example.myapp then you need to import org.example.myapp.BuildConfig. If the class doesn't exist, verify that you have the secrets plugin installed and that your app module is configured with buildConfig=true. See https://stackoverflow.com/questions/20706451/buildconfig-not-getting-created-correctly-gradle-android for how to ensure your app is creating the BuildConfig class.

rlazo avatar Jun 14 '24 15:06 rlazo