Skip to content
Branch: master
Find file History
cartland Update Classy Taxi to Gradle 5.1.1 and Gradle Plugin 3.4.1
Bug: 134483426
Change-Id: I847d62147aa92ff5f8e2c86d35320fc105848720
Latest commit 3fbe5fe Jun 4, 2019

README.md

Classy Taxi: Google Play Billing Subscriptions Sample

Classy Taxi is an end-to-end project that highlights subscription features on Google Play Billing, such as Real-time Developer Notifications, account hold, grace period, and more. The code has 3 major components: an Android app to buy subscriptions, a Node.js server to manage subscriptions, and a read-only web client to access the subscriptions on multiple platforms.

Classy Taxi animation GIF showing subscription features of Google Play Billing

Copyright

Copyright 2018 Google LLC. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Clone the sample project

git clone https://github.com/googlesamples/android-play-billing.git

What do I need?

  • A Google Play Developer Account to publish the app and manage subscriptions

  • A Firebase project to deploy the server implementation on Cloud Functions for Firebase, enable sign-in, and support Firebase Cloud Messaging

    • Note: The Firebase project has a corresponding Google Cloud Console project that will be configured for Real-time Developer Notifications
  • A Google Cloud Console project to use the Google Play APIs

    • Note: This is usually a different Google Cloud Console project than the one used with Firebase

Summary: What order should I do things?

  1. Choose the Android package name

  2. Start the Firebase Project Setup

    • Firebase setup requires that you know the Android package name

    • You can add the SHA1 signature information to the Firebase console later

  3. Complete the Android App Setup

    • The Android app will not build without google-services.json from Firebase
  4. Upload a release-build APK signed with release keys to Google Play

  5. Complete the Firebase Project Setup

    • If you skipped the Firebase configuration step for the SHA1 fingerprint, you can complete that step now by rebuilding with the updated google-services.json
  6. Start the Play Developer Console Setup

    • Google Play Billing APIs require the SKUs to be configured in the console
  7. Optional: Test with a debug APK and debug signatures on your device

  8. Complete the Play Developer Console Setup

    • Link your Google Cloud Console project to the Google Play Developer Account

      • Note: A Google Play Console account can only be linked to a single Google Cloud Console project, so we recommend creating a dedicated Google Cloud Console project that is shared by all apps in your Google Play Console

      • Configure a service account in Google Cloud Console

  9. Complete Real-time Developer Notifications Setup

  10. Deploy Backend Server

Firebase Project Setup

This sample application uses Firebase (Auth, Notifications, Firestore).

  1. Go to Firebase Console and create a Firebase project

  2. In the project, go to Develop > Database > Cloud Firestore > Get started > Start in locked mode

    • Wait for Firestore to be enabled for the project
  3. In the project, go to Develop > Authentication > Sign-in Method. Enable Email/Password and Google as sign-in providers

  4. Add your Android app to the project

  5. Add your app's SHA1 fingerprint for Google Sign-In

  6. Download the generated google-services.json and move it to your app module directory

    • {project_folder}/android/ClassyTaxi/app/google-services.json

Android App Setup

In order to use the Google Play Billing APIs, you must pick a unique package name and upload a signed release APK to Google Play. The Android app will only build after you include a compatible google-services.json file from the Firebase configuration.

Build your APK from Android Studio

  1. Open Android Studio, select File > Open and pick the root build.gradle file

    • {project_folder}/android/ClassyTaxi/build.gradle
  2. Change the androidApplicationId to the correct Android package name

    • {project_folder}/android/ClassyTaxi/gradle.properties
  3. Confirm that you have the latest google-services.json from the Firebase configuration in your app directory

    • {project_folder}/android/ClassyTaxi/app/google-services.json
  4. Optional: If you want to test the UI without the Firebase server, change this variable in Constants.kt

    • USE_FAKE_SERVER = true
  5. Rebuild the Gradle project verify that there are no errors

  6. Sign a release build with your release key

  7. If you skipped the Firebase configuration step for the SHA1 fingerprint, you can complete that step now, and rebuild with the updated google-services.json

Play Developer Console Setup

  1. Create an application in the Google Play Developer Console

  2. Upload and publish the release APK to the Internal Test, Alpha, Beta, or Production channel

  3. Create subscription products for a Basic Subscription and Premium Subscription

  4. Link the Google Cloud Console project to the Google Play Developer Account

  5. Optional: Create a license test account to test subscriptions quickly without spending money

