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