-
Notifications
You must be signed in to change notification settings - Fork 128
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
Comments
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 val generativeModel = GenerativeModel(
modelName = "gemini-1.0-pro",
apiKey = "<YOUR_API_KEY>",
generationConfig = config
) |
@amitthecoder In the top bar menu, I selected |
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: This error message is based on the unmodified github code, macOS 12.7.4, Android Studio Koala | 2023.3.2 Canary 2 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. |
are you still facing this issue @amitthecoder ? |
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? |
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/<unk>/DevTools/AndroidStudioProjects/navix/app/src/main/java/com/<unk>/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 <#96 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AHB65GOCG5IEDZGCCYZAXDTZF4DRJAVCNFSM6AAAAABFJDN4FKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNBZHAYDMNRYGI>.
You are receiving this because you commented.
|
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 <#96 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AHB65GOCG5IEDZGCCYZAXDTZF4DRJAVCNFSM6AAAAABFJDN4FKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNBZHAYDMNRYGI>.
You are receiving this because you commented.
|
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
The first command will try to build the app, which requires it to generate the the second command will print the path to the generated Once you finish with those steps, could you could copy and paste the contents of any of the Thanks! |
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 <#96 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AHB65GLES46VRBOXGZJ2VFDZF7QPXAVCNFSM6AAAAABFJDN4FKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNJRGM4DQMJVG4>.
You are receiving this because you were mentioned.
|
Hey @kds215 great that it works now! A final question Did you configure the plugin like shown in here ? The secrets plugin, by default, looks at Since the issue has been solved, I'll be closing the issue, but feel free to add your comment afterwards. |
When we import generativeai-android-sample app in Android Studio shows an "Unresolved reference: apiKey" error
The text was updated successfully, but these errors were encountered: