|
| 1 | +# Testing Libraries on Android |
| 2 | + |
| 3 | +The following dependencies should be installed in order to be able to run tests: |
| 4 | + |
| 5 | +- Android NDK |
| 6 | +- Android SDK |
| 7 | +- OpenJDK |
| 8 | +- OpenSSL |
| 9 | + |
| 10 | +OpenJDK can be installed on Linux (Ubuntu) using `apt-get`: |
| 11 | +```bash |
| 12 | +sudo apt-get install openjdk-8 unzip |
| 13 | +``` |
| 14 | + |
| 15 | +Android SDK, NDK and OpenSSL can be automatically installed via the following script: |
| 16 | +```bash |
| 17 | +#!/usr/bin/env bash |
| 18 | +set -e |
| 19 | + |
| 20 | +NDK_VER=r21b |
| 21 | +SDK_VER=6200805_latest |
| 22 | +SDK_API_LEVEL=29 |
| 23 | +SDK_BUILD_TOOLS=29.0.3 |
| 24 | +OPENSSL_VER=1.1.1g-alpha-1 |
| 25 | + |
| 26 | +if [[ "$OSTYPE" == "darwin"* ]]; then |
| 27 | + HOST_OS=darwin |
| 28 | + HOST_OS_SHORT=mac |
| 29 | + BASHRC=~/.zprofile |
| 30 | +else |
| 31 | + HOST_OS=linux |
| 32 | + HOST_OS_SHORT=linux |
| 33 | + BASHRC=~/.bashrc |
| 34 | +fi |
| 35 | + |
| 36 | +# download Android NDK |
| 37 | +export ANDROID_NDK_ROOT=~/android-ndk-${NDK_VER} |
| 38 | +curl https://dl.google.com/android/repository/android-ndk-${NDK_VER}-${HOST_OS}-x86_64.zip -L --output ~/andk.zip |
| 39 | +unzip ~/andk.zip -o -d $(dirname ${ANDROID_NDK_ROOT}) && rm -rf ~/andk.zip |
| 40 | + |
| 41 | +# download Android SDK, accept licenses and download additional packages such as |
| 42 | +# platform-tools, platforms and build-tools |
| 43 | +export ANDROID_SDK_ROOT=~/android-sdk |
| 44 | +curl https://dl.google.com/android/repository/commandlinetools-${HOST_OS_SHORT}-${SDK_VER}.zip -L --output ~/asdk.zip |
| 45 | +unzip ~/asdk.zip -o -d ${ANDROID_SDK_ROOT} && rm -rf ~/asdk.zip |
| 46 | +yes | ${ANDROID_SDK_ROOT}/tools/bin/./sdkmanager --sdk_root=${ANDROID_SDK_ROOT} --licenses |
| 47 | +${ANDROID_SDK_ROOT}/tools/bin/./sdkmanager --sdk_root=${ANDROID_SDK_ROOT} "platform-tools" "platforms;android-${SDK_API_LEVEL}" "build-tools;${SDK_BUILD_TOOLS}" |
| 48 | + |
| 49 | +# We also need to download precompiled binaries and headers for OpenSSL from maven, this step is a temporary hack |
| 50 | +# and will be removed once we figure out how to integrate OpenSSL properly as a dependency |
| 51 | +export ANDROID_OPENSSL_AAR=~/openssl-android |
| 52 | +curl https://maven.google.com/com/android/ndk/thirdparty/openssl/${OPENSSL_VER}/openssl-${OPENSSL_VER}.aar -L --output ~/openssl.zip |
| 53 | +unzip ~/openssl.zip -o -d ${ANDROID_OPENSSL_AAR} && rm -rf ~/openssl.zip |
| 54 | +printf "\n\nexport ANDROID_NDK_ROOT=${ANDROID_NDK_ROOT}\nexport ANDROID_SDK_ROOT=${ANDROID_SDK_ROOT}\nexport ANDROID_OPENSSL_AAR=${ANDROID_OPENSSL_AAR}\n" >> ${BASHRC} |
| 55 | +``` |
| 56 | +Make sure `ANDROID_NDK_ROOT`, `ANDROID_SDK_ROOT` and `ANDROID_OPENSSL_AAR` environment variables are accessible and point to correct locations. |
| 57 | + |
| 58 | +## Building Libs and Tests for Android |
| 59 | + |
| 60 | +Now we're ready to build everything for Android: |
| 61 | +``` |
| 62 | +./build.sh mono+libs -os Android -arch x64 |
| 63 | +``` |
| 64 | +and even run tests one by one for each library: |
| 65 | +``` |
| 66 | +./build.sh libs.tests -os Android -arch x64 -test |
| 67 | +``` |
| 68 | +Make sure an emulator is booted (see `AVD Manager`) or a device is plugged in and unlocked. |
| 69 | +`AVD Manager` tool recommends to install `x86` images by default so if you follow that recommendation make sure `-arch x86` was used for the build script. |
| 70 | + |
| 71 | +### Running individual test suites |
| 72 | +The following shows how to run tests for a specific library |
| 73 | +``` |
| 74 | +./dotnet.sh build /t:Test src/libraries/System.Numerics.Vectors/tests /p:TargetOS=Android /p:TargetArchitecture=x64 |
| 75 | +``` |
| 76 | + |
| 77 | +### Test App Design |
| 78 | +Android app is basically a [Java Instrumentation](https://github.com/dotnet/runtime/blob/master/src/mono/msbuild/AndroidAppBuilder/Templates/MonoRunner.java) and a simple Activity that inits the Mono Runtime via JNI. This Mono Runtime starts a simple xunit test |
| 79 | +runner called XHarness.TestRunner (see https://github.com/dotnet/xharness) which runs tests for all `*.Tests.dll` libs in the bundle. There is also XHarness.CLI tool with ADB embedded to deploy `*.apk` to a target (device or emulator) and obtain logs once tests are completed. |
| 80 | + |
| 81 | +### Obtaining the logs |
| 82 | +XHarness for Android doesn't talk much and only saves test results to a file. However, you can also subscribe to live logs via the following command: |
| 83 | +``` |
| 84 | +adb logcat -s "DOTNET" |
| 85 | +``` |
| 86 | +Or simply open `logcat` window in Android Studio or Visual Stuido. |
| 87 | + |
| 88 | +### Existing Limitations |
| 89 | +- `-os Android` is not supported for Windows yet (`WSL` can be used instead) |
| 90 | +- XHarness.CLI is not able to boot emulators yet (so you need to boot via `AVD Manager` or IDE) |
| 91 | +- AOT and Interpreter modes are not supported yet |
0 commit comments