Setup and build Android app from Jenkins

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.

Steps

Setup VM

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


Connect using gcloud CLI:
  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.


Connect using SSH command:

Run (only once on machine that connects to VM):

# -- see all keys --
$ ls -al ~/.ssh

# -- add for later authentications --
$ ssh-add ~/.ssh/google_compute_engine

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

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



Connect

Run:

ssh -i [PATH_TO_PRIVATE_KEY] [USERNAME]@[EXTERNAL_IP_ADDRESS]

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

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 instance.is the path to your private SSH key file.


Open port 8080

Open firewall to 8080.

  • Select VM, scroll to Network interfaces -> Network column -> Select default
  • Scroll down and click Add firewall rule

    name value
    Name allow-8080
    Source filter 0.0.0.0/0
    Allowed protocols and ports tcp:8080

Setup Jenkins

  • Install JDK 8:

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

    wget -q -O - https://jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
    sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian 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 https://developer.android.com/studio/index.html#command-tools and copy link address of Linux binary. In my case: https://dl.google.com/android/repository/sdk-tools-linux-3859397.zip

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

  3. Download the zip file and unzip

    # download android sdk
    sudo curl https://dl.google.com/android/repository/sdk-tools-linux-3859397.zip -o android-sdk.zip
    
    # install unzip command
    sudo apt-get install unzip
    
    # unzip into this folder
    sudo unzip android-sdk.zip -d .
    
    # remove the zip file
    sudo rm android-sdk.zip
    
  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
  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 "your_email@example.com"
    

    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 id_rsa
    

    Show public key content:

    cat ~/.ssh/id_rsa.pub
    
  3. Copy the id_rsa.pub content and paste in -> Go to Github repository -> Settings -> Deploy 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’


Create Android build job

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

  • Check: This build is parameterized and 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 git@github.com:{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.

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. Lint failers. Add to gradle file:

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