initial commit

This commit is contained in:
andriy 2024-03-19 14:06:40 +01:00
commit 5cc4decbcf
6 changed files with 128 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
build/
.obsidian/

86
README.md Normal file
View File

@ -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.

10
compile.sh Executable file
View File

@ -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/

16
run.sh Executable file
View File

@ -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

7
src/CMakeLists.txt Normal file
View File

@ -0,0 +1,7 @@
project(hello-android-cli)
cmake_minimum_required(VERSION 3.16.0)
add_executable(${PROJECT_NAME}
main.cpp
)

7
src/main.cpp Normal file
View File

@ -0,0 +1,7 @@
#include <iostream>
int main(int argc, char** argv)
{
std::cout << "Hello Android CLI" << std::endl;
return 0;
}