-
Notifications
You must be signed in to change notification settings - Fork 77
Android
Version | Compilers | Status | Tests |
---|---|---|---|
13.0 Tiramusi (x64) | Clang 17.0.2 | Compiled | 25/28 |
Android is an open-source mobile operating system developed by Google. Though it is considered to be open-source, but lot of manufacturers (including Google itself) supply proprietary components. Android is based on the modified version of GNU/Linux kernel plus other open-source software. It was initially developed by Android Inc. from 2005, and then by Google releasing the first version in 2008. UI is written in Java, while the core in C/C++. Android was ported to numerous hardware architectures and platforms, including ARM, x86, MIPS.
In general, the development for Android is done using cross-compilation tools and officially supplied Android Studio. SDK is used to develop GUI-based applications with Java and comes with Android Studio, while for native (C/C++) development NDK is used. There are generally two ways to get the required software for Android development:
- Use Android Studio where you can download required versions of SDK, NDK and emulators.
- Use sdkmanager tool separately to do the same from the command line.
The recommended way to manage Android development is using Android Studio. Though it is possible to work from the command line, as well. The following steps describe how to install everything using sdkmanager.
Download and unpack SDK tools only. Then you can use sdkmanager
tool to install NDK:
sdkmanager ndk-bundle
To communicate with device or emulator (upload binaries, execute binaries, etc.) a special tool called Android Debug Bridge, or simply ADB, is used. It comes with platform-tools
package. For emulator you need to install the package emulator
, as well as system images you want to used with the emulator. For example, to be able to launch x86 emulator with Android and Google API version 33, install system image package system-images;android-33;google_apis;x86_64
.
Note: To have root access to the emulator, you must use google_apis
, not the google_apis_playstore
. You may also want to try google_atd
version of an image as it could provide better performance when running the emulator.
Note: All available system images can be retrieved with sdkmanager --list
.
To create an emulator (or Android Virtual Device, AVD), there is a handy tool avdmanager
. For example, to create a Pixel 5 emulator with previously downloaded x86 system image:
avdmanager create avd --abi google_apis/x86_64 --package "system-images;android-33;google_apis;x86_64" --device 28 --name "pixel_5"
To start emulator:
emulator @pixel_5
Note: All available devices can be retrieved with avdmanager list device
.
Note that you need to use emulator
binary from the emulator directory, not from the sdkmanager binary directory. It may also require to install SDK which could be done with sdkmanager "platforms;android-33"
, and then setting the ANDROID_SDK_ROOT
environment variable to downloaded SDK location.
CMake has built-in Android cross-compilation support since version 3.7, however, until version 3.21 it was not synchronized with CMake toolchains provided by NDK. You need to select a proper version of cross-compilation toolchain: either a pre-built one (comes with NDK) or a custom standalone one. See here how to select the proper toolchain based on the target device.
Note: Starting from NDK r19 version, standalone toolchains are considered obsolete. Use either ndk-build or CMake toolchains to compile native code for Android.
In order to properly configure project with CMake for Android, you must pass appropriate parameters to specify target ABI, platform, device, etc. Refer to a detailed description of CMake cross-compiling options for Android. Here is a description of supported architectures, platforms and other parameters you may pass. Please note, that some parameters (in the latter case) are specific only for CMake builds inside Android Studio.
Here is an example of how to configure the library for x86_64 emulator with API level 33 (Tiramisu 13.0):
cmake -DCMAKE_SYSTEM_NAME=Android -DCMAKE_SYSTEM_VERSION=33 -DCMAKE_ANDROID_ARCH_ABI=x86_64 -DCMAKE_ANDROID_NDK=<path_to_andorid_ndk_dir> <path_to_library_sources>
You can switch between GCC and Clang compiler, however, availability depends on the host system. Use -DCMAKE_ANDROID_NDK_TOOLCHAIN_VERSION=clang
to switch to Clang (you may also specify version using values like clang<major>.<minor>
). For GCC you just specify a version in format <major>.<minor>
.
Usually, it is not allowed to run custom native binaries on an Android device: you need root privileges. But for emulator it is possible.
Start adb
utility (it comes with the platform-tools
package) in a root mode: adb root
. Use unroot
parameter to return back to normal user. Once this was done, you can enter a root shell on the device with adb shell
command. You may put binaries somewhere to /data/local/tmp
directory on a device with adb push
command.
- Official web-page of Android.
- Wikipedia page of Android.