אם אתם חדשים ב-Gemini API, Gemini Developer API הוא ספק ה-API המומלץ למפתחי Android. אבל אם יש לכם דרישות ספציפיות לגבי מיקום הנתונים או שאתם כבר משתמשים בסביבת Vertex AI או Google Cloud, אתם יכולים להשתמש ב-Vertex AI Gemini API.
תחילת העבודה
לפני שמתקשרים עם Vertex AI Gemini API ישירות מהאפליקציה, אפשר להתנסות בהנחיות ב-Vertex AI Studio.
הגדרת פרויקט Firebase וקישור האפליקציה ל-Firebase
כשמוכנים לבצע קריאה ל-API מהאפליקציה, פועלים לפי ההוראות בשלב 1 של המדריך לתחילת העבודה עם Firebase AI Logic כדי להגדיר את Firebase ולהפעיל את שירותי ה-API הנדרשים.
הוספת יחסי התלות של Gradle
מוסיפים את יחסי התלות הבאים של Gradle למודול האפליקציה:
Kotlin
dependencies {
// ... other androidx dependencies
// Import the BoM for the Firebase platform
implementation(platform("com.google.firebase:firebase-bom:34.15.0"))
// Add the dependencies for the Firebase AI Logic and App Check libraries
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation("com.google.firebase:firebase-ai")
implementation("com.google.firebase:firebase-appcheck-debug")
}
Java
dependencies {
// Import the BoM for the Firebase platform
implementation(platform("com.google.firebase:34.15.0"))
// Add the dependencies for the Firebase AI Logic and App Check libraries
// When using the BoM, you don't specify versions in Firebase library dependencies
implementation("com.google.firebase:firebase-ai")
implementation("com.google.firebase:firebase-appcheck-debug")
// Required for one-shot operations (to use `ListenableFuture` from Guava Android)
implementation("com.google.guava:guava:31.0.1-android")
// Required for streaming operations (to use `Publisher` from Reactive Streams)
implementation("org.reactivestreams:reactive-streams:1.0.4")
}
הגדרת ספק ניפוי הבאגים של App Check לפיתוח מקומי
החל מתחילת יולי 2026, כחלק מתהליך ההגדרה המודרך של AI Logic במסוף Firebase, ייאכף באופן אוטומטי השימוש ב-Firebase App Check כדי להגן על Gemini API. בפיתוח מקומי, צריך להגדיר את ספק הניפוי באגים של App Check כדי לעקוף את האימות, ועדיין לשמור על האכיפה של App Check.
בגרסת הניפוי באגים, מגדירים את App Check לשימוש ב-factory של ספק ניפוי הבאגים:
Kotlin
Firebase.initialize(context = this) Firebase.appCheck.installAppCheckProviderFactory( DebugAppCheckProviderFactory.getInstance(), )Java
FirebaseApp.initializeApp(/*context=*/ this); FirebaseAppCheck firebaseAppCheck = FirebaseAppCheck.getInstance(); firebaseAppCheck.installAppCheckProviderFactory( DebugAppCheckProviderFactory.getInstance());קבלת טוקן לניפוי באגים:
מריצים את האפליקציה באמולטור או במכשיר הבדיקה.
מחפשים את אסימון הניפוי באגים של App Check ביומנים. לדוגמה:
D DebugAppCheckProvider: Enter this debug secret into the allow list in the Firebase Console for your project: 123a4567-b89c-12d3-e456-789012345678מעתיקים את הטוקן (לדוגמה,
123a4567-b89c-12d3-e456-789012345678).
רושמים את טוקן הניפוי באגים ב-App Check:
במסוף Firebase, עוברים אל Security (אבטחה) > App Check (בדיקת אפליקציות) > הכרטיסייה Apps (אפליקציות).
מוצאים את האפליקציה, לוחצים על תפריט האפשרויות הנוספות () ובוחרים באפשרות ניהול אסימוני ניפוי באגים.
פועלים לפי ההוראות במסך כדי לרשום את אסימון הניפוי באגים.
פרטים על ספק ניפוי הבאגים (כולל איך מקבלים אסימון ניפוי באגים חדש) מופיעים במסמכים הרשמיים של App Check.
הפעלת המודל הגנרטיבי
כדי להתחיל, יוצרים מופע של GenerativeModel ומציינים את שם המודל:
Kotlin
val model = Firebase.ai(backend = GenerativeBackend.vertexAI()) .generativeModel("gemini-2.5-flash")
Java
GenerativeModel firebaseAI = FirebaseAI.getInstance(GenerativeBackend.vertexAI()) .generativeModel("gemini-2.5-flash"); GenerativeModelFutures model = GenerativeModelFutures.from(firebaseAI);
במסמכי התיעוד של Firebase אפשר לקרוא מידע נוסף על המודלים הזמינים של Gemini. אפשר גם לקרוא על הגדרת פרמטרים של מודלים.
יצירת טקסט
כדי ליצור תשובה טקסטואלית, מתקשרים אל generateContent() עם ההנחיה.
Kotlin
suspend fun generateText(model: GenerativeModel) { // Note: generateContent() is a suspend function, which integrates well // with existing Kotlin code. val response = model.generateContent("Write a story about a magic backpack.") // ... }
Java
Content prompt = new Content.Builder() .addText("Write a story about a magic backpack.") .build(); ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt); Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() { @Override public void onSuccess(GenerateContentResponse result) { String resultText = result.getText(); // ... } @Override public void onFailure(Throwable t) { t.printStackTrace(); } }, executor);
בדומה ל-Gemini Developer API, אתם יכולים גם להעביר תמונות, אודיו, סרטונים וקבצים עם הנחיית הטקסט. לפרטים נוספים, אפשר לעיין במאמר בנושא אינטראקציה עם Gemini Developer API מהאפליקציה.
מידע נוסף על Firebase AI Logic SDK זמין במסמכי התיעוד של Firebase.