Automating Publishing to the Play Store - TechGeekD/android_guides GitHub Wiki
The Developer Console for Google Play provides API support for you to be able to push to the store automatically. This ability allows you to trigger builds on your continuous integration server (i.e. Jenkins) and have them uploaded the Play store for alpha or beta testing, as well as pushing to production directly.
This guide will show you how you can setup publishing APK's directly through the command-line, or how you configure a continuous integrations server such as Jenkins to do the same. Regardless of which approach, you will need to setup Google API access.
Setup for Google API access
-
Inside the Google Play Store for your project, navigate to
Settings
->API Access
:<img src="http://imgur.com/0n7ihzM.png"/>
-
There should be a
Service Accounts
section where you need to click theCreate Service Account button
. Click on the link shown on the first step to visit the Google Developers Console.<img src="http://imgur.com/6TnR700.png"/>
-
Click on the
Create New Client ID
button.<img src="http://imgur.com/7VPlkHM.png"/>
-
Click to create new
Service Account
. Make sure to also request the.p12 Key File
instead of JSON:<img src="http://imgur.com/paTHMHK.png"/>
-
You will be prompted to download the .p12 file. Save it somewhere.
-
Note the service account email associated with this new account. You should see it appear in the Google Developer Console:
<img src="http://imgur.com/TVm6CLM.png"/>
-
Once you are done, go back to the Google Play Developer Console and navigate to the
Settings
->API Access
. Make sure the checkboxes forEdit store listing, pricing & distribution
,Manage Production APKs
, andManage Alpha & Beta APKs
are checked for the Google Service account used. (If you intend to upload an alpha or beta SDK through a Google service account, apparently these permissions must be checked according to this discussion.<img src="https://i.imgur.com/QBF0Vmp.png"/>
Setting Up Gradle Plugin (for command-line APK publishing)
If you want to be push your APKs directly through Gradle, you can install a plugin such as the Gradle Play Publisher.
-
Add the following to the top of your
app/build.gradle
file:```gradle buildscript { repositories { jcenter() } dependencies { // ... classpath 'com.github.triplet.gradle:play-publisher:1.1.2' }
}
apply plugin: 'com.github.triplet.play' ```
-
Configure the plugin with the Google Service Account and p12 file saved in steps #5 and #6.
```gradle
play { track = 'alpha' serviceAccountEmail = '[email protected]' pk12File = file('Google Play Android Developer-12345.p12') }
-
The plugin creates the following tasks for you:
| Command | Description | |:---------------------------:|--------------------------------------------------------------------- | |publishApkRelease | Uploads the APK and the summary of recent changes. | |publishListingRelease | Uploads the descriptions and images for the Play Store listing. | |publishRelease | Uploads everything. | |bootstrapReleasePlayResources| Fetch data from the Play Store & bootstrap the required files/folders|
You can now type the following gradle commands such as the following:
./gradlew publishApkRelease
Setting Up Jenkins (for automating CI builds)
This section mainly shows how to setup the Google Play Android Publisher plugin with Jenkins. You can most likely adapt the same steps for other services that enable beta distribution and have a related plugin, such as the one for Hockey.
-
Make sure you have already gone through the process of Building Gradle Projects with Jenkins CI and already have a Jenkins job correctly running. You will only need to install a Jenkins plugin that will allow you to create a build step that will enable the APK generated to be published to the Google Play store directly.
-
Verify that you've the followed the guide about how to configure Google API access.
-
Inside Jenkins, go to
Manage Jenkins
->Manage Plugins
. Assuming the plugin has not yet been installed, select theAvailable
tab and search for theGoogle Play Android Publisher Plugin
. -
Navigate to the
Credentials
section in Jenkins and load the.p12
key file downloaded during the initial setup process of setting up Google API access. A basic walkthrough video also demonstrates how to do this step.<img src="https://i.imgur.com/xxs8qlD.png"/>
-
Add a post-build step to your existing Jenkins project.
<a href="https://i.imgur.com/nfc4xDA.png"><img src="https://i.imgur.com/nfc4xDA.png"></a> a. Make sure to choose the credential name from the drop-down list. It should belong to the Google Play account that manages the app. b. Enter path and/or an [Ant-style](http://stackoverflow.com/questions/69835/how-do-i-use-nant-ant-naming-patterns) wildcard pattern for the APK. For instance, the example in the screenshot expects the APK to be generated inside `**/build/outputs/apk/yourappname*.apk`. c. Choose what track to which the APKs should be deployed (Alpha, Beta, Production). d. You can create release notes before you start the build. If you forget to do this step or your automated process pushes the build, you can edit them later directly on the Google Play Developer Console.
GitHub / Jenkins integration
Do not follow these steps if you intend for your Jenkins jobs to push directly to Production tracks. You should always verify your builds before rolling out to production users.
-
Setup a
Build Trigger
in your Jenkins job to allow builds to be triggered via remote API commands. You can use the URL shown below theAuthentication token
(i.e. http://ci.mycompany.com/view/All/job/AndroidBuild/build?token=TOKEN_NAME) to trigger this Jenkins job.<img src="https://i.imgur.com/QfzhhQM.png"/>
-
Go to your GitHub repository
Settings
page and visit theWebhooks & Services
section. Enter thePayload URL
for your Jenkins build job.<img src="https://i.imgur.com/iONpTHh.png"/>
-
You can also control which GitHub events should fire these Jenkins build jobs:
<img src="https://i.imgur.com/JpwMRTn.png/">