-
Notifications
You must be signed in to change notification settings - Fork 28
add android skills #57
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
base: next
Are you sure you want to change the base?
Changes from all commits
b3641c6
88480ac
b9eaeab
9aa0ce6
8cba6d2
3cec905
32a937a
1793ff0
84b8db8
a20013e
6c4aa5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| # Firebase AI Logic on Android (Kotlin) | ||
|
|
||
| First, ensure you have initialized the Firebase App (see `firebase-basics` skill). Then, initialize | ||
| the AI Logic service as below | ||
| ### 0. Enable Firebase AI Logic via CLI | ||
|
|
||
| Before adding dependencies in your app, make sure you enable the AI Logic service in your Firebase Project using the Firebase CLI: | ||
|
|
||
| ```bash | ||
| npx -y firebase-tools@latest init | ||
| # When prompted, select 'AI logic' to enable the Gemini API in your project. | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ### 1. Add Dependencies | ||
|
|
||
| In your module-level `build.gradle.kts` (usually `app/build.gradle.kts`), add the dependency for Firebase AI: | ||
|
|
||
| ```kotlin | ||
| dependencies { | ||
| // [AGENT] Fetch the latest available BoM version from Maven Central / Web before adding this | ||
| implementation(platform("com.google.firebase:firebase-bom:<latest_bom_version>")) | ||
|
|
||
| // Add the dependency for the Firebase AI library | ||
| implementation("com.google.firebase:firebase-ai") | ||
| } | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ### 2. Initialize and Generate Content | ||
|
|
||
| In your Activity or Fragment, initialize the `FirebaseAI` service and generate content using a Gemini model: | ||
|
|
||
| ```kotlin | ||
VinayGuthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import com.google.firebase.ai.FirebaseAI | ||
| import com.google.firebase.ai.ktx.ai | ||
| import com.google.firebase.ktx.Firebase | ||
|
|
||
| class MainActivity : AppCompatActivity() { | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| setContentView(R.layout.activity_main) | ||
|
|
||
| // Initialize Firebase AI | ||
| val ai = Firebase.ai | ||
|
|
||
| // Use a model (e.g., gemini-2.5-flash-lite) | ||
| val model = ai.generativeModel("gemini-2.5-flash-lite") | ||
|
|
||
| // Generate content | ||
| lifecycleScope.launch { | ||
| try { | ||
| val response = model.generateContent("Write a story about a magic backpack.") | ||
| Log.d(TAG, "Response: ${response.text}") | ||
| } catch (e: Exception) { | ||
| Log.e(TAG, "Error generating content", e) | ||
| } | ||
| } | ||
| } | ||
| } | ||
VinayGuthal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| #### Jetpack Compose (Modern) | ||
|
|
||
| Initialize inside a `ComponentActivity` and use `setContent`: | ||
|
|
||
| ```kotlin | ||
| import android.os.Bundle | ||
| import androidx.activity.ComponentActivity | ||
| import androidx.activity.compose.setContent | ||
| import androidx.compose.material3.MaterialTheme | ||
| import androidx.compose.material3.Text | ||
| import androidx.lifecycle.lifecycleScope | ||
| import com.google.firebase.Firebase | ||
| import com.google.firebase.ai.ai | ||
| import kotlinx.coroutines.launch | ||
|
|
||
| class MainActivity : ComponentActivity() { | ||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| val ai = Firebase.ai | ||
| val model = ai.generativeModel("gemini-2.5-flash-lite") | ||
|
|
||
| lifecycleScope.launch { | ||
| val response = model.generateContent("Hello Gemini!") | ||
| setContent { | ||
| MaterialTheme { | ||
| Text("AI Response: ${response.text}") | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ### 3. Multimodal Input (Text and Images) | ||
|
|
||
| Pass bitmap data along with text prompts: | ||
|
|
||
| ```kotlin | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does it need any information about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using lifecycleScopy adds enough context |
||
| val image1: Bitmap = ... // Load your bitmap | ||
| val image2: Bitmap = ... | ||
|
|
||
| val response = model.generateContent( | ||
| content("Analyze these images for me") { | ||
| image(image1) | ||
| image(image2) | ||
| text("Compare these two items.") | ||
| } | ||
| ) | ||
| Log.d(TAG, response.text) | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ### 4. Chat Session (Multi-turn) | ||
|
|
||
| Maintain chat history automatically: | ||
|
|
||
| ```kotlin | ||
| val chat = model.startChat( | ||
| history = listOf( | ||
| content("user") { text("Hello, I am a software engineer.") }, | ||
| content("model") { text("Hello! How can I help you today?") } | ||
| ) | ||
| ) | ||
|
|
||
| lifecycleScope.launch { | ||
| val response = chat.sendMessage("What should I learn next?") | ||
| Log.d(TAG, response.text) | ||
| } | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ### 5. Streaming Responses | ||
|
|
||
| For faster display, stream the response: | ||
|
|
||
| ```kotlin | ||
| lifecycleScope.launch { | ||
| model.generateContentStream("Tell me a long story.") | ||
| .collect { chunk -> | ||
| print(chunk.text) // Update UI incrementally | ||
| } | ||
| } | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| # Firebase Authentication on Android (Kotlin) | ||
|
|
||
| This guide walks you through using Firebase Authentication in your Android app using Kotlin DSL (`build.gradle.kts`) and Kotlin code. | ||
|
|
||
| ### Enable Authentication via CLI | ||
|
|
||
| Before adding dependencies in your app, make sure you enable the Auth service in your Firebase Project using the Firebase CLI: | ||
|
|
||
| ```bash | ||
| npx -y firebase-tools@latest init auth | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ### 1. Enable Authentication in the Firebase Console | ||
|
|
||
| Before you begin, make sure you have enabled the sign-in providers you want to use in the Firebase Console: | ||
| 1. Go to **Build > Authentication > Sign-in method**. | ||
| 2. Enable **Email/Password** or **Google** (or any other provider you plan to use). | ||
|
Comment on lines
+15
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmmm... The agent should use the Firebase CLI to do this, right? The dev / agent shouldn't need to touch the Firebase console (except to set up non-CLI-supported auth providers). |
||
|
|
||
| ### 2. Add Dependencies | ||
|
|
||
| In your module-level `build.gradle.kts` (usually `app/build.gradle.kts`), add the dependency for Firebase Authentication: | ||
|
|
||
| ```kotlin | ||
| dependencies { | ||
| // [AGENT] Fetch the latest available BoM version from Maven Central / Web before adding this | ||
| implementation(platform("com.google.firebase:firebase-bom:<latest_bom_version>")) | ||
|
|
||
| // Add the dependency for the Firebase Authentication library | ||
| // When using the BoM, you don't specify versions in Firebase library dependencies | ||
| implementation("com.google.firebase:firebase-auth") | ||
| } | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ### 3. Initialize FirebaseAuth | ||
|
|
||
| In your Activity or Fragment, initialize the `FirebaseAuth` instance: | ||
|
|
||
| ```kotlin | ||
| import com.google.firebase.auth.FirebaseAuth | ||
| import com.google.firebase.auth.ktx.auth | ||
| import com.google.firebase.ktx.Firebase | ||
|
|
||
| class MainActivity : AppCompatActivity() { | ||
|
|
||
| private lateinit var auth: FirebaseAuth | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| val auth = Firebase.auth | ||
|
|
||
| setContent { | ||
| MaterialTheme { | ||
| Text("Auth initialized!") | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| #### Jetpack Compose (Modern) | ||
|
|
||
| Initialize inside a `ComponentActivity` using `setContent`: | ||
|
|
||
| ```kotlin | ||
| import android.os.Bundle | ||
| import androidx.activity.ComponentActivity | ||
| import androidx.activity.compose.setContent | ||
| import androidx.compose.material3.MaterialTheme | ||
| import androidx.compose.material3.Text | ||
| import com.google.firebase.Firebase | ||
| import com.google.firebase.auth.auth | ||
|
|
||
| class MainActivity : ComponentActivity() { | ||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| val auth = Firebase.auth | ||
|
|
||
| setContent { | ||
| MaterialTheme { | ||
| Text("Auth initialized!") | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ### 4. Check Current Auth State | ||
|
|
||
| You should check if a user is already signed in when your activity starts: | ||
|
|
||
| ```kotlin | ||
| public override fun onStart() { | ||
| super.onStart() | ||
| // Check if user is signed in (non-null) and update UI accordingly. | ||
| val currentUser = auth.currentUser | ||
| if (currentUser != null) { | ||
| // User is signed in, navigate to main screen or update UI | ||
| } else { | ||
| // No user is signed in, prompt for login | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ### 5. Sign Up New Users (Email/Password) | ||
|
|
||
| Use `createUserWithEmailAndPassword` to register new users: | ||
|
|
||
| ```kotlin | ||
| fun signUpUser(email: String, password: String) { | ||
| auth.createUserWithEmailAndPassword(email, password) | ||
| .addOnCompleteListener(this) { task -> | ||
| if (task.isSuccessful) { | ||
| // Sign up success, update UI with the signed-in user's information | ||
| val user = auth.currentUser | ||
| // Navigate to main screen | ||
| } else { | ||
| // If sign up fails, display a message to the user. | ||
| Toast.makeText(baseContext, "Authentication failed.", Toast.LENGTH_SHORT).show() | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ### 6. Sign In Existing Users (Email/Password) | ||
|
|
||
| Use `signInWithEmailAndPassword` to log in existing users: | ||
|
|
||
| ```kotlin | ||
| fun signInUser(email: String, password: String) { | ||
| auth.signInWithEmailAndPassword(email, password) | ||
| .addOnCompleteListener(this) { task -> | ||
| if (task.isSuccessful) { | ||
| // Sign in success, update UI with the signed-in user's information | ||
| val user = auth.currentUser | ||
| // Navigate to main screen | ||
| } else { | ||
| // If sign in fails, display a message to the user. | ||
| Toast.makeText(baseContext, "Authentication failed.", Toast.LENGTH_SHORT).show() | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ### 7. Sign Out | ||
|
|
||
| To sign out a user, call `signOut()` on the `FirebaseAuth` instance: | ||
|
|
||
| ```kotlin | ||
| auth.signOut() | ||
| // Navigate to login screen | ||
| ``` | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The agent can find the latest BoM version here: https://firebase.google.com/support/release-notes/android
Same comment for throughout.