Quickly build Android compiling environment under Linux

Background: in the era of mobile Internet, most enterprises have mobile clients, and mobile clients have two factions because of different OS, namely Android and IOS. For Internet technology practitioners, the most direct difference between the two is open source and closed source, which also leads to the overall stability and stability of mobile clients Reliability (or even cost) needs to be compiled separately. This article records how to build Android compiling environment in Linux environment

<!--more-->

introduce

In general, the development of Apple series software requires the use of some proprietary development tools, such as xcode , and this tool must run on Mac OS X devices (of course, you can also try various ways of black apple), so whether for individual developers or enterprise building servers, you need to buy more MAC devices. Usually, I know that the enterprise will use Mac Mini as the building environment of Apple series.

As Android Software, it is Google's open-source mobile operating system, so there is no high demand for the underlying development environment and construction environment. In general, developers use Android Studio To develop Android Software, and built-in command line tools command-line By default, Android software compiling tools and environment are provided. It is worth mentioning that Android Studio is open-source, so the tool also provides support for multiple platforms (Windows,Mac,Linux,Chrome OS). This also greatly reduces the overall cost of the enterprise. Generally, the development environment can be developed in any OS environment, and the enterprise can also use Linux environment for the continuous integration and delivery of Android Software

Next, I will focus on how to build Android compiling environment in Linux environment.

The construction of Android compiling environment under Linux

Prerequisite

It should be noted that for Android applications, the management of some dependency packages mainly relies on the command-line tool sdkmanager, which can be found in the Android Studio Page found, and support Windows,Mac,Linux versions of three different platforms.

