Creating, Testing and Uploading an Android App Bundle

From Techotopia
Revision as of 20:48, 11 May 2018 by Neil (Talk | contribs)

Jump to: navigation, search

Once the development work on an Android application is complete and it has been tested on a wide range of Android devices, the next step is to prepare the application for submission to Google Play. Before submission can take place, however, the application must be packaged for release and signed with a private key. This chapter will work through the steps involved in obtaining a private key, preparing the Android App Bundle for the project and uploading it to Google Play.


Contents


The Release Preparation Process

Up until this point in the book, we have been building application projects in a mode suitable for testing and debugging. Building an application package for release to customers via Google Play, on the other hand, requires that some additional steps be taken. The first requirement is that the application be compiled in release mode instead of debug mode. Secondly, the application must be signed with a private key that uniquely identifies you as the application’s developer. Finally, the application must be packaged into an Android App Bundle.

While each of these tasks can be performed outside of the Android Studio environment, the procedures can more easily be performed using the Android Studio build mechanism as outlined in the remainder of this chapter. First, however, it is important to understand a little more about Android App Bundles.

Android App Bundles

When a user installs an app from Google Play, the app is downloaded in the form of an APK file. This file contains everything needed to install and run the app on the user’s device. Prior to the introduction of Android Studio 3.2, the developer would generate one or more APK files using Android Studio and upload them to Google Play. In order to support multiple devices types, screen sizes and locales this would require either the creation and upload of multiple APK files customized for each target device and locale, or the generation of a large universal APK containing all of the different configuration resources and platform binaries within a single package.

Creating multiple APK files involved a significant amount of work that had to be repeated each time the app needed to be updated imposing a considerable time overhead to the app release process.

The universal APK option, while less of a burden to the developer, caused an entirely unexpected problem. By analyzing app installation metrics, Google discovered that the larger an installation APK file becomes (resulting in longer download times and increased storage use on the device), the less conversions the app receives. The conversion rate is calculated as a percentage of the users who completed the installation of an app after viewing that app on Google Play. In fact, Google estimates that the conversion rate for an app drops by 1% for each 6MB increase in APK file size.

Android App Bundles solve both of these problems by providing a way for the developer to create a single package from within Android Studio and have custom APK files automatically generated by Google Play for each individual supported configuration (a concept referred to as Dynamic Delivery).

An Android App Bundle is essentially a ZIP file containing all of the files necessary to build APK files for the devices and locales for which support has been provided within the app project. The project might, for example, include resources and images for different screen sizes. When a user installs the app, Google Play receives information about the user’s device including the display, processor architecture and locale. Using this information, the appropriate pre-generated APK files are transferred onto the user’s device.

Although it is still possible to generate APK files from Android Studio, app bundles are now the recommended way to upload apps to Google Play.


Register for a Google Play Developer Console Account

The first step in the application submission process is to create a Google Play Developer Console account. To do so, navigate to https://play.google.com/apps/publish/signup/ and follow the instructions to complete the registration process. Note that there is a one-time $25 fee to register. Once an application goes on sale, Google will keep 30% of all revenues associated with the application.

Once the account has been created, the next step is to gather together information about the application. In order to bring your application to market, the following information will be required:

  • Title – The title of the application.
  • Short Description - Up to 80 words describing the application.
  • Full Description – Up to 4000 words describing the application.
  • Screenshots – Up to 8 screenshots of your application running (a minimum of two is required). Google recommends submitting screenshots of the application running on a 7” or 10” tablet.
  • Language – The language of the application (the default is US English).
  • Promotional Text – The text that will be used when your application appears in special promotional features within the Google Play environment.
  • Application Type – Whether your application is considered to be a game or an application.
  • Category – The category that best describes your application (for example finance, health and fitness, education, sports, etc.).
  • Locations – The geographical locations into which you wish your application to be made available for purchase.
  • Contact Details – Methods by which users may contact you for support relating to the application. Options include web, email and phone.
  • Pricing & Distribution – Information about the price of the application and the geographical locations where it is to be marketed and sold.

Having collected the above information, click on the Create Application button within the Google Play Console to begin the creation process.

Configuring the App in the Console

When the Create Application button is first clicked, the Store listing screen will appear as shown in Figure 1-1 below. The screen may also be accessed by selecting the Store listing option (marked A) in the navigation panel. Once all of the requirements have been met for the Store listing screen, both the Content rating (B) and Pricing & distribution (C) screens must also be completed:

 [[File:]] 

