Skip to content

Commit ec2209e

Browse files
authored
[docs] How to run tests on iOS and Android (#36297)
1 parent 827a523 commit ec2209e

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Testing Libraries on iOS and tvOS
2+
3+
In order to build libraries and tests for iOS or tvOS you need recent version of XCode installed (e.g. 11.3 or higher).
4+
5+
Build Libraries for iOS:
6+
```
7+
./build.sh mono+libs -os iOS -arch x64
8+
```
9+
Run tests one by one for each test suite on a simulator:
10+
```
11+
./build.sh libs.tests -os iOS -arch x64 -test
12+
```
13+
In order to run the tests on a device you need to specify `DevTeamProvisioning` (see [developer.apple.com/account/#/membership](https://developer.apple.com/account/#/membership), scroll down to `Team ID`):
14+
```
15+
./build.sh libs.tests -os iOS -arch x64 -test /p:DevTeamProvisioning=H1A2B3C4D5
16+
```
17+
[AppleAppBuilder](https://github.com/dotnet/runtime/blob/master/src/mono/msbuild/AppleAppBuilder/AppleAppBuilder.cs) generates temp Xcode projects you can manually open and resolve provisioning issues there using native UI and deploy to your devices.
18+
19+
### Running individual test suites
20+
- The following shows how to run tests for a specific library:
21+
```
22+
./dotnet.sh build src/libraries/System.Numerics.Vectors/tests /t:Test /p:TargetOS=iOS /p:TargetArchitecture=x64
23+
```
24+
25+
### Test App Design
26+
iOS/tvOS `*.app` (or `*.ipa`) is basically a simple [ObjC app](https://github.com/dotnet/runtime/blob/master/src/mono/msbuild/AppleAppBuilder/Templates/main-console.m) that inits the Mono Runtime. This Mono Runtime starts a simple xunit test
27+
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 to deploy `*.app` and `*.ipa` to a target (device or simulator) and listens for logs via network sockets.
28+
29+
### Existing Limitations
30+
- Most of the test suites crash on devices due to #35674
31+
- Simulator uses JIT mode only at the moment (to be extended with FullAOT and Interpreter)
32+
- Interpreter is not enabled yet.

0 commit comments

Comments
 (0)