At the same time, Android Studio, the development tool for Android applications, uses [Gradle](https://github.com/gradle/gradle) for compilation and packaging. Therefore, for Android applications, gradle will also be used for compilation and packaging. The software can be used in Gradle Page found.

Secondly, [NDK (native development kit)] (https://developer.android.google.cn/ndk/guides/) is a set of tools, which enables developers to use C and C + + code in Android applications, and provides many platform libraries, which can be used by developers to manage native activities and access physical device components, such as sensors and tap input. The development suite can be NDK Page found

Finally, the underlying languages of the above components are developed in java, so the JDK related environment needs to be installed.

To sum up, the following components are required to compile Android environment in Linux Environment:

  • JDK : the basic compiling and running environment of java language
  • sdkmanager : dependency package manager under Android Application
  • NDK : Android native development suite, which can call the underlying C and C + + code
  • Gradle : Android software compilation tools (tools like maven)

Quick install base environment

# Download basic package
$ mkdir -p /opt/servers/ && cd /opt/servers/
# JDK (openjdk can be selected)
$ wget http://dl.bgbiao.top/dav/jdk1.8.0_191.tar.gz
$ tar -zxf jdk1.8.0_191.tar.gz -C /opt/servers/


# Download and configure sdkmanager
$ wget https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip
$ unzip sdk-tools-linux-4333796.zip 
$ mkdir -p /opt/sdk
$ ln -s /opt/servers/tools /opt/sdk/tools

# Configure environment variables
$ cat /etc/profile
export JAVA_HOME=/opt/servers/jdk1.8.0_191
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CLASSPATH
export PATH=${JAVA_HOME}/bin:${PATH}
export ANDROID_HOME=/opt/sdk
export PATH=${ANDROID_HOME}/tools/bin:${ANDROID_HOME}/tools/bin:${ANDROID_HOME}:${PATH}

# Test the use of sdkmanager
$ source /etc/profile
$ sdkmanager --list
Warning: File /root/.android/repositories.cfg could not be loaded.
Installed packages:
  Path    | Version | Description              | Location
  ------- | ------- | -------                  | -------
  tools   | 26.0.1  | Android SDK Tools 26.0.1 | tools/


# Install the specified version of the package
$ sdkmanager --list | grep cmake
Warning: File /root/.android/repositories.cfg could not be loaded.
  cmake;3.10.2.4988404              | 3.10.2       | CMake 3.10.2.4988404
  cmake;3.6.4111459                 | 3.6.4111459  | CMake 3.6.4111459

$ sdkmanager 'cmake;3.6.4111459' 
....

# Download and install ndk
$ cd /opt/servers && wget https://dl.google.com/android/repository/android-ndk-r16b-linux-x86_64.zip
$ unzip android-ndk-r16b-linux-x86_64.zip
$ ln -s /opt/servers/android-ndk-r16b /opt/ndk

# Configure ndk configuration environment (add the following configuration)
$ cat /etc/profile
export NDK_HOME=/opt/ndk
export ANDROID_NDK_HOME=/opt/ndk
export PATH=$NDK_HOME:${ANDROID_NDK_HOME}:$PATH


# Download and install gradle
$ cd /opt/servers && wget https://services.gradle.org/distributions/gradle-4.10.1-all.zip
$ unzip gradle-4.10.1-all.zip
$ ln -s /opt/servers/gradle-4.10.1 /opt/gradle

# Configure the gradle environment (add the following configuration)
$ cat /etc/profile
export GRADLE_HOME=/opt/gradle
export PATH=${GRADLE_HOME}/bin:${PATH}

# Test gradle
$ gradle -v

------------------------------------------------------------
Gradle 4.10.1
------------------------------------------------------------

Build time:   2018-09-12 11:33:27 UTC
Revision:     76c9179ea9bddc32810f9125ad97c3315c544919

Kotlin DSL:   1.0-rc-6
Kotlin:       1.2.61
Groovy:       2.4.15
Ant:          Apache Ant(TM) version 1.9.11 compiled on March 23 2018
JVM:          1.8.0_191 (Oracle Corporation 25.191-b12)
OS:           Linux 3.10.0-862.el7.x86_64 amd64


Install Android basic dependency

Note: before the official compilation, you should make the license and move the licenses directory to / opt/sdk /, which is level with the tools directory of sdkmanager

# Install android basic dependency package
$ sdkmanager 'build-tools;28.0.3' 'platforms;android-28' 'cmake;3.6.4111459'

# Generate licenses
$ sdkmanager --licenses
$ cp -rp licenses /opt/sdk/

Compile Android package

# Load the overall environment variable
$ cat /etc/profile
....
....
export JAVA_HOME=/opt/servers/jdk1.8.0_191
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CLASSPATH
export PATH=${JAVA_HOME}/bin:${PATH}
export ANDROID_HOME=/opt/sdk
export PATH=${ANDROID_HOME}/tools/bin:${ANDROID_HOME}/tools/bin:${ANDROID_HOME}:${PATH}
export GRADLE_HOME=/opt/gradle
export PATH=${GRADLE_HOME}/bin:${PATH}
export NDK_HOME=/opt/ndk
export ANDROID_NDK_HOME=/opt/ndk
export PATH=$NDK_HOME:${ANDROID_NDK_HOME}:$PATH

$ source /etc/profile

# Download the source file of Android Application
$ git clone http://git.bgbiao.top/test-app.git

$ cd test-app
$ export ENV="fNormal"
$ gradle clean assemble${ENV}Release
Starting a Gradle Daemon, 2 busy Daemons could not be reused, use --status for details
Parallel execution with configuration on demand is an incubating feature.
<-------------> 0% CONFIGURING [29s]
> root project > Resolve dependencies of :classpath > guava-18.0.pom > 3 KB/5 KB downloaded
> IDLE
> root project > Resolve dependencies of :classpath > commons-lang-2.6.pom > 9 KB/17 KB downloaded
> root project > Resolve dependencies of :classpath > osdetector-gradle-plugin-1.4.0.pom
....
....

BUILD SUCCESSFUL in 7m 7s
501 actionable tasks: 164 executed, 288 from cache, 49 up-to-date


# View generated apk packages
$ ls app/build/outputs/apk/fNormal/release/
app-fNormal-release.apk  output.json


# Install QR code generator
$ yum install qrencode-3.4.1-3.el7.x86_64 -y

# Upload the generated apk package to the specified http service
$ curl -T app/build/outputs/apk/fNormal/release/app-fNormal-release.apk http://dl.bgbiao.top/dav/

# Generate a QR code for apk download file
$ qrencode -o test-android.png "http://dl.bgbiao.top/dav/app-fNormal-release.apk"

# Upload QR code
$ curl -T test-android.png http://dl.bgbiao.top/dav/

Next, the developer user can scan and install the QR code address http://dl.bgbiao.top/dav/test-android.png, and test and verify the app function of this version.

It should be noted that in general, if developers use Mac OSX to write code, they may use code like "include 'MD5.h" in the code. It seems that there is no problem, but because the Mac OSX or Windows system is not case sensitive, so the case will not have much impact, because the compiler can find MD5. H in the system, but In the Linux environment, the system is very sensitive to case. If MD5. H is written in the code and MD5. H is written in the system library, it will definitely fail to compile, and ordinary people will not think of the case problem when they see the exception.

OK, the trip is finished. Next, I provide a benefit. Based on the above construction history environment, I packaged a docker image to encapsulate the basic environment of Android compilation

docker mirror image

$ cat Dockerfile
FROM centos:7.5.1804
MAINTAINER "BGBiao <https://bgbiao.top/>"
ENV TZ "Asia/Shanghai"

RUN yum clean all && \
    yum install unzip wget curl -y && \
    mkdir -p /opt/{servers,app} && \
    cd /opt/servers/ && \
    wget  http://dl.bgbiao.top/hadoop/jdk1.8.0_191.tar.gz && \
    wget http://dl.bgbiao.top/dav/android-build/android-ndk-r16b-linux-x86_64.zip && \
    wget http://dl.bgbiao.top/dav/android-build/gradle-4.10.1-all.zip && \
    wget http://dl.bgbiao.top/dav/android-build/sdk-tools-linux-4333796.zip

RUN pushd /opt/servers && \
    tar -zxf jdk1.8.0_191.tar.gz && \
    unzip android-ndk-r16b-linux-x86_64.zip && \
    unzip gradle-4.10.1-all.zip && \
    unzip sdk-tools-linux-4333796.zip && \
    mkdir -p /opt/sdkmanager && \
    ln -s /opt/servers/tools /opt/sdkmanager/tools && \
    ln -s /opt/servers/gradle-4.10.1 /opt/gradle && \
    ln -s /opt/servers/android-ndk-r16b /opt/ndk

COPY profile /opt/servers/setenv.sh
$ cat profile
export JAVA_HOME=/opt/servers/jdk1.8.0_191
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CLASSPATH
export PATH=${JAVA_HOME}/bin:${PATH}
export ANDROID_HOME=/opt/sdkmanager
export PATH=${ANDROID_HOME}/tools/bin:${ANDROID_HOME}/tools/bin:${ANDROID_HOME}:${PATH}
export GRADLE_HOME=/opt/gradle
export PATH=${GRADLE_HOME}/bin:${PATH}
export NDK_HOME=/opt/ndk
export ANDROID_NDK_HOME=/opt/ndk
export PATH=$NDK_HOME:${ANDROID_NDK_HOME}:$PATH

# Users can build an image according to the Dockerfile mentioned above, and can also directly use an image built by me
$ docker pull xxbandy123/android-build-env:19-12-12

# Usage mode
# Based on the above image, users need to use sdkmanager to install the dependent Android library, and arrange their own gradle packaging commands, which can be compiled directly in their own Android projects
$ docker run -itd --name android-build-env:19-12-12 bash 
[root@4c05d4ded28d /]# source /opt/servers/setenv.sh
[root@4c05d4ded28d /]# git clone your-android-app.git
[root@4c05d4ded28d /]# sdkmanager 'build-tools;28.0.3' 'platforms;android-28' 'cmake;3.6.4111459'
[root@4c05d4ded28d /]# sdkmanager --licenses
[root@4c05d4ded28d /]# cp -rp licenses ${ANDROID_HOME}/
[root@4c05d4ded28d /]# ls ${ANDROID_HOME}
build-tools  cmake  licenses  platforms  platform-tools  tools
# Start compiling (long wait)
[root@4c05d4ded28d /]# cd your-android-app && gradle clean assembleRelease

Matters needing attention

  • 1. Generally, continuous integration uses Jenkins for compilation and packaging, so the above environment needs to install git client name before accessing Jenkins salve
  • 2. In general, the client will download the package through QR code scanning when testing the package. Therefore, qrencode software needs to be installed on the environment to generate QR code
  • 3. It should be noted that gradle will start a daemons during compilation. If the compilation ends abnormally, the daemons may not be released immediately. At this time, compiling again immediately may lead to OOM wx company number: bgbio, progress together~

Keywords: Operation & Maintenance Android Gradle Linux SDK

Added by pelegk2 on Thu, 12 Dec 2019 22:23:43 +0200