| #!/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. | |
| # | |
| set -e | |
| # Navigate to the repository root | |
| # 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" | |
| # Use the Bazel caches pre-warmed in the container image, if any. | |
| bazel_flags=() | |
| if [[ -n "${BAZEL_REPOSITORY_CACHE:-}" ]]; then | |
| bazel_flags+=("--repository_cache=${BAZEL_REPOSITORY_CACHE}") | |
| fi | |
| if [[ -n "${BAZEL_DISK_CACHE:-}" ]]; then | |
| bazel_flags+=("--disk_cache=${BAZEL_DISK_CACHE}") | |
| fi | |
| # Run Bazel build and test all targets | |
| bazel build "${bazel_flags[@]}" ... | |
| bazel test "${bazel_flags[@]}" ... | |
| 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 > "${KOKORO_ARTIFACTS_DIR}/emulator-logs/emulator_${emulator_name}.log" 2>&1 & | |
| } | |
| 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 [ "${RUN_INSTRUMENTATION_TESTS:-0}" = "1" ] || [ "${RUN_INSTRUMENTATION_TESTS:-false}" = "true" ]; then | |
| echo "Stopping emulator..." | |
| adb emu kill 2>/dev/null || true | |
| fi | |
| } | |
| trap cleanup EXIT | |
| # --- Run Gradle build and tests for C2PA Android SDK --- | |
| if [ -d "android" ] && [ -x "android/gradlew" ]; then | |
| if ! java -version 2>&1 | grep -q "17\."; then | |
| echo "Installing OpenJDK 17..." | |
| sudo apt-get update || true | |
| sudo apt-get install -y openjdk-17-jdk || true | |
| export JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64" | |
| export PATH="${JAVA_HOME}/bin:${PATH}" | |
| fi | |
| cd android | |
| gradle_tasks=(assembleDebug testDebugUnitTest) | |
| if [ "${RUN_INSTRUMENTATION_TESTS:-0}" = "1" ] || [ "${RUN_INSTRUMENTATION_TESTS:-false}" = "true" ]; then | |
| echo "Starting Android emulator for instrumentation tests..." | |
| create_and_start_emulator | |
| wait_for_emulator_to_boot | |
| gradle_tasks+=(connectedAndroidTest) | |
| fi | |
| ./gradlew "${gradle_tasks[@]}" --no-daemon --stacktrace | |
| else | |
| echo "No android/gradlew found; skipping Android Gradle build." | |
| fi |