Once the app entry has been fully configured, the next step is to enable app signing and upload the app bundle.

Enabling Google Play App Signing

Up until recently, Google Play uploads were signed with a release app signing key from within Android Studio and then uploaded to the Google Play console. While this option is still available, the recommended way to upload files is to now use a process referred to as Google Play App Signing. For a newly created app, this involves opting in to Google Play App Signing and then generating an upload key that is used to sign the app bundle file within Android Studio. When the app bundle file generated by Android Studio is uploaded, the Google Play console removes the upload key and then signs the file with an app signing key that is stored securely within the Google Play servers. For existing apps, some additional steps are required to enable Google Play Signing and will be covered at the end of this chapter.

Within the Google Play console, select the newly added app entry from the dashboard and click on the App releases option located in the left-hand navigation panel. On the App releases screen, select the Manage button in the Internal test track section. The internal test track is particularly useful when performing tests early in the app development cycle, or when experimenting with Android App Bundles. Eventually the app will progress through Alpha and Beta testing before finally being ready for production release.

 [[File:]]


On the subsequent screen, click on the Create Release button to display the settings screen shown in Figure 1-3:

 [[File:]] 

To enroll the app in Google Play App Signing, click on the Continue button highlighted in the above figure. This will generate an app signing certificate for the app which will be securely stored within Google Play.

The next step is to generate the upload key from within Android Studio. This is performed as part of the process of generating a signed app bundle.

Enabling ProGuard

When generating an application package, the option is available to use ProGuard during the package creation process. ProGuard performs a series of optimization and verification tasks that result in smaller and more efficient byte code. In order to use ProGuard, it is necessary to enable this feature within the Project Structure settings prior to generating the app bundle file.

The steps to enable ProGuard are as follows:

1. Display the Project Structure dialog (File -> Project Structure).

2. Select the “app” module in the far left panel.

3. Select the “Build Types” tab in the main panel and the “release” entry from the middle panel.

4. Change the “Minify Enabled” option from “false” to “true” and click on OK.

5. Follow the steps to create a keystore file and build the app bundle file.

With the project configured for release building, the next step is to create a keystore file containing the upload key.

Creating a Keystore File

To create a keystore file, select the Build -> Generate Signed Bundle / APK… menu option to display the Generate Signed Bundle or APK Wizard dialog as shown in Figure 1-4:

 [[File:]] 

Verify that the Android App Bundle option is selected before clicking on the Next button.

In the event that you have an existing release keystore file, click on the Choose existing… button on the next screen and navigate to and select the file. If you have yet to create a keystore file, click on the Create new… button to display the New Key Store dialog (Figure 1-5). Click on the button to the right of the Key store path field and navigate to a suitable location on your file system, enter a name for the keystore file (for example, release.keystore.jks) and click on the OK button.

The New Key Store dialog is divided into two sections. The top section relates to the keystore file. In this section, enter a strong password with which to protect the keystore file into both the Password and Confirm fields. The lower section of the dialog relates to the upload key that will be stored in the key store file.

 [[File:]] 

Within the Certificate section of the New Key Store dialog, enter the following details:

  • An alias by which the key will be referenced. This can be any sequence of characters, though only the first 8 are used by the system.
  • A suitably strong password to protect the key.
  • The number of years for which the key is to be valid (Google recommends a duration in excess of 25 years).

In addition, information must be provided for at least one of the remaining fields (for example, your first and last name, or organization name).

 [[File:]]


Once the information has been entered, click on the OK button to proceed with the bundle creation.

Creating the Android App Bundle

The next step is to instruct Android Studio to build the application app bundle file in release mode and then sign it with the newly created private key. At this point the Generate Signed Bundle or APK dialog should still be displayed with the keystore path, passwords and key alias fields populated with information:

 [[File:]]


Make sure that the Export Encrypted Key option is enabled and, assuming that the other settings are correct, click on the Next button to proceed to the app bundle generation screen (Figure 1-8). Within this screen, review the Destination Folder: setting to verify that the location into which the app bundle file will be generated is acceptable. In the event that another location is preferred, click on the button to the right of the text field and navigate to the desired file system location.

 [[File:]] 

Click on the Finish button and wait for the Gradle system to build the app bundle. Once the build is complete, a dialog will appear providing the option to open the folder containing the app bundle file in an explorer window, or to load the file into the APK Analyzer:


[[File:]]


At this point the application is ready to be submitted to Google Play. Click on the locate link to open a filesystem browser window. The file should be named bundle.aab and be located in the app/release sub-directory of the project folder unless another location was specified.

