Add Android sample test and emulator screenshot workflow #219
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| name: Test Android build scripts | |
| 'on': | |
| pull_request: | |
| paths: | |
| - 'scripts/**' | |
| - 'BUILDING.md' | |
| push: | |
| branches: | |
| - master | |
| paths-ignore: | |
| - '**/*.md' | |
| jobs: | |
| build-android: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| artifact_dir: ${{ steps.build-app.outputs.artifact_dir }} | |
| app_apk: ${{ steps.build-app.outputs.app_apk }} | |
| test_apk: ${{ steps.build-app.outputs.test_apk }} | |
| package_name: ${{ steps.build-app.outputs.package_name }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Setup workspace | |
| run: ./scripts/setup-workspace.sh -q -DskipTests | |
| - name: Build Android port | |
| run: ./scripts/build-android-port.sh -q -DskipTests | |
| - name: Build Hello Codename One Android app | |
| id: build-app | |
| run: ./scripts/build-android-app.sh -q -DskipTests | |
| - name: Upload Android APK artifacts | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: hello-codenameone-apks | |
| path: ${{ steps.build-app.outputs.artifact_dir }} | |
| if-no-files-found: error | |
| android-device-tests: | |
| needs: build-android | |
| runs-on: macos-14 | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v4 | |
| with: | |
| distribution: temurin | |
| java-version: '17' | |
| - name: Install Android SDK tools | |
| uses: android-actions/setup-android@v3 | |
| - name: Install required Android packages | |
| run: | | |
| set -eux | |
| yes 2>/dev/null | sdkmanager --licenses >/dev/null | |
| yes 2>/dev/null | sdkmanager --install "platform-tools" "build-tools;34.0.0" "platforms;android-33" >/dev/null | |
| - name: Verify platform tools | |
| run: | | |
| set -eux | |
| command -v adb | |
| adb version | |
| - name: Download APK artifacts | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: hello-codenameone-apks | |
| path: artifacts | |
| - name: Launch emulator and capture screenshots | |
| uses: reactivecircus/android-emulator-runner@v2 | |
| with: | |
| api-level: 33 | |
| arch: arm64-v8a | |
| profile: pixel_6 | |
| pre-emulator-launch-script: | | |
| set -euxo pipefail | |
| adb version | |
| script: | | |
| set -euxo pipefail | |
| mkdir -p screenshots | |
| adb install -r "artifacts/${{ needs.build-android.outputs.app_apk }}" | |
| adb install -r -t "artifacts/${{ needs.build-android.outputs.test_apk }}" | |
| adb logcat -c | |
| PACKAGE_NAME="${{ needs.build-android.outputs.package_name }}" | |
| TEST_PACKAGE="" | |
| INSTRUMENTATION_NAME="" | |
| INSTRUMENTATION_LINE="$(adb shell cmd package list instrumentation | tr -d '\r' | grep "target=${PACKAGE_NAME})" || true)" | |
| if [ -n "$INSTRUMENTATION_LINE" ]; then | |
| INSTRUMENTATION_NAME="${INSTRUMENTATION_LINE#instrumentation:}" | |
| INSTRUMENTATION_NAME="${INSTRUMENTATION_NAME%% (*}" | |
| TEST_PACKAGE="${INSTRUMENTATION_NAME%%/*}" | |
| fi | |
| if [ -n "$INSTRUMENTATION_NAME" ]; then | |
| echo "Running instrumentation tests via $INSTRUMENTATION_NAME" | |
| adb shell am instrument -w "$INSTRUMENTATION_NAME" | |
| else | |
| if [ -z "$TEST_PACKAGE" ]; then | |
| TEST_PACKAGE="${PACKAGE_NAME}.tests" | |
| fi | |
| echo "Instrumentation runner not found, launching $TEST_PACKAGE via monkey" | |
| adb shell monkey -p "$TEST_PACKAGE" 1 | |
| fi | |
| adb shell monkey -p "${{ needs.build-android.outputs.package_name }}" 1 || true | |
| sleep 20 | |
| if [ -f artifacts/tests.txt ]; then | |
| while IFS= read -r testName; do | |
| [ -n "$testName" ] || continue | |
| adb exec-out screencap -p > "screenshots/${testName}.png" | |
| done < artifacts/tests.txt | |
| else | |
| adb exec-out screencap -p > screenshots/test-suite.png | |
| fi | |
| target: default | |
| cores: 2 | |
| avd-name: test | |
| force-avd-creation: true | |
| emulator-boot-timeout: 600 | |
| emulator-options: -no-window -gpu host -no-snapshot -noaudio -no-boot-anim -feature HVF -accel hvf | |
| - name: Upload emulator screenshots | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: hello-codenameone-test-screenshots | |
| path: screenshots | |
| if-no-files-found: error |