Register Android App With Firebase Project AND Add code to connect/save data
SPECIAL NOTE: I ended up using the JDK from Jetbrains (the embeded 1.7 version) to get all of this code to compile properly
STEP 1: Click to "Add Adroid app" from Firebase Project (google firebase console) main interface
There are multiple steps to do this (this assumes you have already created a Firebase Project and have an existing Android Application)
1) you will enter in your applications package name, followed by a name you want to call this application (I usually keep it the same name as the project I have created for the application) 2) FOLLOW the directions they show on how to get a SHA certificate. For us we are simply going to use the generated free debug certificate that you can generate on the command line of your computer.
-
keytool -list -v -alias androiddebugkey -keystore %USERPROFILE%\.android\debug.keystore
-
Note you need to put the path for userprofile for example mine would be on a windows machine
-
keytool -list -v -alias androiddebugkey -keystore C:\lynne\.android\debug.keystore
-
The keytool utility prompts you to enter a password for the keystore.
The default password for the debug keystore is android.
The keytool then prints the fingerprint to the terminal. For example:
3) Download the created google-services.json file (this has the certificate and database information for connection) --- DO NOT upload this to a github repository) and put it in the "app" folder in your AndroidStudio project as directory in the directions.
STEP 2: Add necessary software to app to support Firebase AND update the build gradle files
STEP 2 - OPTION 1: How to use the Android Studio Tools assitant to setup your project to use Firebase Firestore, Auth, Analytics, etc. To to Tools->Firebase.
STEP 2.1 : Select Cloud Firestore
STEP 2.2 Select Get Started with Cloud Firestore(Java)
STEP 2.3: Make sure that you hit the button to " Add the XXX" where XXX is the service from Firestore you want to add to your app. Like shown below "Add the Cloud Firestore SDK to your app".
While you can find directions to do this manually it is more work. ----> It will add the necessary software and update your build gradle files as necessary
>>>> SPECIAL NOTE: If you get an error about the Java and gradle version compatibility issues after adding the above support
Unsupported Java.
Your build is currently configured to use Java 21.0.1 and Gradle 8.0.
Possible solution:
- Open Gradle wrapper settings, change `distributionUrl` property to use compatible Gradle version and reload the project
SOLUTION:
STEP 1: Figure out what the compatibility between your java version and gradle should be
STEP 2: edit the gradle-wrapper.properties file and update the gradle version
SPECIAL NOTE: After I did these steps I got a different error and I ended up using the JDK from Jetbrains (the embeded 1.7 version) with a newer gradle version (8.5) to get all of this code to compile properly
STEP 2 - OPTION 2: Manually add the necessary elements in build file and software
1) To make the google-services.json config values accessible to Firebase SDKs, you need the Google services Gradle plugin.
FOR JAVA: Add the plugin as a dependency to your project-level build.gradle file --add it under dependencies:
classpath 'com.google.gms:google-services:4.0.1'
For example, here is my project-level build.gradle file ---IMPORTANT: you may have different versions or Additional information in your file depending on what your project does.
Follow the directions(yes in Kotlin here but, change to format shown here for Java) to see the LATEST version or for changes to process.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript { dependencies { classpath 'com.google.gms:google-services:4.3.14' } } // Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { id 'com.android.application' version '8.0.0' apply false id 'com.android.library' version '8.0.0' apply false // Add the dependency for the Google services Gradle plugin id 'com.google.gms.google-services' version '4.4.0' apply false }
2) Then, in your module (app-level) build.gradle file, add both the google-services plugin and any Firebase SDKs that you want to use in your app. You will add to
both your plugins and dependencies. I am highlighting what you need to add BUT USE CURRENT VERSIONS --again see
plugins {
id 'com.android.application'
// Add the dependency for the Google services Gradle plugin
id 'com.google.gms.google-services' version '4.4.0' apply false
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.navigation:navigation-fragment:2.5.3'
implementation 'androidx.navigation:navigation-ui:2.5.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
// Import the Firebase BoM
implementation 'com.google.firebase:firebase-bom:32.7.0'
// TODO: Add the dependencies for Firebase products you want to use
// When using the BoM, don't specify versions in Firebase dependencies implementation 'com.google.firebase:firebase-firestore:24.10.0'
implementation 'com.google.firebase:firebase-analytics:21.2.0' implementation 'com.google.firebase:firebase-auth:21.1.0'i
// Add the dependencies for any other desired Firebase products
// https://firebase.google.com/docs/android/setup#available-libraries
}
STEP 3: Add the appropriate code to your Android Application
See this repository example for a SIMPLE application that does not require authentication (running in TEST mode--see how to setup Firebase+Firestore) and from the Main Activity (main interface) it has a simple form to collect a user name and email and creates from it a new document in a Firestore collection called "users"
SEE the API for how to do this in general (make sure you select Java language examples)
ADD to appropriate class (like Main activity or Activity you are doing event handling from)
FirebaseFirestore db;
//in the onCreate method()
db = FirebaseFirestore.getInstance();
somewhere maybe based on a button being hit --
you can add data to the datastore --here the data is hard coded
// Create a new user with a first and last name
Map<String, Object> user = new HashMap<>();
user.put("first", "Ada");
user.put("last", "Lovelace");
user.put("born", 1815);
// Add a new document with a generated ID db.collection("users") .add(user) .addOnSuccessListener(new OnSuccessListener<DocumentReference>() { @Override public void onSuccess(DocumentReference documentReference) { Log.d(TAG, "DocumentSnapshot added with ID: " + documentReference.getId()); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.w(TAG, "Error adding document", e); } });