[LINUX] Creating an environment that automatically builds with Github Actions (Android)

Overview

We verified CI / CD on Github Actions. Among them, I created a template that can be applied to other development projects on a regular basis. The project of the created template is as follows. github-actions-examples

This time, I will describe it here as a memorandum for building such an environment. Also, the above project has been tried in various other environments, and this time I will focus on the method of automatic build with ʻAndroid application` in Github Actions. I will introduce it to.

Please refer to this article for automatic deployment on the website and basic settings in Github Actions. Creating an environment for automatic deployment with Github Actions (Website)

In addition to this, we are currently verifying the environment construction of automatic build with ʻiOS and ʻUnity. I would like to introduce this as soon as the method is established.

Thing you want to do

Build

Conclusion

Please refer to the following contents respectively

Various explanations

JDK setup

Build an Android application as a Java application. This time I will build using JDK to build (even if I am developing with Kotlin) In Github Actions, use action / setup-java to set up JDK. I can. Also, this time we will build with Java version 1.8, so specify the version to use. Describe as follows in the yml file.

jobs:
  build:
    steps:
      ...
      - name: setup JDK 1.8
        uses: actions/setup-java@v1
        with:
          java-version: 1.8

Android SDK setup

Once you have set up the JDK, the next step is to download and set up the ʻAndroid SDK. ʻAndroid SDK is generally downloaded by Android Studio, but this time it is executed by CLI, so download it by a method that does not use Android Studio. Download the zip file that contains the ʻAndroid SDK in the Command line tools onlysection of [Android Studio](https://developer.android.com/studio). Thiszip file depends on the version of ʻAndroid SDK Tools. Enter the URL that specifies the version you want to download and use. The following process is the process of downloading and unzipping the zip file.

wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/sdk-tools-linux-${{ matrix.sdk-tools }}.zip
unzip -d android-sdk-linux android-sdk.zip

For matrix.sdk-tools, specify the value of the version of ʻAndroid SDKabove. In this example, we will use4333796`.

ʻWhen the Android SDK is downloaded and decompressed, pass the settings and environment variables PATHfor usingbuild-tools`` platform-tools`.

echo y | android-sdk-linux/tools/bin/sdkmanager "platforms;android-${{ matrix.compile-sdk }}" >/dev/null
echo y | android-sdk-linux/tools/bin/sdkmanager "platform-tools" >/dev/null
echo y | android-sdk-linux/tools/bin/sdkmanager "build-tools;${{ matrix.build-tools }}" >/dev/null
export ANDROID_HOME=$PWD/android-sdk-linux
export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/

Then accept lincense to use ʻAndroid SDK`.

set +o pipefail
yes | android-sdk-linux/tools/bin/sdkmanager --licenses
set -o pipefail

Now that you have set up the ʻAndroid SDK`, you can build your Android app.

gradle setup

Use Gradle to build the app on Android. The Gradle settings are set up automatically by running gradlew. (Various Gradle commands are also executed using gradlew), updating permissions so that the gradlew file can be executed.

chmod +x ./gradlew

(Bonus) Warning resolved

It is set to build up to the above, but if you build as it is, Warning will appear, so create the following file to prevent Warning.

sudo touch /root/.android/repositories.cfg

Debug Build

When the environment for building the Android application is ready, first perform Debug Build. Debug Build can be built by executing the following command.

./gradlew assembleDebug

A signature is required to build an Android app, but in Debug Build which does not specify a signature key, it is generated in ~ / .android / debug.keystore when ʻAndroid SDKis installed. The build will be done using thedebug keystore`.

【reference】 I checked the keystore for debug

Release Build

In the case of Debug Build, the build was done using Android's debug keystore, but when distributing the built Android application to Google Play Store etc., it is distributed by the application built with the default signing key. can not do. In order to distribute, you can build the application to be distributed to the Store by creating the signature key yourself and building with the created signature key (here, such a build is called Release Build". Masu) In the following, I will show you how to do Release Build on Github Actions. For creating the key, refer to here.

Make it possible to refer to the key used for the build

In order to build by specifying the created signature key, it is applied when building by specifying the signature key file in ʻapp / bulk.gradle`.

At this time, the information of storeFile`` storePassword keyAlias`` keyPassword is required, so specify this information in ʻapp / buld.gradle. However, since this information is an environment variable that you want to keep secret and manage, describe the above information in local.properties that describes the environment variable on Android, and put the contents in ʻapp / build.gradle. It will be referenced when building by loading it. (local.properties is listed in .gitignore by default)

The description in ʻapp / buld.gradle` is as follows.

build.gradle


Properties properties = new Properties()
def localPropertiesFile = project.rootProject.file('local.properties');
if(!localPropertiesFile.exists()){
    //If there is no localPropertiesFile, create it
    localPropertiesFile.createNewFile();
}
properties.load(localPropertiesFile.newDataInputStream())

In the above example, if local.properties does not exist at build time, create a new one, and if there is, load the existing file to apply it to the settings. (If you do not do this, you will get a build error if local.properties does not exist (for example, when you do Debug Build))

To apply the read value of local.properties to the build when Release Build, write as follows.

build.gradle


