From 5cc4decbcf72dd3728c803a2dd515b62a15a7006 Mon Sep 17 00:00:00 2001 From: andriy Date: Tue, 19 Mar 2024 14:06:40 +0100 Subject: [PATCH] initial commit --- .gitignore | 2 ++ README.md | 86 ++++++++++++++++++++++++++++++++++++++++++++++ compile.sh | 10 ++++++ run.sh | 16 +++++++++ src/CMakeLists.txt | 7 ++++ src/main.cpp | 7 ++++ 6 files changed, 128 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 compile.sh create mode 100755 run.sh create mode 100644 src/CMakeLists.txt create mode 100644 src/main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5e69491 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/ +.obsidian/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..d650487 --- /dev/null +++ b/README.md @@ -0,0 +1,86 @@ +This project is a simple demo of how to compile&run native **Aarch64** (or **x86**) binary on a Android device. + +# TL;DR + +*.bashrc* +```bash +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](https://developer.android.com/ndk/downloads) 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](https://developer.android.com/studio#command-tools) 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: +```bash +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) +```bash +sdkmanager "system-images;android-27;default;arm64-v8a" "platforms;android-27" +``` + +## emulator + +- Install emulator and tools: +```bash +sdkmanager "emulator" "platform-tools" +``` + +- Add tools to `PATH`: `$ANDROID_SDK_ROOT/emulator`, `$ANDROID_SDK_ROOT/platform-tools` + +- Create emulator device: +```bash +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: +```bash +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. diff --git a/compile.sh b/compile.sh new file mode 100755 index 0000000..a1c1896 --- /dev/null +++ b/compile.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +set -euxo pipefail + +cmake \ + -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \ + -DANDROID_ABI=arm64-v8a \ + -Bbuild \ + -Ssrc + +cmake --build build/ diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..5a4aa25 --- /dev/null +++ b/run.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +set -euxo pipefail + +# +# Create emulator +# +# avdmanager --verbose create avd --force --name "pixel_6.0" --device "pixel" --package "system-images;android-23;google_apis;arm64-v8a" --tag "google_apis" --abi "arm64-v8a" + + +# +# Run emulator +# +# emulator @pixel_6.0 + +adb push build/hello-android-cli /data/local/tmp +adb shell /data/local/tmp/hello-android-cli diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..b270e5e --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,7 @@ +project(hello-android-cli) + +cmake_minimum_required(VERSION 3.16.0) + +add_executable(${PROJECT_NAME} + main.cpp + ) diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..8fc9676 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,7 @@ +#include + +int main(int argc, char** argv) +{ + std::cout << "Hello Android CLI" << std::endl; + return 0; +}