Easy, scalable backends for Android developers
This is the sample project to accompany my conference talk "Easy, scalable backends for Android developers". There are two primary use cases illustrated here, with both frontend and backend code provided. The frontend is an Android app that uses Kotlin, coroutines, Jetpack architecture components, and MVVM. The backend is hosted on Firebase / Google Cloud.
The top-level components in this repo are:
- An Android app under android
- A Cloud Functions backend (nodejs/TypeScript) under cloud-functions-ts
- A Cloud Functions backend (JVM/Kotlin) under cloud-functions-kt
- A Cloud Run backend under cloud-run
The information and instructions here require that you're familiar with these processes:
- Creating a Firebase / Google Cloud project and adding an Android app to it (documentation)
- Deploying code to Cloud Functions using the Firebase CLI
- Deploying code to Cloud Run using the Google Cloud SDK (gcloud)
Please note that a Firebase project is a Google Cloud project. If you create a Firebase project, you are also implicitly creating a Google Cloud project, and you have all the features and benefits of Google Cloud available in your Firebase project. To better understand the relationship between Firebase and Google Cloud, read this blog.
Cloud Functions: Firebase callable function (TypeScript, nodejs)
This repo contains sample code that shows how an Android app can invoke a Firebase callable function using the provided SDKs.
- Cloud Functions / TypeScript code in index.ts under cloud-functions-ts.
- Android / Kotlin code contained in a package for a fragment.
Note that it's possible to deploy and run this code on the Firebase free Spark plan.
To run this sample:
- Create a Firebase project and add the Android app in this repo to it.
- Deploy the Cloud Functions code using the Firebase CLI.
- Launch the Android app and press the "invoke callable sum" button.
Deployment with the Firebase CLI goes like this:
cd cloud-functions-ts
firebase deploy
The output should show that the function called "sum" was successfully deployed.
In the Android app, when you ress the "invoke callable sum" button, it will use the Firebase Functions SDK to invoke the function and get a sum of two integers, as defined in the client source code.
Cloud Run: HTTP endpoints (Kotlin, coroutines, ktor, kotlinx.serialization)
This repo contains sample code that shows how an Android app can invoke a backend deployed to Cloud Run using Kotlin and ktor.
- Cloud Run / Kotlin code in package under cloud-run. It uses a Kotlin web server called ktor.
- Android / Kotlin code contained in a package for a fragment. It uses the ktor HTTP client library.
Your Firebase project must be on a payment plan in order to deploy and run this sample. For basic experimentation, you are highly unlikely to incur any actual costs, as there is a perpetual free allowance for all of the Firebase and Google Cloud products in use here. If you a concerned about future charges, then you can simply delete the project or disable billing after you're done experimenting with it.
Summary of steps to run the sample:
- Create a Firebase / Google Cloud project, or reuse the one from the prior sample.
- Build and deploy the Cloud Run code using gcloud (using the commands in the provided deploy.sh script)
- Note the base URL of the deployment in the output. It will have a domain of "run.app".
- Edit android/app/src/main/res/values/strings.xml and copy the base URL
into the
cloud_run_root
string resource. - Run the Android app and press the "invoke cloud run endpoint" button.
Note that it can be time-consuming to get started with Google Cloud if you don't have an experience with it, and the instructions here are not comprehensive. You will likely need to spend some time sorting through the documentation in order to get acquainted with how Google Cloud and its CLI and console work. You should also study the Cloud Run documentation to better understand how to navigate the product and the Google Cloud console.
You should have the Google Cloud SDK (particularly, the gcloud CLI) installed and configured to deploy the backend code to Cloud Run. You will also need a unique name for your app to put in place of the $APP_NAME variable below in order to build and deploy a container to Cloud Run:
cd cloud-run
./deploy.sh $PROJECT_NAME $APP_NAME
$PROJECT_NAME
is the name of your project, and $APP_NAME
is the name of your
Cloud Run app (also called a "service"). Examine the deploy.sh shell
script to see the gcloud commands that it runs to build and deploy a docker
image to Cloud Run. If the Cloud Run app you named here doesn't exist, it will
be created. If you are prompted for anything during deployment, take the
defaults.
If you are running an OS that can't run unix shell scripts, you will have to reverse-engineer the gcloud commands in build.sh for build and deployment.
When the deployment finishes, you will recevie a base URL for your HTTP endpoints. For example, the last line of output should look like this:
Service [helloworld] revision [helloworld-00008-sih] has been deployed and is serving 100 percent of traffic.
Service URL: https://helloworld-jtdztnhypa-uc.a.run.app
Cloud Functions: HTTP function (Kotlin)
This repo contains sample code that shows how to write an HTTP Cloud Function using Kotlin and deploy it with gcloud.
- Cloud Functions / Kotlin code in package under cloud-functions-kt.
- Android / Kotlin code is the same as in the Cloud Run example above. It just needs to be configured to hit the Cloud Functions endpoint rather than Cloud Run.
Your Firebase project must be on a payment plan in order to deploy and run this sample. For basic experimentation, you are highly unlikely to incur any actual costs, as there is a perpetual free allowance for all of the Firebase and Google Cloud products in use here. If you a concerned about future charges, then you can simply delete the project or disable billing after you're done experimenting with it.
Summary of steps to run the sample:
- Create a Firebase / Google Cloud project, or reuse the one from the prior sample.
- Build and deploy the Cloud Functions code using gcloud (using the commands in the provided deploy.sh script)
- Note the base URL of the deployment in the output. It will have a domain of "cloudfunctions.net".
- Edit android/app/src/main/res/values/strings.xml and copy the base URL
into the
cloud_run_root
string resource (without the /nextVersion path). - Run the Android app and press the "invoke cloud run endpoint" button.
You should have the Google Cloud SDK (particularly, the gcloud CLI) installed and configured to deploy the backend code to Cloud Run.
cd cloud-functions-kt
./deploy.sh $PROJECT_NAME $FUNCTION_NAME $ENTRY_POINT
$PROJECT_NAME
is the name of your project, $FUNCTION_NAME
is the name of the
function to deploy (e.g. nextVerion), and $ENTRY_POINT
is the full name of the
class the handles execution (com.hyperaware.bfa.cloudfunctions.NextVersion).
Examine the deploy.sh shell script to see the gcloud commands that it runs
to deploy the function.
If you are running an OS that can't run unix shell scripts, you will have to reverse-engineer the gcloud commands in build.sh for build and deployment.
When the deployment finishes, you will recevie a base URL for your HTTP endpoints. For example, there should be output that looks like this:
httpsTrigger:
url: https://us-central1-$PROJECT_NAME.cloudfunctions.net/$FUNCTION_NAME