The private key generated as part of this process should be used when signing and releasing future applications and, as such, should be kept in a safe place and securely backed up.

Generating Test APK Files

An optional step at this stage is to generate APK files from the app bundle and install and run them on devices or emulator sessions. Google provides a command-line tool called bundletool designed specifically for this purpose which can be downloaded from the following URL:

https://github.com/google/bundletool/releases

At time of writing, bundletool is provide as a .jar file which can be executed from the command line as follows (noting that the version number may have changed since this book was published):

java -jar bundletool-all-0.3.3.jar

Running the above command will list all of the options available within the tool. To generate the APK files from the app bundle, the build-apks option is used. To generate APK files that can be installed onto a device or emulator the files will also need to be signed. To achieve this include the --ks option specifying the path of the keystore file created earlier in the chapter, together with the --ks-key-alias option specifying the alias provided when the key was generated.

Finally, the --output flag must be used to specify the path of the file (referred to as the APK Set) into which the APK files will be generated. This file must not already exist and is required to have a .apks filename extension. Bringing these requirements together results in the following command-line (allowing for differences in your operating system path structure):

java -jar bundletool-all-0.3.3.jar build-apks --bundle=/tmp/MyApps/app/release/bundle.aab --output=/tmp/MyApks.apks --ks=/MyKeys/release.keystore.jks --ks-key-alias=MyReleaseKey

When this command is executed, a prompt will appear requesting the keystore password before the APK files are generated into the specified APK Set file. The APK Set file is simply a ZIP file containing all of the APK files generated from the app bundle.

To install the appropriate APK files onto a connected device or emulator, use a command similar to the following:

java -jar bundletool-all-0.3.3.jar install-apks --apks=/tmp/MyApks.apks

This command will instruct the tool to identify the appropriate APK files for the connected device and install them so that the app can be launched and tested.

It is also possible extract the APK files from the APK Set for the connected device without installing them. The first step in this process is to obtain the specification of the connected device as follows:

java -jar bundletool-all-0.3.3.jar get-device-spec --output=/tmp/device.json

The above command will generate a JSON file similar to the following:

{
  “supportedAbis”: [“x86”],
  “supportedLocales”: [“en-US”],
  “screenDensity”: 420,
  “sdkVersion”: 27
}

Next, this specification file is used to extract the matching APK files from the APK Set:

java -jar bundletool-all-0.3.3.jar extract-apks --apks=/tmp/MyApks.apks --output-dir=/tmp/nexus5_apks --device-spec=/tmp/device.json

When executed, the directory specified via the --output-dir flag will contain correct APK files for the specified device configuration.

The next step in bringing an Android application to market involves submitting it to the Google Play Developer Console so that it can be made available for testing.

Uploading App Bundle to the Google Play Developer Console

Return to the internal testing track screen within the Google Play Console and upload the app bundle by dragging and dropping it onto the designated area, or click on the button to browse for the bundle file:

 [[File:]]


After the app bundle file has uploaded, Google Play will generate all of the necessary APK files ready for testing. Once the APK files have been generated, scroll down to the bottom of the screen and click on the Save button. Once the settings have been saved, click on the Review button.

Exploring the App Bundle

On the review screen, click on the bundle name to unfold the information about the bundle as shown in Figure 1-11:

 [[File:]] 

This section of the screen provides summary information relating the API levels, screen layouts and platforms supported by the app bundle. For more information, click on the Explore App Bundle option highlighted in the figure above. This will display information about the size savings compared to a universal APK file (marked A in Figure 1-12) and provide the option to download individual APK files for local testing purposes (B) as an alternative to using the bundletool:

 [[File:]] 

The bundle explorer also lists the supported screen densities (C) and respective install APK size. Clicking on a View Devices option for a screen density will display the devices that are supported by the APK file:


[[File:]]


At this point, the app is ready for testing but cannot be rolled out until some testers have been set up within the console.

Managing Testers

If the app is still in the Internal, Alpha or Beta testing phase, a list of authorized testers may be specified by selecting the app from within the Google Play console, clicking on App releases in the navigation panel, selecting the Manage button for the release type and unfolding the Manage testers section of the release screen as shown in Figure 1-13:

 [[File:]] 

The following options are available for app testing:

  • Internal Testing – The app is made available to up to 100 designated internal testers. This is the fastest way to get the app to a small group of known testers.
  • Closed Testing – Testing is only available for designated users identified by email address or membership in Google Groups and Google+ communities.
  • Open Testing – The app is made available to all users within the Google Play Store. Users are provided with a mechanism to provide feedback to you during testing. The total number of testers may also be specified (though the number cannot be less than 1000 users).

To configure testing, use the Choose testing method menu to select the type of testing to be performed, click on the Create List button and specify the email addresses for the test users either manually or by uploading a CSV file.

The opt-in URL will be displayed when the app has been rolled out for testing. Return to the Review page for the app (App Releases -> Manage -> Edit Release -> Review) and click on the Start Rollout to Internal Test button.

The URL may now be copied from the manage testers screen (Figure 1-13 above) and provided to the test users so that they accept the testing invitation and download the app.

Uploading New App Bundle Revisions

The first app bundle file uploaded for your application will invariably have a version code of 1. If an attempt is made to upload another bundle file with the same version code number, the console will reject the file with the following error:

You need to use a different version code for your APK because you already have 
one with version code 1.

To resolve this problem, the version code embedded into the bundle file needs to be increased. This is performed in the module level build.gradle file of the project, shown highlighted in Figure 1-16:

 [[File:]] 

By default, this file will typically read as follows:

apply plugin: 'com.android.application'
 
android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "com.ebookfrenzy.demoapp"
        minSdkVersion 14
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
 
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.0.2'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:design:26.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
} 

To change the version code, simply change the number declared next to versionCode. To also change the version number displayed to users of your application, change the versionName string. For example:

versionCode 2
versionName "2.0"

Having made these changes, rebuild the APK file and perform the upload again.

Analyzing the App Bundle File

Android Studio provides the ability to analyze the content of an app bundle file. To analyze an bundle file, select the Android Studio Build -> Analyze APK… menu option and navigate to and choose the bundle file to be reviewed. Once loaded into the tool, information will be displayed about the raw and download size of the package together with a listing of the file structure of the package as illustrated in Figure 1-17:

Android studio 3.2 bundle analyzer.png 

Selecting the classes.dex file will display the class structure of the file in the lower panel. Within this panel, details of the individual classes may be explored down to the level of the methods within a class:

As3.0 build analyze dex.png


Similarly, selecting a resource or image file within the file list will display the file content within the lower panel. The size differences between two bundle files may be reviewed by clicking on the Compare with previous APK… button and selecting a second bundle file.

Enabling Google Play Signing for an Existing App

To enable Google Play Signing for an app already registered within the Google Play console, begin by selecting that app from the list of apps in the console dashboard. Once selected, click on the App signing link in the left-hand navigation panel as shown in Figure 1-19:

Android studio download signing tools.png 

The first step is to click on the button to download the PEPK Tool (A) which will be used to encrypt the app signing key for the project. Once downloaded, copy it to the directory containing your existing keystore file and run the following command where (<your app signing key file> and <your alias> are replaced by the name of your keystore file and the corresponding alias key respectively):

java -jar pepk.jar --keystore=<your app signing key file> --alias=<your alias> --output=encrypted_private_key_path --encryptionkey=<your app signing key>

Enter the keystore and key passwords when prompted, then check that a file named encrypted_private_key_path has been generated. This file contains your app signing key encrypted for uploading to the Google Play Store. Return to the Google Play console, click on the App Signing Key button (B) and upload the encrypted_private_key_path file.

Next, follow the steps outlined earlier in this chapter to generate the upload key and store it in a new keystore file. In a terminal or command-prompt window, change directory to the location of the upload keystore file and run the following command to convert the keystroke into a PEM certificate format file:

keytool -export -rfc -keystore <your upload key file> -alias <your alias> -file upload_certificate.pem

With the file generated, click on the Upload Public Key Certificate button (C) in the Google Play console and upload the PEM certificate file.

Finally, enroll the app in Google Play Signing by clicking on the Enroll button (D). Once the app is enrolled, the new upload keystore file must be used whenever the signed app bundle file is generated within Android Studio.

Summary

Once an app project is either complete, or ready for user testing, it can be uploaded to the Google Play console and published for production, internal, alpha or beta testing. Before the app can be uploaded, an app entry must be created within the console including information about the app together with screenshots to be used within the Play Store. A release Android App Bundle file is then generated and signed with an upload key from within Android Studio. After the bundle file has been uploaded, Google Play removes the upload key and replaces it with the securely stored app signing key and the app is ready to be published.

The content of a bundle file can be reviewed at any time by loading it into the Android Studio APK Analyzer tool.