Setup and build Android app from Jenkins

Author: Roman Kushnarenko Oct 09, 2017 DevOps Jenkins Android

In this step by step guide I will explain how to setup very basic end to end flow of building Android debug builds.


Setup VM

Create new Ubuntu 16.04 VM instance from dev console : n1-standard-1 (1 vCPU, 3.75 GB memory)

Do it once, create SSH key
  1. Download gcloud to your local machine install link.

  2. Go to VM instance in dev console and in SSH options choose: View gcloud command.

  3. Copy the command and run it. For example:

    gcloud compute --project "my-secret-project-name" ssh --zone "us-central1-a" "jenkins"

    The SSH private & public keys will be created - ~/.ssh/google_compute_engine.

  4. Copy the public key and add to your console

    # -- show the content of the key --
    $ cat ~/.ssh/

    Copy the public key value to your console. Under Metadata section -> SSH Keys. Ref: this link.

Connect (everytime)

PATH_TO_PRIVATE_KEY = is the path to your private SSH key file. In this case it is: ~/.ssh/google_compute_engine

USERNAME = is the name of the user connecting to the instance. The username for your SSH key pair was specified when the SSH key pair was created. You can connect to the instance as that user if the instance has a valid public SSH key for that user and if you have the matching private SSH key.

EXTERNAL_IP_ADDRESS is the external IP address for your the path to your privaate SSH key file.

Open port 8080
  • Select VM, scroll to Network interfaces -> Network column -> Select default

  • Scroll down and click Add firewall rule

    name value
    Name allow-8080
    Targets All instances in the network
    Source filter
    Allowed protocols and ports tcp:8080

Congrats, you can connect to VM from your machine.

Setup Jenkins

  • Install JDK 8:

    sudo apt-get install openjdk-8-jdk
  • Install Jenkins server:

    wget -q -O - | sudo apt-key add -
    sudo sh -c 'echo deb binary/ > /etc/apt/sources.list.d/jenkins.list'
    sudo apt-get update
    sudo apt-get install jenkins
  • Set some password from newly created user jenkins:

    sudo passwd jenkins
  • Start jenkins server:

    sudo service jenkins start
  • Open firewall on tcp:8080 (check VM setup in Ubuntu section)

  • Open http://<your-vm-domain-or-ip>:8080

  • Copy the generated password:

    sudo cat /var/lib/jenkins/secrets/initialAdminPassword
  • Install suggested plugins. We will anyway come back to plugins we need later. But for now, you can install what is suggested.

  • Complete: Create First Admin User -> Save and Finish

Congrats, you have Jenkins working and running.

Setup Android SDK

  1. Go to and copy link address of Linux binary. In my case:

  2. Create directory: /var/lib/jenkins/android-sdk/

  3. Download the zip file and unzip

    # download android sdk
    sudo curl -o
    # install unzip command
    sudo apt-get install unzip
    # unzip into this folder
    sudo unzip -d .
    # remove the zip file
    sudo rm
  4. Update tools

    sudo ./bin/sdkmanager "tools"
  5. Install latest Android SDK Build-Tools, Android SDK Platform, Android Support Repository, Google Repository

    sudo ./bin/sdkmanager "build-tools;26.0.2" "platforms;android-26" "extras;android;m2repository" "extras;google;m2repository"
  6. Install additional mostly used libs: Solver for ConstraintLayout, ConstraintLayout for Android

    sudo ./bin/sdkmanager "extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.2" "extras;m2repository;com;android;support;constraint;constraint-layout;1.0.2"
  7. Check list of all available packages and installed

    sudo ./bin/sdkmanager --list

Configure Jenkins

Global Properties

Open Jenkins: Manage Jenkins -> Configure System -> Global properties. Mark “Environment variables” and add:

  • ANDROID_HOME : /var/lib/jenkins/android-sdk
  • JAVA_HOME : /usr/lib/jvm/java-8-openjdk-amd64

Press Save

Add SSH Credentials to Github / Bitbucket repository
  1. Switch to jenkins user:

    su - jenkins
  2. Create new SSH key and use defaulf name: id_rsa (see SSH section)

    Generate new SSH key:

    ssh-keygen -t rsa -b 4096 -C ""

    the default name will be id_rsa, but you can set another name if you need

    Add key to the ssh-agent:

    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_rsa

    Give more permissions:

    chmod 700 ~/.ssh/id_rsa

    Show public key content:

    cat ~/.ssh/
  3. Copy the content and paste.

    • For Github: -> Go to Your Github repository -> Settings -> Deploy keys.
    • For Bitbucket: -> Go to Yout Bitbucket repository -> Settings -> Access keys.
  4. Open Jenkins and go to Credentials-> System -> Global credentials -> Add Credentials

    Field Value
    Kind SSH Username with private key
    Scope Global
    Username jenkins
    Private Key From the Jenkins master ~/.ssh
    Passphrase ‘the one you used in key creation’

Note: Private Key - if you use custom name and not default one, then be sure setting the full path to private key. Like: /var/lib/jenkins/.ssh/my_rsa_key

Create Android build job

  • Open Jenkins -> New Item. Enter any job name. Choose Freestyle projet. Press OK.

  • This build is parameterized -> add String Parameter. Give a name: branch

  • Source Code Management -> Check Git and give:

    • Repository URL: Git url to your repo. Take this URL from Github. It should be format of{username}/{repo}.git
    • Credentials: Select the one you created before.
    • Branches to build: $branch
  • Build -> Execute shell:

    chmod +x gradlew
    ./gradlew clean
    ./gradlew assembleDebug
  • Post-build Actions -> Archive artifacts: **/*.apk

Press Save.

You can always configure this job later.

Run the job
  • Go to job and press Build with Parameters

  • Enter master branch.

Press Start.

Now, you can watch the progress in Console Output. Once the job is finished successfully, you will see the APK :tada:.

Errors you might get
  1. Cannot run program “git”

    You are missing git, just install it and run the job again.

    sudo apt-get install git
  2. Lint failing. Add to gradle file:

    lintOptions {
        checkReleaseBuilds false
        abortOnError false
  3. Missing some SDK components. Make sure to install them via sdkmanager as we did before.

Helpful stuff
  • Start / restart jenkins

    sudo service jenkins start
    sudo systemctl restart jenkins
  • Update Jenkins

    Once in a while it’s good to update to latest version.

    sudo apt-get update
    sudo apt-get install jenkins
  • Jenkins log tail

    tail -f /var/log/jenkins/jenkins.log

Next blog post

Build by types and sign Android app from Jenkins

In this part I will explain how to update Gradle file, setup signing options and build by types from Jenkins. Keeping sensitive keystore credential hidden.


If you have any comments, please open an issue at