How to Set Up Jenkins and Jenkins Pipeline with Android : Step-by-Step Guide (Part I)
Today, we’ll start to talk about how to set up Jenkins in the first part of series. Throughout the series of articles, each article will represent a step for the Jenkins pipeline setup.
Let’s start;
To briefly talk about Jenkins, it is an open source server for automating many existing jobs in software development stages. in their own words;
Jenkins is a self-contained, open source automation server which can be used to automate all sorts of tasks related to building, testing, and delivering or deploying software.
If you want, you can get more detail info here. We’ll start with the following steps ;
How to …
1-) Install And Set Up Jenkins
2-) Configure Jenkins for Android
3-) Create an Android Project in Jenkins
4-) Build Android Project
5-) Get Archiving Artifacts (Generate APKs)
1-) How to Download And Install Jenkins?
In order to use Jenkins, we will install Jenkins on a computer system which is windows or macOS. We will perform all things on macOS throughout the tutorial.
Firstly, we need to download Jenkins depending on operating sistem. You can download the compatible Jenkins for your operating system. Since my operating system is macOS, I will choose macOS option as below. If your operating system is different, you should choose the one compatible. You can find all of them available list here.
If you use windows, you can download .msi extension installing file and install it easily but if you can use macOS, you need to use Homebrew package manager to download and install. If you haven’t Homebrew for your macOS, you can get it here.
After installing Homebrew, run the commands below to install Jenkins.
$ brew install jenkins-lts
If all go well, you should get a result like the one below. Since I have a Jenkins installed before, it gave an up-to-date warning. But if you haven’t Jenkins installed your computer system before, it will directly output successful.
If you’ve come this far without any problems, now it’s time to run Jenkins. You can start Jenkins with the following command. Note: If you are using windows this won’t be necessary.
$ brew services start jenkins-lts
After this step, the web browser will automatically be directed to the Jenkins setting up screen. If this screen don’t appear, you can access it via the http://localhost:8080/ port assigned by default from any web browser here. When you click on the link, a screen like the one below will be appear for you.
By default, Jenkins creates a unique password at startup and stores it under the set initialAdminPassword file. We need to take the password in the unique file specified there and write it here. On the screen you can see where you can find the file path.
After this step, you will see the customize Jenkins screen that contains two options the install suggested plugins and the select plugins to install. We will choose the install suggested plugins as it will bring us the most commonly used plugins as git and Gradle. If you want to choose different plugins according to your needs, you can continue with the select plugins to install options.
After clicked the install suggested plugins, you will see the installing plugin list screen below. This process may take 5 to 10 minutes.
After the installation process is completed, the user screen is shown as below. You have to fill in the information here. Don’t forget your password and user name as you will be logged into the system with the username information and password here.
After clicked save and finish button, finally you will see the instance configuration screen there. You need to set the port where Jenkins will run. By default, 8080 comes. If there is no other server running on the same port and conflicting with this port, we can use this port without changing it.
After completing the instance configuration step, if you managed to get the jenkins dashboard screen below, congratulations Jenkins is ready and now we can start to set up the build system. 🎊
2-) Configure Jenkins for Android
The steps we will take after this point will be to create the environment that is the build system. Let’s set up the necessary all things at this point, step by step;
Firstly, we need to define Java Development Kit (JDK), Git, Gradle and android SDK to the system. Git and Gradle plugins are already installed due to the install suggested plugins option we chose at first. Now, we only need to install Android SDK Tools and JDK.
- How to install Java Development Kit (JDK) ?
From here, you can easily install java according to the operating system you use. After the JDK is installed, we will need to define Jenkins as environment variables. If you already have an android studio installed on your system and you have a project that you can get build without any problem, you can quickly get the JDK path from it as below.
File -> Project Structure -> SDK Location -> Gradle -> Gradle JDK
If you haven’t any installed android studio, you can get the JDK path command below on macOS.
$ /usr/libexec/java_home
Now it’s time to add environment variables as global properties for Jenkins. Let’s follow the Manage Jenkins -> Configure System tabs on the Jenkins dashboard or you can access there http://localhost:8080/configure. Now find the global properties section on the screen that appear. And click to enable the environment variables checkbox.
Now let’s add our JDK path as property as follows.
After added JDK variable, click apply and save buttons. now let’s continue to configure with android SDK tools.
- How to install Android SDK Tools?
If there is no android SDK installed on your computer, we can download and install it on our computer from here. If you already have an android SDK installed, you can get the path as below.
If everything is ok until this stage, now we should add the Android SDK as the environment variable as we added the previous JDK. You can add it under JAVA_HOME variable that we added it before.
3-) Create an Android Project in Jenkins
In this step, we will create a new project from Dashboard -> New Item. You can think of each item as a project. The enter an item name field will be the name of your project that appears on the dashboard. And select as a freestyle project. We will already touch others in the next articles.
After saying OK and continuing, it will direct us to the project configure screen. Here we will see 5 different tabs. Let’s skip the first tab and click on the Source Code Management tab on the second tab. Here we need to define the git repository of which project we want to build from. We need to copy the repository url from git and paste it here.
After this step, if our project is private, Jenkins will ask us for git user information for access. By the way, Jenkins now asks for personal access token information instead of user password. Here you can easily find how to create a GitHub personel access token. After creating the token, we enter the information, and after adding it, we add the information we created from the credential add button.
Let’s press the add button and return to the previous screen and select the key we added under the credential menu. if everything is ok you won’t get any red error text under repository url field. Finally, we should add the branch name we want to build in this area and press the save button.
Now it’s time to set up the build environment. Click on the Build Tab -> Add build Step -> Invoke Gradle script. The screen below will appear.
There you will see two options as the invoke gradle and the use gradle wrapper.
If we select the invoke gradle option, we need to choose the Gradle version available for our project from the Global Tool configuration menu. as default no Gradle version will come there. firstly, we need to add it from Gradle section under Dashboard-> Manage Jenkins->Global Tool configuration. Find Gradle Section and click add Gradle button. If you have android studio, you can see your available Gradle version as below.
After determining the appropriate Gradle version, add it now to Global Tool Configuration.
After doing this step, you can see the all added options under build -> invoke gradle -> gradle versions as below.
Here we select the appropriate version that we have just added and click on save.
Let’s go through the use gradle wrapper, which is another option. if we do it through this option, we will show the project path and get the appropriate build Gradle locally.
${workspace} represents the Jenkins root of the project. If there is another subfolder in the root of the project created in Jenkins workspace, you have to add it to the end of the ${workspace}. The wrapper location should point to executable gradlew file.
Finally, we will do the build process by adding this code to the task field below.
clean assembleDebug --stacktrace
clean : This command cleans an Android project and deletes the build directory.
assembleDebug : The assemble Debug build task builds the debug version of your app and signs it with the default local certificate
stacktrace : You can get more detailed log outputs via this command.
If we have completed this process without any problems, now let’s get to the build process.
4-) Build Android Project
If we have completed all our operations without any problems, we can now release a version via build. Click on Jenkins Dashboard and select your project.
Click on build now on the left side of the project panel.
You can follow the console logs by clicking the relevant build item -> Console Output. If everything goes well, you will need to see a log output like the one below.
5-) Get Archiving Artifacts (Generate APKs)
After a successful build in Jenkins, it assigns the apk created by default into the root file (.jenkins -> workspace -> project -> app -> build-> outputs -> apk). If you want, we can change this path. Click on add post-build action and select archive the artifacts. Let’s write the path where the generated apks’ will be saved in the relevant field. And click on save button.
If you have completed everything up to this point, you will be able to successfully generate .apk on Jenkins.
In the continuation of the series, we will go one step further and examine the pipeline institutions. Thanks for reading 🙏 and finally don’t forget to clap 👏