android {
    ...
    signingConfigs {
        release {
            //Describe only when the specified file exists
            if(!properties.getProperty("RELEASE_STORE_FILE", "").empty){
                storeFile file(properties.getProperty("RELEASE_STORE_FILE", ""))
            }
            storePassword properties.getProperty("RELEASE_STORE_PASSWORD", "")
            keyAlias properties.getProperty("RELEASE_KEY_ALIAS", "")
            keyPassword properties.getProperty("RELEASE_KEY_PASSWORD", "")
        }
    }
    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }
}

At this time, set the following environment variables in local.properties.

local.properties


RELEASE_STORE_FILE=File path of signing key file(Absolute path)String
RELEASE_STORE_PASSWORD=Store password string
RELEASE_KEY_ALIAS=Key alias string
RELEASE_KEY_PASSWORD=Key password string

Key management

Normally, the signing key file is a binary file, so you need to save it somewhere and get it or embed it in your project. However, there is a problem that the signing key file is known to many developers.

So this time, save the signature key management in Github Secrets and apply it as a secret environment variable </ font>

The file size of the signing key is trivial, so use base64 to string the binary file. Save this string in Github Secrets, and when you execute Github Actions, save it back to a binary file to keep the key secret. It will be possible. First, output the signature key file to a character string with base64.

base64 Android build signing key.keystore

The character string output by executing this command is registered in Github Secrets.

In Github Actions, the information of Github Secrets set here is applied, so write it to a binary file withbase64 -d>as follows. I will.

echo ${ANDROID_REALSE_BASE64_KEY} | base64 -d > ./release-application-key
  • Here, ʻANDROID_REALSE_BASE64_KEYis set toGithub Secrets, which is a binary file converted to a character string with base64. release-application-key` is the filename of the signing key to temporarily store.

Describe the information required to perform the above various Release Builds in local.properties.

When performing Github Actions, describe the following information in local.properties with the information required to perform Release Build.

local.properties


RELEASE_STORE_FILE=File path of signing key file(Absolute path)String
RELEASE_STORE_PASSWORD=Store password string
RELEASE_KEY_ALIAS=Key alias string
RELEASE_KEY_PASSWORD=Key password string

All of this information should be set in Github Secrets, and the settings should be added to local.properties as shown below.

echo ${ANDROID_REALSE_BASE64_KEY} | base64 -d > ./release-application-key
echo "RELEASE_STORE_FILE=`pwd`/release-application-key" >> ./local.properties
echo "RELEASE_STORE_PASSWORD=${ANDROID_REALSE_KEY_PASSWORD}" >> ./local.properties
echo "RELEASE_KEY_ALIAS=key0" >> ./local.properties
echo "RELEASE_KEY_PASSWORD=${ANDROID_REALSE_KEY_PASSWORD}" >> ./local.properties

This will cause the build to be done with the value applied to local.properties when run on Github Actions.

Do Release Build in Gradle

By executing the following command, [apk file](https://ja.wikipedia.org/wiki/APK_(%E3%83%95%E3%82%A1%E3%82%A4%E3%83% Perform Release Build to output AB% E5% BD% A2% E5% BC% 8F))

./gradlew assembleRelease

Next, execute the following command to perform Release Build that outputs aab file.

./gradlew bundleRelease

Make the built artifacts available for download

[Apk file](https://ja.wikipedia.org/wiki/APK_(%E3%83%95%E3%82%A1%E3) after the build is completed for both Debug Build and Release Build % 82% A4% E3% 83% AB% E5% BD% A2% E5% BC% 8F))), aab file Add the following items so that you can download various artifacts such as

- uses: actions/upload-artifact@master
  with:
    name: outputs
    path: app/build/outputs/

This will allow you to download the artifacts as follows:

release-build-artifact.png

[apk file](https://ja.wikipedia.org/wiki/APK_(%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E5%BD%A2%] E5% BC% 8F)) deliverables are ʻoutputs / apk / release / app-release.apk, [aab file](https://developer.android.com/guide/app-bundle?hl=ja) The artifacts of are in ʻoutputs / bundle / release / app-release.aab.

Summary

That's all for Github Actions to explain and show you how to automatically build native Android apps. If you want to introduce it to the project as soon as possible, please refer to (copy) each of the following contents and introduce it.

Future outlook

  • How to release to Google Play Store
  • How to do Android NDK Build
  • How to run Android tests

I would like to add.

Recommended Posts

Creating an environment that automatically builds with Github Actions (Android)
Note when creating an environment with python
Build a data analysis environment with Kedro + MLflow + Github Actions
Create an environment with virtualenv
Creating an egg with python
Tested pipenv with GitHub Actions
When creating an environment that uses python django on Ubuntu 12.04 LTS
Studying Python Part.1 Creating an environment
A memo when creating an environment that can be debugged with Lambda @ Edge for the time being
[Python] Building an environment with Anaconda [Mac]
Creating an Ubuntu 18.04 + MAAS2.4 environment starting from 0
Creating an unknown Pokemon with StyleGAN2 [Part 1]
Automatically switch virtual environment with conda + direnv
[Golang] Create docker image with Github Actions
Creating a virtual environment in an Anaconda environment
Creating an environment for OSS-DB Silver # 1_Create a Linux environment (CentOS7 virtual environment) with VirtualBox/Vagrant
[kotlin] Create an app that recognizes photos taken with a camera on android
Build a data analysis environment that links GitHub authentication and Django with JupyterHub