blob: ffd42264126d7f2a20131927e6db3ed296728b29 [file]
#!/bin/bash
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Builds and tests the C2PA Android SDK, including emulator instrumentation
# tests, on a Kokoro VM that supports nested virtualization.
set -e
# KOKORO_ARTIFACTS_DIR is set to /tmpfs/src by default in the Kokoro Ubuntu image.
export KOKORO_ARTIFACTS_DIR="${KOKORO_ARTIFACTS_DIR:-/tmpfs/src}"
# Default to running instrumentation tests when Android environment is present.
export RUN_INSTRUMENTATION_TESTS="${RUN_INSTRUMENTATION_TESTS:-1}"
cd "${KOKORO_ARTIFACTS_DIR}/git/staging"
EMULATOR_PID=""
function create_and_start_emulator() {
local emulator_name=c2pa_emulator
if ! sdkmanager --list_installed 2>/dev/null | grep -q "system-images;android-35;google_apis;x86_64"; then
echo "Emulator system image not pre-installed; downloading..."
echo y | sdkmanager "platforms;android-35" "emulator" "system-images;android-35;google_apis;x86_64" || true
fi
echo no | avdmanager create avd -n "${emulator_name}" \
--package "system-images;android-35;google_apis;x86_64" \
--force
mkdir -p "${KOKORO_ARTIFACTS_DIR}/emulator-logs"
"${ANDROID_HOME}/emulator/emulator" -avd "${emulator_name}" \
-no-window \
-gpu swiftshader_indirect \
-no-snapshot \
-no-audio \
-no-boot-anim </dev/null > "${KOKORO_ARTIFACTS_DIR}/emulator-logs/emulator_${emulator_name}.log" 2>&1 &
EMULATOR_PID=$!
}
function wait_for_emulator_to_boot() {
local retries=60 # 5 minutes (60 * 5s)
while [[ $retries -gt 0 ]]; do
local boot_completed
boot_completed=$(adb shell getprop sys.boot_completed 2>/dev/null | tr -d '\r')
if [[ "${boot_completed}" == "1" ]]; then
break
fi
echo "Waiting for emulator to boot..."
sleep 5
((retries--))
done
if [[ $retries -eq 0 ]]; then
echo "Emulator failed to boot in time."
exit 1
fi
echo "Emulator booted."
}
function cleanup() {
set +e
if [ -n "${EMULATOR_PID:-}" ]; then
echo "Stopping emulator..."
adb emu kill >/dev/null 2>&1 || true
kill -9 "${EMULATOR_PID}" >/dev/null 2>&1 || true
wait "${EMULATOR_PID}" 2>/dev/null || true
fi
if [ -x "android/gradlew" ]; then
(cd android && ./gradlew --stop >/dev/null 2>&1) || true
elif [ -x "./gradlew" ]; then
./gradlew --stop >/dev/null 2>&1 || true
fi
if command -v adb >/dev/null 2>&1; then
adb kill-server >/dev/null 2>&1 || true
fi
}
trap cleanup EXIT
# --- Run Gradle build and tests for C2PA Android SDK ---
if [ -d "android" ] && [ -x "android/gradlew" ]; then
cd android
./gradlew assembleDebug :crypto:tsa_app:assembleDebug testDebugUnitTest --no-daemon --stacktrace
if [ "${RUN_INSTRUMENTATION_TESTS:-0}" = "1" ] || [ "${RUN_INSTRUMENTATION_TESTS:-false}" = "true" ]; then
# Fail fast with a clear message instead of timing out on emulator boot.
if [ ! -w /dev/kvm ]; then
echo "/dev/kvm is not available; the Android emulator needs nested virtualization."
exit 1
fi
echo "Starting Android emulator for instrumentation tests..."
create_and_start_emulator
wait_for_emulator_to_boot
echo "Installing TSA test app APK (debug) for instrumentation test runs..."
for apk in crypto/tsa_app/build/outputs/apk/debug/*.apk; do
if [ -f "$apk" ]; then
adb install -r -t "$apk"
break
fi
done
./gradlew connectedAndroidTest \
-Pandroid.testInstrumentationRunnerArguments.clearPackageData=true \
-Pandroid.testoptions.execution=ANDROIDX_TEST_ORCHESTRATOR \
--no-daemon --stacktrace
fi
else
echo "No android/gradlew found; skipping Android Gradle build."
fi