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..dfaf8d3 100644 --- a/.github/workflows/android.yml +++ b/.github/workflows/android.yml @@ -12,7 +12,13 @@ # 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 jars have +# their own cache entry, keyed on their versions (app/build.gradle.kts). +# Gradle downloads from Google's mirror of Maven Central first +# (MAVEN_CENTRAL_MIRROR), because Maven Central's rate limit refuses +# downloads from the runners. # # D-Modem is a public submodule: the checkout needs no token. # @@ -43,6 +49,9 @@ concurrency: cancel-in-progress: ${{ github.event_name == 'pull_request' }} env: + # Google's mirror of Maven Central, which Gradle tries first (settings.gradle.kts): Maven Central's + # rate limit (HTTP 429) refuses downloads from the runners + MAVEN_CENTRAL_MIRROR: https://maven-central.storage-download.googleapis.com/maven2/ NDK_VERSION: "29.0.14206865" CMAKE_VERSION: "3.31.6" @@ -78,11 +87,26 @@ jobs: - uses: gradle/actions/setup-gradle@v5 - - name: SDK packages + # 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) + # (the key: Robolectric's version and its jars' versions, then the files the entry holds; + # a run that downloads more files saves a new entry) + - name: Robolectric's jars, key + id: robolectric 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" + echo "prefix=robolectric-$({ grep -h '^robolectric' gradle/libs.versions.toml; 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.prefix }} + restore-keys: ${{ steps.robolectric.outputs.prefix }} + + - name: SDK packages + 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 +133,27 @@ 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() }} + env: + PREFIX: ${{ steps.robolectric.outputs.prefix }} + RESTORED: ${{ steps.robolectric-restore.outputs.cache-matched-key }} + run: | + d=~/.gradle/caches/modules-2/files-2.1/org.robolectric + [ -d "$d" ] || exit 0 + key="$PREFIX$(cd "$d" && find . -type f | sort | sha256sum | cut -c1-16)" + if [ "$key" != "$RESTORED" ]; then + echo "key=$key" >> "$GITHUB_OUTPUT" + fi + - if: ${{ !cancelled() && steps.robolectric-got.outputs.key != '' }} + uses: actions/cache/save@v5 + continue-on-error: true + with: + path: ~/.gradle/caches/modules-2/files-2.1/org.robolectric + key: ${{ steps.robolectric-got.outputs.key }} + - name: The APK's signer if: ${{ !cancelled() && hashFiles('app/build/outputs/apk/debug/app-debug.apk') != '' }} run: | @@ -153,10 +198,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 +338,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 diff --git a/.github/workflows/apple.yml b/.github/workflows/apple.yml index faa8451..3f1f285 100644 --- a/.github/workflows/apple.yml +++ b/.github/workflows/apple.yml @@ -23,6 +23,11 @@ concurrency: group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} cancel-in-progress: ${{ github.event_name == 'pull_request' }} +env: + # Google's mirror of Maven Central, which Gradle tries first (settings.gradle.kts): Maven Central's + # rate limit (HTTP 429) refuses downloads from the runners + MAVEN_CENTRAL_MIRROR: https://maven-central.storage-download.googleapis.com/maven2/ + jobs: engine: runs-on: macos-latest diff --git a/.github/workflows/d-modem-bump.yml b/.github/workflows/d-modem-bump.yml index bb02989..a9b43a3 100644 --- a/.github/workflows/d-modem-bump.yml +++ b/.github/workflows/d-modem-bump.yml @@ -51,9 +51,12 @@ jobs: exit 1 fi + # main as it is now, not the commit the event names: a run that waited for the one before it + # (concurrency) builds on the main that the earlier run merged into, and does not conflict with it - uses: actions/checkout@v6 with: token: ${{ secrets.DMODEM_BUMP_TOKEN }} + ref: main fetch-depth: 0 # only the D-Modem submodule (proot stays where it is) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 7aa3d7a..d23031c 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -201,10 +201,39 @@ val buildHostSsh = tasks.register("buildHostSsh") { ) } -// Robolectric fetches its Android framework jars itself: -ProbolectricRepoUrl= to fetch them elsewhere +// Robolectric's Android framework jars (android-all-instrumented, about 200 MB each), one for each SDK +// that a test's @Config names (app/src/test). Gradle downloads them, with its retries and its cache +// (CI's setup-gradle keeps the cache). Robolectric then runs offline from their directory. +// Without this, Robolectric downloads them itself during the tests, and Maven Central's rate limit +// (HTTP 429) makes each Robolectric test fail. +// A new SDK in a test's @Config: add its jar here. Robolectric's DefaultSdkProvider gives the version +// for its release (libs.versions.toml): "-robolectric--i". +// -ProbolectricRepoUrl=: Robolectric downloads them itself from that mirror instead. +// (one configuration for each jar: in one configuration, Gradle keeps only the highest version of a module) +val robolectricJars = mapOf( + // SDK 28 (Android 9) and SDK 35 (Android 15), Robolectric 4.17's jars + 28 to "org.robolectric:android-all-instrumented:9-robolectric-4913185-2-i7", + 35 to "org.robolectric:android-all-instrumented:15-robolectric-13954326-i7", +).map { (sdk, jar) -> + configurations.create("robolectricJarsSdk$sdk") { + isTransitive = false + isCanBeConsumed = false + }.also { dependencies.add(it.name, jar) } +} +val robolectricJarsDir = layout.buildDirectory.dir("robolectric-jars") +val syncRobolectricJars = tasks.register("syncRobolectricJars") { + description = "Copies Robolectric's Android framework jars to one directory, for its offline mode" + from(robolectricJars) + into(robolectricJarsDir) +} +val robolectricRepoUrl = providers.gradleProperty("robolectricRepoUrl").orNull tasks.withType().configureEach { - providers.gradleProperty("robolectricRepoUrl").orNull?.let { - systemProperty("robolectric.dependency.repo.url", it) + if (robolectricRepoUrl != null) { + systemProperty("robolectric.dependency.repo.url", robolectricRepoUrl) + } else { + dependsOn(syncRobolectricJars) + systemProperty("robolectric.offline", "true") + systemProperty("robolectric.dependency.dir", robolectricJarsDir.get().asFile.path) } // the SSH client's JNI library built for the host (native-ssh), for the SSH tests dependsOn(buildHostSsh) diff --git a/gradle.properties b/gradle.properties index 5299c5f..a63faaa 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,3 +3,7 @@ org.gradle.caching=true android.useAndroidX=true android.nonTransitiveRClass=true kotlin.code.style=official +# A download that Maven Central refuses for a short time (HTTP 429, its rate limit): Gradle tries it again, +# up to 6 times, waiting 2 s, then 4 s, and so on (Robolectric's large jars: app/build.gradle.kts) +systemProp.org.gradle.internal.repository.max.retries=6 +systemProp.org.gradle.internal.repository.initial.backoff=2000 diff --git a/settings.gradle.kts b/settings.gradle.kts index bbbb96b..bafa7c1 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,14 +1,20 @@ +// MAVEN_CENTRAL_MIRROR (CI: Google's mirror of Maven Central): a mirror that Gradle tries before Maven +// Central itself, whose rate limit (HTTP 429) refuses downloads from CI's runners +// (pluginManagement comes first in a settings script: it reads the variable itself) pluginManagement { repositories { google() + System.getenv("MAVEN_CENTRAL_MIRROR")?.takeIf { it.isNotEmpty() }?.let { maven(it) } mavenCentral() gradlePluginPortal() } } +val mavenCentralMirror: String? = providers.environmentVariable("MAVEN_CENTRAL_MIRROR").orNull?.takeIf { it.isNotEmpty() } dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() + mavenCentralMirror?.let { maven(it) } mavenCentral() // usb-serial-for-android (USB serial adapters' drivers) is on JitPack only: JitPack for it alone exclusiveContent {