1#!/bin/bash 2 3# Copyright 2018 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17################################################ 18# Script to build and run the Oboe tests on an attached Android device or emulator 19# 20# Prerequisites: 21# - CMake on PATH. This is usually found in $ANDROID_HOME/cmake/<version>/bin. 22# - ANDROID_NDK environment variable is set to your Android NDK location 23# e.g. $HOME/Library/Android/sdk/ndk/<version> 24# - Android device or emulator attached and accessible via adb 25# 26# Instructions: 27# - Run this script 28# - Check the test results on your target device 29# 30# What does the script do? 31# - Builds a test binary for the target architecture 32# - Copies the test binary into the UnitTestRunner app 33# - Builds, installs and runs the app on the target device 34# 35# The initial run may take some time as GTest is built, subsequent runs should be much faster. 36# 37# If you want to perform a clean build just delete the 'build' folder and re-run this script. You will need to do 38# this if you change target architectures (e.g. when changing between real device and emulator) 39# 40# Why is running the tests so convoluted? 41# The tests require the RECORDING permission and on many devices (e.g Samsung) the adb user does not have this 42# permission (and `run-as` is broken). This means that the test binary must be executed by an app which has this 43# permission, hence the need for the UnitTestRunner app. 44# 45################################################ 46 47# Directories, paths and filenames 48BUILD_DIR=build 49CMAKE=cmake 50TEST_BINARY_FILENAME=testOboe 51TEST_RUNNER_DIR=UnitTestRunner 52TEST_RUNNER_PACKAGE_NAME=com.google.oboe.tests.unittestrunner 53TEST_RUNNER_JNILIBS_DIR=${TEST_RUNNER_DIR}/app/src/main/jniLibs 54TEST_RUNNER_ASSETS_DIR=${TEST_RUNNER_DIR}/app/src/main/assets 55 56# Check prerequisites 57if [ -z "$ANDROID_NDK" ]; then 58 echo "Please set ANDROID_NDK to the Android NDK folder" 59 exit 1 60fi 61 62if [ ! $(type -P ${CMAKE}) ]; then 63 echo "${CMAKE} was not found on your path. You can install it using Android Studio using Tools->Android->SDK Manager->SDK Tools." 64 echo "Once done you will need to add ${HOME}/Library/Android/sdk/cmake/<current_version>/bin to your path." 65 exit 1 66fi 67 68# Get the device ABI 69ABI=$(adb shell getprop ro.product.cpu.abi | tr -d '\n\r') 70 71if [ -z "$ABI" ]; then 72 echo "No device ABI was set. Please ensure a device or emulator is running. You may need to unplug extra devices." 73 exit 1 74fi 75 76echo "Device/emulator architecture is $ABI" 77 78if [ ${ABI} == "arm64-v8a" ] || [ ${ABI} == "x86_64" ]; then 79 PLATFORM=android-21 80elif [ ${ABI} == "armeabi-v7a" ] || [ ${ABI} == "x86" ]; then 81 PLATFORM=android-16 82else 83 echo "Unrecognised ABI: ${ABI}. Supported ABIs are: arm64-v8a, armeabi-v7a, x86_64, x86. If you feel ${ABI} should be supported please file an issue on github.com/google/oboe" 84 exit 1 85fi 86 87mkdir -p ${BUILD_DIR} 88 89echo "Cleaning up previous build because swapping phones may result in stale binaries" 90rm -r ${BUILD_DIR} 91 92# Configure the build 93echo "Building tests for ${ABI} using ${PLATFORM}" 94 95CMAKE_ARGS="-S. \ 96 -B${BUILD_DIR} \ 97 -DANDROID_ABI=${ABI} \ 98 -DANDROID_PLATFORM=${PLATFORM} \ 99 -DCMAKE_BUILD_TYPE=RelWithDebInfo \ 100 -DCMAKE_CXX_FLAGS=-std=c++17 \ 101 -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \ 102 -DCMAKE_VERBOSE_MAKEFILE=1" 103 104cmake ${CMAKE_ARGS} 105 106# Perform the build 107pushd ${BUILD_DIR} 108 make -j5 109 110 if [ $? -eq 0 ]; then 111 echo "Tests built successfully" 112 else 113 echo "Building tests FAILED" 114 exit 1 115 fi 116 117popd 118 119# Copy the binary into the jniLibs and assets folders of the unit test runner app 120# The assets folder does not work after Android R for security reasons 121# The jniLibs folder doesn't seem to work before Android O 122# Thus, copy into both 123mkdir ${TEST_RUNNER_JNILIBS_DIR} 124mkdir ${TEST_RUNNER_JNILIBS_DIR}/${ABI} 125DESTINATION_DIR=${TEST_RUNNER_JNILIBS_DIR}/${ABI}/${TEST_BINARY_FILENAME} 126echo "Copying binary to ${DESTINATION_DIR}" 127cp ${BUILD_DIR}/${TEST_BINARY_FILENAME} ${DESTINATION_DIR}.so 128mkdir ${TEST_RUNNER_ASSETS_DIR} 129mkdir ${TEST_RUNNER_ASSETS_DIR}/${ABI} 130DESTINATION_DIR=${TEST_RUNNER_ASSETS_DIR}/${ABI}/${TEST_BINARY_FILENAME} 131echo "Copying binary to ${DESTINATION_DIR}" 132cp ${BUILD_DIR}/${TEST_BINARY_FILENAME} ${DESTINATION_DIR}.so 133 134# Build and install the unit test runner app 135pushd ${TEST_RUNNER_DIR} 136 echo "Building test runner app" 137 ./gradlew assembleDebug 138 if [ $? -ne 0 ]; then 139 echo "Building test app FAILED" 140 exit 1 141 fi 142 143 echo "Installing to device" 144 ./gradlew installDebug 145 if [ $? -ne 0 ]; then 146 echo "Installing tests FAILED" 147 exit 1 148 fi 149popd 150 151echo "Clear logcat from before the test." 152adb logcat -c 153echo "Starting app - Check your device for test results" 154adb shell am start ${TEST_RUNNER_PACKAGE_NAME}/.MainActivity 155 156sleep 1 157echo "Logging test logs and Oboe logs. Run adb logcat for complete logs." 158adb logcat ${TEST_RUNNER_PACKAGE_NAME}.MainActivity:V OboeAudio:V *:S 159