hello-android-ndk/README.md
2024-03-22 16:06:58 +01:00

3.1 KiB

This project is a simple demo of how to compile&run native Aarch64 (or x86) binary on a Android device.

TL;DR

.bashrc

export ANDROID_NDK="~/bin/android-ndk/r26c-linux"
export ANDROID_SDK_ROOT="~/bin/android_sdk_root"
export PATH="$PATH:$ANDROID_SDK_ROOT/cmdline-tools/latest/bin:$ANDROID_SDK_ROOT/emulator:$ANDROID_SDK_ROOT/platform-tools"

Compile with Android NDK

  • Download latest version of Android NDK. I'm going go use Linux distribution.
  • Set ANDROID_NDK Environment Variable to the extracted contents of an archive.
  • At this point you should be able to compile the project with compile.sh script.

Run

Technically, if you have Android device with Development Mode ON and adb binary somewhere on your host PC - you shall be able to install and execute compiled artifact from previous step with help of run.sh script. If that is not what you want or have - next sections are for you.

Android Emulator without Android Studio

Directory Structure

$ANDROID_SDK_ROOT/
├── cmdline-tools/
|   └── latest/
|       └── bin/
|           ├── avdmanager
|           ├── sdkmanager
|           ├── ...
|── emulator/
|── platforms/
|   └─ android-23/
|── platform-tools/
|   ├─ adb
|   ├─ fastboot
|   ├─ ...
|── system-images/
|   └─ android-23/

cmdline-tools

  • Download Command line tools only.
  • Set ANDROID_SDK_ROOT Environment Variable to the extracted contents of downloaded archive. It is recommended to follow next convention: $ANDROID_SDK_ROOT/cmdline-tools/latest/bin
  • Add it to PATH.

sdkmanager

  • List available packages with:
sdkmanager --list

[!NOTE] QEMU host limitations It looks that Android API 27 is the latest officially supported OS image that is capable of being executed as Aarch64 Android on x86_64 host. Later versions of Android can be used only as x86_64 binaries.

  • Install packages for Android API 27 (MARSHMELLOW (6.0), etc)
sdkmanager "system-images;android-27;default;arm64-v8a" "platforms;android-27"

emulator

  • Install emulator and tools:
sdkmanager "emulator" "platform-tools"
  • Add tools to PATH: $ANDROID_SDK_ROOT/emulator, $ANDROID_SDK_ROOT/platform-tools

  • Create emulator device:

avdmanager --verbose create avd --force --name "pixel_6.0_27" --device "pixel" --package "system-images;android-27;default;arm64-v8a" --tag "default" --abi "arm64-v8a"
  • Start emulator:
emulator @pixel_6.27  -qemu -machine virt

[!NOTE] -qemu -machine virt This two options are only necessary if you are going to run Aarch64 Android on a x86_64 host.

adb server

If you see access denied error message for adb command, run adb server with sudo:

sudo ~/bin/android_sdk_root/platform-tools/adb start-server

The command will spawn background adb server process, granting necessary access permissions for all subsequent local adb commands.