For this homework, you should submit 2 Android Studio Projects
- The Room local storage project, as done during Lab 5, finishing at least steps 1-3. (3 pts)
- The Firestore project, as described below (2 pts)
Trying out Firebase Firestore cloud storage
We will create a new Firebase project and use Firestore database for it. This requires a Google account. * https://firebase.google.com/docs/firestore/quickstart is a good support material for the following steps
Go to https://console.firebase.google.com/ and create a new project
- You can turn off Analytics when asked
- When the project has been created, select the Android icon under "Get started by adding Firebase to your app ". At this stage you should create a new Android Studio project
- provide the package name of the new App (cs.ut.ee.<<myAppName>> , for example). The one provided to Firebase MUST match the actual one used in the app!
- Follow the web instructions regarding google-services.json
- Follow the web instructions regarding SDK dependancies. Note that you have to modify two build.gradle files!
- For the app-level build.gradle dependencies{ .. },
implementation 'com.google.firebase:firebase-firestore:21.1.1'
is sufficient.
- For the app-level build.gradle dependencies{ .. },
Once you finish the above steps on firebase website, from the web console, add Firestore to your app (E.g. left-hand side "Database" -> Cloud Firestore - Create Database )
- When asked about locked or test mode, you can choose test mode, which gives read & write access to everybody who can reach your DB! Check here for more details on security in firestore.
- For region, you can choose something in Europe.
Try to create a collection and add some documents from the web project manager.
Let's add some data from our App MainActivity Now, create a collection named "cities" and add the following Documents to it, using appropriate data types:
In Android Studio (MainActivity), try to access the database:
// Get instance of database val db = FirebaseFirestore.getInstance()
Then, add data of 3 documents to the "cities" collection with this code:
val cities = db.collection("cities") val dataTallinn = hashMapOf( "name" to "Tallinn", "population" to 434562, "area_km2" to 159 ) cities.document("TLL").set(dataTallinn) val dataTartu = hashMapOf( "name" to "Tartu", "population" to 93865, "area_km2" to 39 ) cities.document("TRT").set(dataTartu) val dataNarva = hashMapOf( "name" to "Narva", "population" to 55249, "area_km2" to 68 ) cities.document("NRV").set(dataNarva)
(You may comment out the code once it has been created on the first run, firestore doesn't raise exceptions by default when inserting duplicate data)
Now, study the Firestore Docs and try to put together code which queries the cities collection, sorting results in descending order by city area, and logs the whole data of each document.
- How to read data and log it:
- How to sort results:
As a result, when running your app, it should log the cities data to logcat in the correct order!
Tips / Troubleshooting
If you get an error about no of method references exceeding 65,536 methods after adding the Google services libraries, you probably have to enable multidex:
https://developer.android.com/studio/build/multidex#mdex-gradle