Download Jenkins and follow the installing instructions. It shouldn't be tricky.
- Git plugin (clone the project)
- Gradle Plugin (build/test/etc. Android project)
- Mailer Plugin (notify the team)
- Android emulator plugin (run Android emulator)
- Static Analysis Collector Plug-in (combined report of Checkstyle/DRY/FindBugs/PMD reports)
- JaCoCo Plugin (collect code coverage)
- Checkstyle Plug-in (Checkstyle errors/warnings)
- Android Lint plugin (Lint errors/warnings)
- JUnit Plugin (unit tests report)
- HockeyApp Plugin or Fabric Beta Publisher (distribute APK's)
Optional
- Next build number (set next build number used as versionCode)
- Clone Workspace SCM Plugin (clone workspace and use in downstream job)
- JobConfigHistory Plugin (saves copies of all job and system configurations)
- Embeddable Build Status Plugin (build status badge)
In order to build the project we need to have environment variables set in Jenkins (Manage Jenkins -> Configure system -> Global properties -> Environment variables
):
-
Create job with Freestyle type:
-
Configure source code management section:
-
Configure webhooks/polling:
In order to build the project automatically when changes are pushed to the remote repository webhook should be configured. How to create a webhook.
If 2fa is enabled on github, then it will not be possible to configure webhook as mentioned in previous link. Polling might be used as an alternative in this case.
-
Configure Android emulator for UI tests. It will be started automatically during the build:
Startup delay isn't mandatory. In order to start Android emulator during the build ensure that all required Android targets and ABI's installed on Jenkins node.
android list targets
command shows "Available Android targets". -
Configure how the project will be built using Gradle build scripts
In "Tasks" section we need to specify a list of Gradle tasks to be invoked.
For example:
clean connectedAndroidTest assembleDebug testDebug jacocoTestReport checkstyle pmd jdepend lintDebug buildDashboard -PversionCode=${BUILD_NUMBER} -PfabricApiKey="YOUR_KEY" -PfabricApiSecret="YOUR_SECRET"
-
Configure reports
Make sure JaCoCo exclusions match JaCoCo project configuration.
-
Configure APK distribution to Fabric Beta
-
Configure email notifications
Bonus: Jenkins job described above can be easily created using jenkins/config.xml file.
- Check Jenkins HOME directory structure to have better understanding where Jenkins keeps jobs.
- Create subdirectory in
JENKINS_HOME/jobs
and copyconfig.xml
file there. - Restart jenkins (
https://YOUR_JENKINS_SERVER_URL/restart
) to apply the changes. - Configure the job with missing secret keys (Fabric API key & secret, etc.)
Not all plugins are compatible with Pipeline plugin. Check COMPATIBILITY document for complete information.
jenkins/Jenkinsfile contains Pipeline job configuration.
It contains clone/build/test/analyse/notify
steps as a regular job.
Secret keys must be exported on the Jenkins node or set directly in Jenkinsfile or passed as build parameters like
env.FABRIC_API_KEY = "${FABRIC_API_KEY}"
env.FABRIC_API_SECRET = "${FABRIC_API_SECRET}"
How to create Pipeline job:
-
Create job with Pipeline type
-
Configure build parameters
-
Add Pipeline script or use from Jenkinsfile
-
Build the project by pushing it to remote repository or manually
Different Android project might use different Android SDK versions and build tools. These should be installed on the build machine. Having 2 jobs in Jenkins which help with Android dependencies installation should be enough:
-
install_specific_android_sdk.sh - This will install specific Android SDK on the build machine. Create parametrized job with content of this shell script and
SDK_LIST
String parameter (comma-separated Android SDK versions). -
update_android_sdk.sh - This will update/install
all
Android SDK versions. Create parametrized job with content of this shell script and schedule periodic builds of this job. Would be nice to execute this job every night to make sure that build machine has latest Android SDK versions installed. To achieve that we need to enable periodic midnight builds -Build Triggers -> Build periodically -> @midnight
.