This is 2nd article in series of articles for setting up an Android build. I hope you have read the previous one here Part 1 – Setting up Android build on Jenkins
Requirements for this tutorial is that you have a working Android Project which builds fine inside Android Studio. Source code for this sample is at https://github.com/vnextcoder/trav. So lets crack on !
Step 1 – Login (obviously) , login to Jenkins CI server.
Step 2 – Click ‘Create New Jobs” on the home screen below . Once you do that select Freestyle Project and click OK
Step 3 – Select Github project and set Project URL as the root where gradlew or gradlew.bat resides.
In my case it is there at https://github.com/vnextcoder/trav. Additionally setup GIT in SCM and GIT repository URL as https://github.com/vnextcoder/trav. IN branches to build, you can choose master as of now “*/master ” which is just descendent of the GIT Repo root.
Step 4 – Build triggers
Build Triggers are parameters which would trigger the build. if you dont setup any , the build need to be triggered manually . We would setup a SCM Polling to poll every 10 mins for any SCM changes and trigger the build. H/10 * * * * means it would be run every 10 mins.
Step 5 – Build step – Update Android SDK
Jenkins doesn’t specify any pre-build steps like Circle CI or Travis-CI if you are big fan of those Cloud solutions, However, Jenkins has whole lot of customization over and above those nasty YML files. Under Jenkins, you can run virtually any Shell script / tool .
First build step for any Android build should be to get all the build, platform-tools, etc which are needed for the project build. so lets create a Shell script build
Add build step –> Execute Shell
add below line on the step
echo yes | android update sdk --no-ui --all --filter tools,platform-tools,build-tools-24.0.3,android-24,extra-google-m2repository,extra-google-google_play_services,extra-android-support
Now, this step would make sure all the build tools, source libs, m2repo everything is up to date on the $ANDROID_HOME folder before the Gradle build actually kicks in. Do note that –filter only works with –all option . “echo yes” would make sure that the License agreements are accepted automatically by the build. So far I haven’t found a way to configure this task to not update the android sdk in case it is already updated. This seems to be a feature request and I have requested the same https://code.google.com/p/android/issues/detail?id=224879. Many Stackoverflow questions on this topic are still unanswered.
Step 6 – Add one more “execute shell script” step to accept license automatically –
mkdir "$ANDROID_HOME/licenses" || true echo -e "\n8933bad161af4178b1185d1a37fbf41ea5269c55" > "$ANDROID_HOME/licenses/android-sdk-license" echo -e "\n84831b9409646a918e30573bab4c9c91346d8abd" > "$ANDROID_HOME/licenses/android-sdk-preview-license"
Step 7 – Add Gradle build step.
The Gradle wrapper plugin works same way as the gradle wrapper from within the Android studio. Do select tick mark “make gradlew executable” . This would make sure that execute permissions are applied on the gradlew script after SCM checkout. Under the tasks add “clean build” string to execute clean and build tasks.
Step 8 – Post – build Actions.
Jenkins allows to archive the artifacts or even push them to Play store (custom plugins needed). At the moment we would archive the apk files . Click Post-build actions –> add Post-build action –> Archive the artifacts . in the Files to archive enter “**/*.apk”. This is a glob which would find out any *.apk files under any file depth and archive them .
Save the build now.
Step 9 – Trigger the build.
Click build Now from the build configuration screen. next go to Console Output and which should show the real build run output
If all goes well you should see “BUILD SUCCESSFUL” message in end like below .