Building an Android Compilation Environment in CentOS 7

In addition to Android Studio, you can also use Gradle Script to compile Android projects and build and test applications.
This article is a memo to record my process of tossing around with the Android compilation environment under the x86_64 GNU/Linux system CentOS Linux release 7.8.2003.

This article will install the following components:

Git
JDK 1.8
Go 1.14.7
Android SDK
  Android NDK
Rust 1.45.0
  Android Targets:
    armv7-linux-androideabi
    aarch64-linux-android
    i686-linux-android
    x86_64-linux-android

All the following procedures are performed under the root user.

1. Install Git and JDK 1.8

These two components come with the distribution and can be installed through the following commands. At the same time, install some other necessary components wget and unzip.

$ yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel wget unzip git

After completion, verify the JDK version number through the following command.

$ java -version

return

openjdk version "1.8.0_252"
OpenJDK Runtime Environment (build 1.8.0_252-b09)
OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)
$ javac -version

return

javac 1.8.0_252

2. Install Go

The latest version of Go is currently 1.14.7. The installation process is as follows.

$ GO_VERSION=1.14.7
$ wget -O /tmp/go${GO_VERSION}.tar.gz https://golang.org/dl/go${GO_VERSION}.linux-amd64.tar.gz
$ tar -C /usr/local -xzf /tmp/go${GO_VERSION}.tar.gz
$ rm -fv /tmp/go${GO_VERSION}.tar.gz
$ export PATH=/usr/local/go/bin:$PATH

After completion, verify the Go version number through the following command.

$ go version

return

go version go1.14.7 linux/amd64

3. Install Android SDK and Android NDK

hypothesis ANDROID_HOME The path is /opt/sdk. The installation process is as follows.

$ mkdir -p /opt/sdk && cd /opt/sdk
$ wget https://dl.google.com/android/repository/commandlinetools-linux-6609375_latest.zip
$ unzip -q commandlinetools-linux-6609375_latest.zip
$ rm -fv commandlinetools-linux-6609375_latest.zip
$ export PATH=/opt/sdk/tools/bin:$PATH
$ ( -z "${ANDROID_HOME}" ) && export ANDROID_HOME=/opt/sdk
$ yes | sdkmanager --sdk_root=${ANDROID_HOME} --licenses

pass sdkmanager to install some components. One command completes the task in one step. as follows.

$ sdkmanager --sdk_root=${ANDROID_HOME} "platform-tools" "platforms;android-30" "platforms;android-29" "ndk-bundle" "ndk;21.0.6113669" "build-tools;29.0.2"

Once completed, confirm the installed list.

$ sdkmanager --sdk_root=${ANDROID_HOME} --list

return

Installed packages:
  Path                 | Version      | Description                     | Location             
  -------              | -------      | -------                         | -------              
  build-tools;29.0.2   | 29.0.2       | Android SDK Build-Tools 29.0.2  | build-tools/29.0.2/  
  ndk-bundle           | 21.3.6528147 | NDK                             | ndk-bundle/          
  ndk;21.0.6113669     | 21.0.6113669 | NDK (Side by side) 21.0.6113669 | ndk/21.0.6113669/    
  patcher;v4           | 1            | SDK Patch Applier v4            | patcher/v4/          
  platform-tools       | 30.0.3       | Android SDK Platform-Tools      | platform-tools/      
  platforms;android-29 | 4            | Android SDK Platform 29         | platforms/android-29/
  platforms;android-30 | 1            | Android SDK Platform 30         | platforms/android-30/
  tools                | 2.1.0        | Android SDK Tools 2.1           | tools/               
Available Packages:
  以下省略

4. Install Rust and others

The latest version of Rust is currently 1.45.0. The installation process is as follows.

$ export RUSTUP_HOME=/usr/local/rustup CARGO_HOME=/usr/local/cargo
$ RUST_VERSION=1.45.0
$ RUSTARCH='x86_64-unknown-linux-gnu'
$ wget -O /tmp/rustup-init "https://static.rust-lang.org/rustup/archive/1.21.1/${RUSTARCH}/rustup-init"
$ chmod 755 /tmp/rustup-init
$ /tmp/rustup-init -y --no-modify-path --profile minimal --default-toolchain $RUST_VERSION
$ rm -fv /tmp/rustup-init
$ chmod -R a+w ${RUSTUP_HOME} ${CARGO_HOME}
$ export PATH=/usr/local/cargo/bin:$PATH

After completion, use the following command to verify the version numbers of Rust and components.

$ rustup --version

return

rustup 1.21.1 (7832b2ebe 2019-12-20)
$ cargo --version

return

cargo 1.45.0 (744bd1fbb 2020-06-15)
$ rustc --version

return

rustc 1.45.0 (5c1f21c3b 2020-07-13)

Rust installs the following Android Targets:

armv7-linux-androideabi
aarch64-linux-android
i686-linux-android
x86_64-linux-android

The installation process is as follows.

$ rustup install stable
$ rustup default stable
$ rustup target add armv7-linux-androideabi
$ rustup target add i686-linux-android
$ rustup target add aarch64-linux-android
$ rustup target add x86_64-linux-android

5. Persistent environment variables

The above installation steps define the system PATH through export. The settings will be lost after logging out of the current login.
Therefore, some environment variables need to be persisted.
edit ~/.bash_profile document.Add to ANDROID_HOMERUSTUP_HOMECARGO_HOME Definition and addition of PATH definition.
The final presentation content is as follows.

# .bash_profile

# Get the aliases and functions
if ( -f ~/.bashrc ); then
. ~/.bashrc
fi

# User specific environment and startup programs

export ANDROID_HOME=/opt/sdk RUSTUP_HOME=/usr/local/rustup CARGO_HOME=/usr/local/cargo

PATH=$PATH:$HOME/bin:$ANDROID_HOME/tools/bin:/usr/local/go/bin:/usr/local/cargo/bin

export PATH

6. Compile Android project

Here is a relatively simple project to practice on:v2ray-plugin-android
The compilation process is as follows.

$ git clone https://github.com/shadowsocks/v2ray-plugin-android.git
$ cd v2ray-plugin-android
$ git submodule update --init --recursive
$ ./gradlew assembleRelease

After completion, the compiled apk file is located in app/build/outputs/apk/release under the path.
The Android system requires that all apks be digitally signed with a certificate before they can be installed on a device or updated.
Therefore, these apk files cannot be installed directly.
As for how to sign, please refer to the following link and the process is omitted.
https://developer.android.com/studio/publish/app-signing

7. Summary

When you get used to using the command line to do things, you will find that your efficiency is often greatly improved. The same goes for compiled code.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button