Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show "Unresolved reference: apiKey" #96

Closed
amitthecoder opened this issue Mar 26, 2024 · 15 comments
Closed

Show "Unresolved reference: apiKey" #96

amitthecoder opened this issue Mar 26, 2024 · 15 comments
Assignees
Labels
component:android sdk Issue/PR related to Android SDK status:triaged Issue/PR triaged to the corresponding sub-team type:bug Something isn't working

Comments

@amitthecoder
Copy link

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

@amitthecoder
Copy link
Author

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

@thatfiredev
Copy link
Member

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

@amitthecoder
Copy link
Author

We already add this

@thatfiredev
Copy link
Member

@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 thatfiredev self-assigned this Mar 26, 2024
@thatfiredev
Copy link
Member

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

@amitthecoder
Copy link
Author

Yes I tried

@singhniraj08 singhniraj08 added type:bug Something isn't working component:android sdk Issue/PR related to Android SDK status:triaged Issue/PR triaged to the corresponding sub-team labels Mar 27, 2024
@kds215
Copy link

kds215 commented Apr 4, 2024

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.

@rlazo
Copy link
Collaborator

rlazo commented Jun 4, 2024

Yes I tried

are you still facing this issue @amitthecoder ?

@kds215
Copy link

kds215 commented Jun 5, 2024

I'm using the string literal.

@rlazo
Copy link
Collaborator

rlazo commented Jun 5, 2024

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?

@kds215
Copy link

kds215 commented Jun 5, 2024 via email

@kds215
Copy link

kds215 commented Jun 5, 2024 via email

@rlazo
Copy link
Collaborator

rlazo commented Jun 6, 2024

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!

@kds215
Copy link

kds215 commented Jun 6, 2024 via email

@rlazo
Copy link
Collaborator

rlazo commented Jun 6, 2024

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 rlazo closed this as completed Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:android sdk Issue/PR related to Android SDK status:triaged Issue/PR triaged to the corresponding sub-team type:bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants