Skip to content

Build Android Application and Android targeted binary on unsupported OS/arch.

Notifications You must be signed in to change notification settings

zongou/android-build

Repository files navigation

Android Build Enviroment

Build Android Application and Android targetd binary on unsupported os/arch.
For example:

  • android
  • prooted linux distro on android
  • aarch64-linux

Setup enviroment to build android app

Setup SDK

First. Install JDK.

NOTE that on android prooted alpine. jdk > 17 may not work

Then run

./setup_sdk.sh [PREFIX_DIR]

Setup NDK

First. Get NDK official / github and decompress

Then. Run

./setup_ndk.sh [ANDROID_NDK_ROOT]

Get aapt2

aapt2 is needed when building android application.

Example: Compiling termux in prooted alpine

apt update
apt install git -y
git clone https://github.com/zongou/termux-app
cd termux-app
echo "ndk.dir=${ANDROID_NDK_ROOT}" >> local.properties
echo "android.aapt2FromMavenOverride=/usr/local/bin/aapt2 >> local.properties
gradlew assembleRelease

Setup toolchain for compiling C/C++ programs only

First. Get NDK official / github and decompress

Then. Run

./setup_toolchain.sh [ANDROID_NDK_ROOT]

Test

./bin/aarch64-linux-android21-clang tests/hello.c -o hello-c
file hello-c
./bin/aarch64-linux-android21-clang++ tests/hello.cpp -o hello-cpp
file hello-cpp

How does it work?

We can make use of NDK prebuilted sysroot and clang resource dir with host clang toolchain.

TOOLCHAIN="<ANDROID_NDK_ROOT>/toolchains/llvm/prebuilt/linux-x86_64"
RESOURCE_DIR="${TOOLCHAIN}/lib/clang/<LLVM_VERSION>"
SYSROOT="${TOOLCHAIN}/sysroot"
TARGET="aarch64-linux-android21"

clang \
	-resource-dir "${RESOURCE_DIR}" \
	--sysroot="${SYSROOT}" \
	--target="${TARGET}" \
	-xc - \
	-o "hello-c" \
	<<-EOF
		#include <stdio.h>

		int main() {
		  printf("%s\n", "Hello, C!");
		  return 0;
		}
	EOF

About

Build Android Application and Android targeted binary on unsupported OS/arch.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages