diff --git a/.github/actions/android-sdk/action.yml b/.github/actions/android-sdk/action.yml new file mode 100644 index 0000000..11550a9 --- /dev/null +++ b/.github/actions/android-sdk/action.yml @@ -0,0 +1,62 @@ +# sdkmanager install with its downloads cached (actions/cache): the +# packages that the runner image does not have (for example CMake, or the +# emulator's system image) are kept in the cache, keyed on the runner image +# (ImageOS, ImageVersion) and the package list. On a hit, sdkmanager finds +# them installed and downloads nothing. On a miss, sdkmanager downloads +# them (it tries again up to 3 times), and the cache is saved for the next +# run. The packages that the image has are not cached. +name: Android SDK packages +description: sdkmanager install, cached +inputs: + packages: + description: the sdkmanager packages (spaces or newlines between), for example "cmake;3.31.6" + required: true +runs: + using: composite + steps: + # the key, and the directories of the packages that the image does not have + # ("cmake;3.31.6" is $ANDROID_HOME/cmake/3.31.6) + - id: key + shell: bash + env: + PKGS: ${{ inputs.packages }} + run: | + pkgs=$(printf '%s\n' $PKGS | sort -u | tr '\n' ' ') + echo "pkgs=$pkgs" >> "$GITHUB_OUTPUT" + dirs= + for p in $pkgs; do + d="$ANDROID_HOME/${p//;//}" + [ -e "$d" ] || dirs="$dirs$d"$'\n' + done + { + echo 'dirs<> "$GITHUB_OUTPUT" + [ -n "$dirs" ] && echo "any=true" >> "$GITHUB_OUTPUT" || echo "any=false" >> "$GITHUB_OUTPUT" + # (ImageOS, ImageVersion: the runner's environment, not the env context) + echo "key=sdk-${ImageOS:-none}-$RUNNER_ARCH-${ImageVersion:-none}-$(printf '%s' "$pkgs" | sha256sum | cut -c1-16)" >> "$GITHUB_OUTPUT" + + - id: restore + if: steps.key.outputs.any == 'true' + uses: actions/cache/restore@v5 + with: + path: ${{ steps.key.outputs.dirs }} + key: ${{ steps.key.outputs.key }} + + - id: install + shell: bash + env: + PKGS: ${{ steps.key.outputs.pkgs }} + run: | + set -euo pipefail + sdk="$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" + yes | "$sdk" --licenses > /dev/null || true + for i in 1 2 3; do "$sdk" $PKGS && break; [ $i = 3 ] && exit 1; sleep $((i * 10)); done + + - if: steps.key.outputs.any == 'true' && steps.restore.outputs.cache-hit != 'true' + uses: actions/cache/save@v5 + continue-on-error: true + with: + path: ${{ steps.key.outputs.dirs }} + key: ${{ steps.key.outputs.key }} diff --git a/.github/workflows/android.yml b/.github/workflows/android.yml index 6ed78a1..45c6d13 100644 --- a/.github/workflows/android.yml +++ b/.github/workflows/android.yml @@ -12,7 +12,12 @@ # pull request is tried with). # # Packages: apt's downloads are cached (.github/actions/apt), keyed on -# the runner image and the package list. +# the runner image and the package list. The Android SDK packages that +# the image does not have (CMake, the emulator's system image) are cached +# the same way (.github/actions/android-sdk). Robolectric's Android +# framework jars have their own cache entry, keyed on their versions +# (app/build.gradle.kts), so the unit tests do not download them from +# Maven Central. # # D-Modem is a public submodule: the checkout needs no token. # @@ -77,12 +82,26 @@ jobs: java-version: "21" - uses: gradle/actions/setup-gradle@v5 + with: + # (Robolectric's jars: their own cache, below) + gradle-home-cache-excludes: caches/modules-2/files-2.1/org.robolectric + + # Robolectric's Android framework jars (about 200 MB each, app/build.gradle.kts), in Gradle's + # cache: their own cache entry, keyed on their versions, so a run does not download them + # from Maven Central (its rate limit, HTTP 429) + - name: Robolectric's jars, key + id: robolectric + run: echo "key=robolectric-$(grep -h 'android-all-instrumented:' app/build.gradle.kts | sort | sha256sum | cut -c1-16)" >> "$GITHUB_OUTPUT" + - id: robolectric-restore + uses: actions/cache/restore@v5 + with: + path: ~/.gradle/caches/modules-2/files-2.1/org.robolectric + key: ${{ steps.robolectric.outputs.key }} - name: SDK packages - run: | - yes | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --licenses > /dev/null - "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" \ - "ndk;$NDK_VERSION" "cmake;$CMAKE_VERSION" "platforms;android-37.0" + uses: ./.github/actions/android-sdk + with: + packages: ndk;${{ env.NDK_VERSION }} cmake;${{ env.CMAKE_VERSION }} platforms;android-37.0 - name: OpenSSH, PuTTY, mksh (the interop tests), gawk (proot's build) uses: ./.github/actions/apt @@ -109,6 +128,20 @@ jobs: ANDROID_KEY_ALIAS: ${{ secrets.ANDROID_KEY_ALIAS }} ANDROID_KEY_PASSWORD: ${{ secrets.ANDROID_KEY_PASSWORD }} + # (saved also when a test fails: the jars that Gradle downloaded are good) + - name: Robolectric's jars, downloaded + id: robolectric-got + if: ${{ !cancelled() && steps.robolectric-restore.outputs.cache-hit != 'true' }} + run: | + d=~/.gradle/caches/modules-2/files-2.1/org.robolectric/android-all-instrumented + [ "$(find "$d" -name '*.jar' 2>/dev/null | wc -l)" -ge 2 ] && echo "save=true" >> "$GITHUB_OUTPUT" || true + - if: ${{ !cancelled() && steps.robolectric-got.outputs.save == 'true' }} + uses: actions/cache/save@v5 + continue-on-error: true + with: + path: ~/.gradle/caches/modules-2/files-2.1/org.robolectric + key: ${{ steps.robolectric.outputs.key }} + - name: The APK's signer if: ${{ !cancelled() && hashFiles('app/build/outputs/apk/debug/app-debug.apk') != '' }} run: | @@ -153,10 +186,9 @@ jobs: - uses: gradle/actions/setup-gradle@v5 - name: SDK packages - run: | - yes | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --licenses > /dev/null - "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" \ - "ndk;$NDK_VERSION" "cmake;$CMAKE_VERSION" "platforms;android-37.0" + uses: ./.github/actions/android-sdk + with: + packages: ndk;${{ env.NDK_VERSION }} cmake;${{ env.CMAKE_VERSION }} platforms;android-37.0 - name: gawk (proot's build) uses: ./.github/actions/apt @@ -294,10 +326,10 @@ jobs: sudo udevadm trigger --name-match=kvm - name: SDK packages - run: | - yes | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" --licenses > /dev/null - "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" \ - "ndk;$NDK_VERSION" "cmake;$CMAKE_VERSION" "platforms;android-37.0" + uses: ./.github/actions/android-sdk + with: + # (the emulator's system image too: the emulator runner finds it installed) + packages: ndk;${{ env.NDK_VERSION }} cmake;${{ env.CMAKE_VERSION }} platforms;android-37.0 system-images;android-35;google_apis;x86_64 - name: Instrumented tests uses: reactivecircus/android-emulator-runner@v2