google-gemini/generative-ai-android

Show "Unresolved reference: apiKey"

amitthecoder opened this issue · 18 comments

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

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 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)

We already add this

@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
)

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

Yes I tried

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.

Yes I tried

are you still facing this issue @amitthecoder ?

I'm using the string literal.

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?

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!

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.

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

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.