Jenkins is a great way to automate your builds. Setting it up can be a little tricky, so here are a bunch of lessons learned from setting up Jenkins on a buildserver and configuring it for android builds.
Most of the Android SDK/Build Tool install process assumes you are using Android Studio. Since we are not, we’ll have to do our initial setup manually. Note that setting up the gradle wrapper will let Jenkins get additional SDKs.
To install old versions of the Android build tools, use:
$ANDROID_HOME/tools/android update sdk --all --filter build-tools-21.1.0 --no-ui
Substitute the version you want as appropriate.
Helpfully, you can configure various path variables from within Jenkins on the Configure Jenkins screen.
You may be interested in integrating unit or integration tests with Jenkins and your automated build process. To do this, you can build your unit and integration tests in Android Studio as you usually would, and set up your gradle tasks. You can then use the gradle task ‘test’ in Jenkins to call this as part of your build process. You’ll also want to add the option ‘--rerun-tasks’ to make sure they run every time, even if there are no changes to your tests. By default, gradle will only rebuild (and consequently rerun) your tests if they have changed.
When doing signed release builds, you will need to provide a path to your release key and the passwords. The simplest way to do this is to put both the path and the passwords in your build.gradle file, but you may not wish to expose your passwords this way.
If you do not, I have found that the following procedure works well. The mask passwords plugin may be useful here. In your build configuration in Jenkins, check ‘Use Parameterized Build’. Create parameters for your passwords, e.g. ‘MY_KEYSTORE_PASSWORD’ and ‘MY_ALIAS_PASSWORD’. You can save your passwords here if you feel comfortable doing so, or you can let the system prompt you when you run a build. With the <which?> plugin, your passwords will be obfuscated and not visible in the logs.
Then, to get access to your passwords in your build.gradle, use:
signingConfigs {
…
release {
storeFile file("/path/to/my/keystore/on/server.keystore")
storePassword "${System.getenv("MY_KEYSTORE_PASSWORD")}"
keyAlias "my-key-name"
keyPassword "${System.getenv("MY_ALIAS_PASSWORD")}"
}
}
The release builds themselves can be downloaded from within the Jenkins file browser by selecting ‘workspace’ and navigating to your /build/outputs/apk/ directory. You can then download the apk and post it manually. This will be overwritten after every build. If you would like to keep old builds around, you can add a post-build action to ‘Archive the Artifacts’ and tell it to keep .apk files.
If desired, you can also automate the pushing of completed builds to the Play store using the Google Play Android Publisher Plugin. You will only want this for your production build tasks though.
Comments