Coder Social home page Coder Social logo

codingdoug / backends-for-android Goto Github PK

View Code? Open in Web Editor NEW
19.0 2.0 1.0 522 KB

Sample project to accompany my conference talk "Easy, scalable backends for Android"

License: Apache License 2.0

Kotlin 91.67% TypeScript 0.56% Dockerfile 2.78% Shell 4.99%
android kotlin cloud-functions cloud-run android-application

backends-for-android's Introduction

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:

The information and instructions here require that you're familiar with these processes:

  1. Creating a Firebase / Google Cloud project and adding an Android app to it (documentation)
  2. Deploying code to Cloud Functions using the Firebase CLI
  3. 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.

Note that it's possible to deploy and run this code on the Firebase free Spark plan.

To run this sample:

  1. Create a Firebase project and add the Android app in this repo to it.
  2. Deploy the Cloud Functions code using the Firebase CLI.
  3. 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.

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:

  1. Create a Firebase / Google Cloud project, or reuse the one from the prior sample.
  2. Build and deploy the Cloud Run code using gcloud (using the commands in the provided deploy.sh script)
  3. Note the base URL of the deployment in the output. It will have a domain of "run.app".
  4. Edit android/app/src/main/res/values/strings.xml and copy the base URL into the cloud_run_root string resource.
  5. 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:

  1. Create a Firebase / Google Cloud project, or reuse the one from the prior sample.
  2. Build and deploy the Cloud Functions code using gcloud (using the commands in the provided deploy.sh script)
  3. Note the base URL of the deployment in the output. It will have a domain of "cloudfunctions.net".
  4. 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).
  5. 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

backends-for-android's People

Contributors

codingdoug avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

yhdesai

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.