Real-time Developer Notifications Setup

  1. Use the Google Cloud Console project that corresponds to the Firebase project to enable Cloud PubSub

  2. Configure the Google Play Console to allow your backend to receive notifications from Play Store for subscription purchase changes

Deploy Backend Server

  1. Make sure you have installed Node.js, npm, and Firebase CLI

  2. From the command line, navigate to {project_folder}/firebase/server

  3. Run npm install to install dependencies

  4. Configure Cloud Functions for Firebase with your Android app and subscription products

     firebase use --add {your_firebase_project_id}
    
     firebase functions:config:set app.package_name="your_android_application_id"
    
     firebase functions:config:set app.basic_plan_sku="your_basic_subscription_product_sku_id"
    
     firebase functions:config:set app.premium_plan_sku="your_premium_subscription_product_sku_id"
    
  5. Run firebase deploy to deploy your backend to Cloud Functions for Firebase

Troubleshoot common issues

  • Q: There is a warning "Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions" in my Cloud Functions log.

    • A: You can safely ignore the warning and stay use the Firebase Spark Plan because the sample only accesses the Google network.
  • Q: Android build: Execution failed for task :app:processDebugGoogleServices. File google-services.json is missing. The Google Services Plugin cannot function without it.

    • A: Download google-services.json from the Firebase console.
  • Q: Android build: No matching client found for package name 'com.example.subscriptions.classytaxi'.

    • A: Change the androidApplicationId in gradle.properties and download the updated google-services.json from your Firebase project configuration.
  • Q: Real-time Developer Notifications are not working.

    • A: Make sure the Google Play service account has access as the Publisher so Google Play can publish updates to you.

Optional: Build the Android app from the command line

The sample allows you to specify signing keys and a package name with the Gradle command line. This allows you to build and sign a release APK without making any modifications to the git repository. The sample contains a file example-keystore.properties that you can copy (or modify) to meet your needs.

  1. Create a file release-keystore.properties with your release key information (see example-keystore.properties for the expected format)

  2. Download the generated google-services.json and move it to your app module directory

    • {project_folder}/android/ClassyTaxi/app/google-services.json
  3. Run the following Gradle command in the directory {project_folder}/android/ClassyTaxi

    • Replace the Android application ID with your unique package name

      ./gradlew -PandroidApplicationId=com.example.subscriptions.different.package PkeystorePropertiesFilename=release-keystore.properties assembleRelease
      
  4. Find the location of the output APK

    {project_folder}/android/ClassyTaxi/app/build/outputs/apk/release/app-release.apk
    
  5. Deploy this APK to your Internal Test track for fast development

Android files required for a release APK with release keys

  • Keystore: release.jks
  • Passwords: release-keystore.properties
  • Firebase configuration: google-services.json

Full list of commands to build a release APK

cd {project_folder}/android/ClassyTaxi

### Put release keystore information in the root project directory
cp /path/to/your/secrets/release.jks .
cp /path/to/your/secrets/release-keystore.properties .

### Put Firebase google-services.json in the `app/` directory
cp /path/to/your/secrets/google-services.json ./app/google-services.json

### Check to make sure the files are in the correct location
ls -1 *.jks *keystore.properties
# debug.jks
# keystore.properties
# release-keystore.properties
# release.jks

### Make sure you are not tracking any secret files in git
git status
# On branch master
# nothing to commit, **working tree clean**

### Release build with release keys
./gradlew -PkeystorePropertiesFilename=release-keystore.properties -PandroidApplicationId=com.example.subscriptions.different.package assembleRelease

### Debug build with release keys
./gradlew -PkeystorePropertiesFilename=release-keystore.properties -PandroidApplicationId=com.example.subscriptions.different.package assembleDebug

### Specify version name and version code
./gradlew -PandroidMobileVersionName=1.0.1 -PandroidMobileVersionCode=2 -PkeystorePropertiesFilename=release-keystore.properties -PandroidApplicationId=com.example.subscriptions.different.package assembleRelease

### Note: Release builds must be installed by Google Play.
### This means you must deploy release builds to a
### track on Google Play in order to use the Google Play Billing APIs.
### Use debug builds to sideload the app for testing.
### Android Studio creates debug builds by default.
### Gradle will create debug builds with the task `assembleDebug`.
You can’t perform that action at this time.