1#!/bin/bash
2# Copyright (c) Meta Platforms, Inc. and affiliates.
3#
4# This source code is licensed under the MIT license found in the
5# LICENSE file in the root directory of this source tree.
6
7# inspired by https://github.com/Originate/guide/blob/master/android/guide/Continuous%20Integration.md
8
9# shellcheck disable=SC1091
10source "scripts/.tests.env"
11
12# NOTE: This doesn't run in Circle CI currently!
13function getAndroidPackages {
14  export PATH="$ANDROID_HOME/platform-tools:$ANDROID_HOME/tools:$ANDROID_HOME/tools.bin:$PATH"
15
16  DEPS="$ANDROID_HOME/installed-dependencies"
17
18  # Package names can be obtained using `sdkmanager --list`
19  if [ ! -e "$DEPS" ] || [ ! "$CI" ]; then
20    echo "Installing Android API level $ANDROID_SYSTEM_IMAGE_API_LEVEL, Google APIs, $AVD_ABI system image..."
21    sdkmanager "system-images;android-$ANDROID_SYSTEM_IMAGE_API_LEVEL;google_apis;$AVD_ABI"
22    echo "Installing build SDK for Android API level $ANDROID_SDK_BUILD_API_LEVEL..."
23    sdkmanager "platforms;android-$ANDROID_SDK_BUILD_API_LEVEL"
24    echo "Installing target SDK for Android API level $ANDROID_SDK_TARGET_API_LEVEL..."
25    sdkmanager "platforms;android-$ANDROID_SDK_TARGET_API_LEVEL"
26    echo "Installing SDK build tools, revision $ANDROID_SDK_BUILD_TOOLS_REVISION..."
27    sdkmanager "build-tools;$ANDROID_SDK_BUILD_TOOLS_REVISION"
28    # These moved to "system-images;android-$ANDROID_SDK_BUILD_API_LEVEL;google_apis;x86" starting with API level 25, but there is no ARM version.
29    echo "Installing Google APIs $ANDROID_GOOGLE_API_LEVEL..."
30    sdkmanager "add-ons;addon-google_apis-google-$ANDROID_GOOGLE_API_LEVEL"
31    echo "Installing Android Support Repository"
32    sdkmanager "extras;android;m2repository"
33    $CI && touch "$DEPS"
34  fi
35}
36
37function getAndroidNDK {
38  NDK_HOME="/opt/ndk"
39  DEPS="$NDK_HOME/installed-dependencies"
40
41  if [ ! -e $DEPS ]; then
42    cd $NDK_HOME || exit
43    echo "Downloading NDK..."
44    curl -o ndk.zip https://dl.google.com/android/repository/android-ndk-r19c-linux-x86_64.zip
45    unzip -o -q ndk.zip
46    echo "Installed Android NDK at $NDK_HOME"
47    touch $DEPS
48    rm ndk.zip
49  fi
50}
51
52function createAVD {
53  if [ -z "$ANDROID_DISABLE_AVD_TESTS" ]
54  then
55    AVD_PACKAGES="system-images;android-$ANDROID_IMAGE_API_LEVEL;google_apis;$AVD_ABI"
56    echo "Creating AVD with packages $AVD_PACKAGES"
57    echo no | avdmanager create avd --name "$AVD_NAME" --force --package "$AVD_PACKAGES" --tag google_apis --abi "$AVD_ABI"
58  else
59    echo "Skipping AVD-related test setup..."
60  fi
61}
62
63function launchAVD {
64  # Force start adb server
65  adb start-server
66
67  if [ -z "$ANDROID_DISABLE_AVD_TESTS" ]
68  then
69    # The AVD name here should match the one created in createAVD
70    if [ "$CI" ]
71    then
72      "$ANDROID_HOME/emulator/emulator" -avd "$AVD_NAME" -no-audio -no-window
73    else
74      "$ANDROID_HOME/emulator/emulator" -avd "$AVD_NAME"
75    fi
76  else
77    echo "Skipping AVD-related test setup..."
78  fi
79}
80
81function waitForAVD {
82  if [ -z "$ANDROID_DISABLE_AVD_TESTS" ]
83  then
84    echo "Waiting for Android Virtual Device to finish booting..."
85    local bootanim=""
86    export PATH=$(dirname $(dirname $(command -v android)))/platform-tools:$PATH
87    until [[ "$bootanim" =~ "stopped" ]]; do
88      sleep 5
89      bootanim=$(adb -e shell getprop init.svc.bootanim 2>&1)
90      echo "boot animation status=$bootanim"
91    done
92    echo "Android Virtual Device is ready."
93  else
94    echo "Skipping AVD-related test setup..."
95  fi
96}
97