Skip to content

Commit 9ab6968

Browse files
committed
test(nav3): Verify the SerialName mapper under R8
Add nav3-built-in-back-stack-mappers-r8-check, an app that maps representative back stack keys with BackStackEntryMapper.SerialName in an R8-minified release build and reports whether each name and unknown entry reason is right, and scripts/test-nav3-built-in-back-stack-mappers-r8.sh, which builds it and runs it on a booted emulator or device. Besides names, the app checks that R8 renamed classes, and, through a logger that records the mapper's messages, that the compatibility probe was conclusive. The Nav3 R8 workflow runs the check on API 23 in R8 full mode against the kotlinx.serialization floor and its latest stable release, and in compat mode against the floor, and runs the module's unit tests against the latest stable release. It only runs when files that can affect the check change.
1 parent 7010a59 commit 9ab6968

11 files changed

Lines changed: 807 additions & 0 deletions

File tree

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
name: Nav3 Built-in Back Stack Mappers R8 Check
2+
3+
# Verifies behavior of BackStackEntryMapper.SerialName from sentry-android-navigation3 in
4+
# R8-minified builds on an emulator. Runs tests in:
5+
#
6+
# - R8 full mode against the compile floor of kotlinx-serialization-core and its latest stable
7+
# release; and
8+
# - R8 compat mode against the compile floor.
9+
#
10+
# Also runs all the module's unit tests against the latest stable release. See
11+
# sentry-android-integration-tests/nav3-built-in-back-stack-mappers-r8-check/README.md.
12+
13+
# Only runs for the paths below. Not a required check, so skipped runs don't block PRs. Catalog
14+
# filters match whole files, including changes to unrelated dependencies.
15+
on:
16+
push:
17+
branches:
18+
- main
19+
paths: &nav3-r8-paths
20+
- 'sentry-android-navigation3/**'
21+
- 'sentry-android-integration-tests/nav3-built-in-back-stack-mappers-r8-check/**'
22+
# The settings, build script, and properties of the build that produces the check's APKs.
23+
- 'sentry-android-integration-tests/*.gradle.kts'
24+
- 'sentry-android-integration-tests/*.properties'
25+
- 'scripts/test-nav3-built-in-back-stack-mappers-r8.sh'
26+
- '.github/workflows/integration-tests-nav3-r8.yml'
27+
# Build settings, R8 flags, build logic, the Gradle version, and the version catalogs
28+
# (Kotlin, AGP and its R8, Compose, Nav3, and the kotlinx.serialization matrix).
29+
- 'gradle.properties'
30+
- 'settings.gradle.kts'
31+
- 'build.gradle.kts'
32+
- 'gradle/wrapper/**'
33+
- 'gradle/libs.versions.toml'
34+
- 'gradle/dependency-matrix.versions.toml'
35+
- 'buildSrc/**'
36+
- 'build-logic/**'
37+
# Utilities the mapper uses to handle exceptions.
38+
- 'sentry/src/main/java/io/sentry/util/**'
39+
pull_request:
40+
paths: *nav3-r8-paths
41+
42+
permissions:
43+
contents: read
44+
45+
concurrency:
46+
group: ${{ github.workflow }}-${{ github.ref }}
47+
cancel-in-progress: true
48+
49+
env:
50+
APK_DIR: "nav3-r8-check-apks"
51+
APK_ARTIFACT_NAME: "nav3-r8-check-apks"
52+
53+
jobs:
54+
# Runs separately from the APK builds, so a unit test failure doesn't hide the R8 check's results.
55+
unit-tests:
56+
name: Unit tests
57+
runs-on: ubuntu-latest
58+
59+
steps:
60+
- name: Checkout code
61+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
62+
63+
- name: Set up Java
64+
uses: actions/setup-java@b843ebf9b913146b38da2eaa7aeddf76074ae1ad # v6.1.0
65+
with:
66+
distribution: 'temurin'
67+
java-version: '21'
68+
69+
- name: Setup Gradle
70+
uses: gradle/actions/setup-gradle@8da46b539cd8fe6ab27ca75128919a18d757604c # v6.4.1
71+
72+
# Apps may ship a newer kotlinx.serialization than sentry-android-navigation3 compiles
73+
# against. The Build workflow runs the unit tests against the floor.
74+
- name: Run unit tests against the latest stable kotlinx.serialization
75+
run: ./gradlew :sentry-android-navigation3:testReleaseUnitTest -Pnav3TestKotlinx=latestStable
76+
77+
build:
78+
name: Build
79+
runs-on: ubuntu-latest
80+
81+
steps:
82+
- name: Checkout code
83+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
84+
85+
- name: Set up Java
86+
uses: actions/setup-java@b843ebf9b913146b38da2eaa7aeddf76074ae1ad # v6.1.0
87+
with:
88+
distribution: 'temurin'
89+
java-version: '21'
90+
91+
- name: Setup Gradle
92+
uses: gradle/actions/setup-gradle@8da46b539cd8fe6ab27ca75128919a18d757604c # v6.4.1
93+
94+
- name: Build APKs
95+
run: ./scripts/test-nav3-built-in-back-stack-mappers-r8.sh build "${{ env.APK_DIR }}"
96+
97+
- name: Upload APK artifact
98+
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7
99+
with:
100+
name: ${{ env.APK_ARTIFACT_NAME }}
101+
path: ${{ env.APK_DIR }}
102+
retention-days: 1
103+
104+
# Run on API 23, Nav3's minSdk, to verify that the minified app and its dependencies work on the
105+
# oldest supported runtime.
106+
run:
107+
name: Run on API Level 23
108+
needs: build
109+
runs-on: ubuntu-latest
110+
steps:
111+
- name: Checkout code
112+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
113+
114+
- name: Enable KVM
115+
run: |
116+
echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
117+
sudo udevadm control --reload-rules
118+
sudo udevadm trigger --name-match=kvm
119+
120+
- name: Download APK artifact
121+
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8
122+
with:
123+
name: ${{ env.APK_ARTIFACT_NAME }}
124+
path: ${{ env.APK_DIR }}
125+
126+
- name: Run check
127+
uses: reactivecircus/android-emulator-runner@a421e43855164a8197daf9d8d40fe71c6996bb0d # pin@v2.38.0
128+
with:
129+
api-level: 23
130+
target: google_apis
131+
arch: x86_64
132+
disable-animations: true
133+
disable-spellchecker: true
134+
emulator-options: -memory 4096 -no-window -gpu auto -noaudio -no-boot-anim -camera-back none -no-snapshot-save
135+
script: ./scripts/test-nav3-built-in-back-stack-mappers-r8.sh run "${{ env.APK_DIR }}"
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#!/usr/bin/env bash
2+
# Verifies BackStackEntryMapper.SerialName in R8-minified builds on a booted emulator or device.
3+
# See sentry-android-integration-tests/nav3-built-in-back-stack-mappers-r8-check/README.md.
4+
#
5+
# Usage:
6+
# test-nav3-built-in-back-stack-mappers-r8.sh Build all APKs, then run them.
7+
# test-nav3-built-in-back-stack-mappers-r8.sh build DIR Build all APKs into DIR.
8+
# test-nav3-built-in-back-stack-mappers-r8.sh run DIR Install and run every APK built into DIR.
9+
set -euo pipefail
10+
11+
module="nav3-built-in-back-stack-mappers-r8-check"
12+
moduleDir="sentry-android-integration-tests/nav3-built-in-back-stack-mappers-r8-check"
13+
appId="io.sentry.tests.nav3.r8"
14+
tag="NAV3_R8_CHECK"
15+
16+
copyApk() {
17+
cp "${moduleDir}/build/outputs/apk/release/nav3-built-in-back-stack-mappers-r8-check-release.apk" "$1"
18+
}
19+
20+
# Prints the subdirectory of DIR $1 that holds this script's APKs.
21+
apkDirIn() {
22+
echo "$1/${module}"
23+
}
24+
25+
build() {
26+
local outDir
27+
outDir="$(apkDirIn "$1")"
28+
mkdir -p "$outDir"
29+
# Otherwise run() would also run APKs for versions no longer tested.
30+
rm -f "$outDir"/*.apk
31+
for mode in full compat; do
32+
local fullMode=true
33+
[[ "$mode" == "compat" ]] && fullMode=false
34+
echo "Building R8 $mode mode APK (kotlinx floor)..."
35+
./gradlew -p sentry-android-integration-tests ":${module}:assembleRelease" "-Pandroid.enableR8.fullMode=${fullMode}"
36+
copyApk "${outDir}/${mode}.apk"
37+
38+
# Compat mode keeps more than full mode, so it's only tested with the floor, where it changes
39+
# the outcome for named companion objects (kotlinx.serialization's own R8 rules only cover them
40+
# from 1.10.0 on).
41+
[[ "$mode" == "compat" ]] && continue
42+
43+
echo "Building R8 $mode mode APK (kotlinx latestStable)..."
44+
./gradlew -p sentry-android-integration-tests ":${module}:assembleRelease" \
45+
"-Pandroid.enableR8.fullMode=${fullMode}" "-Pnav3CheckKotlinx=latestStable"
46+
copyApk "${outDir}/${mode}-kotlinx-latestStable.apk"
47+
done
48+
}
49+
50+
# Installs and launches APK $1, prints its check's output, and fails unless the check passed.
51+
runApk() {
52+
local apk="$1"
53+
adb uninstall "$appId" > /dev/null 2>&1 || true
54+
adb install "$apk" > /dev/null || return 1
55+
# Reads only this launch's lines, so the previous APK's RESULT=PASS can't hide a failure. The
56+
# timestamp has second precision, hence the sleep.
57+
local since
58+
sleep 1
59+
since="$(adb shell "date +'%m-%d %H:%M:%S.000'" | tr -d '\r')" || return 1
60+
adb shell am start -W -n "${appId}/.BuiltInBackStackMappersActivity" > /dev/null || return 1
61+
62+
local output=""
63+
for _ in $(seq 1 60); do
64+
output="$(adb logcat -d -T "$since" -s "${tag}:I" | grep "${tag}" || true)"
65+
if grep -q "RESULT=" <<< "$output"; then
66+
break
67+
fi
68+
if adb logcat -d -T "$since" -b crash | grep -q "$appId"; then
69+
break
70+
fi
71+
sleep 1
72+
done
73+
74+
if [[ -n "$output" ]]; then
75+
sed -E "s/^.*${tag}: //" <<< "$output"
76+
fi
77+
if ! grep -q "RESULT=PASS" <<< "$output"; then
78+
adb logcat -d -T "$since" -b crash | grep -A 20 "$appId" || true
79+
return 1
80+
fi
81+
}
82+
83+
run() {
84+
local apkDir
85+
apkDir="$(apkDirIn "$1")"
86+
local failed=0
87+
shopt -s nullglob
88+
local apks=("$apkDir"/*.apk)
89+
if [[ ${#apks[@]} -eq 0 ]]; then
90+
echo "No APKs found in $apkDir"
91+
exit 1
92+
fi
93+
94+
for apk in "${apks[@]}"; do
95+
local name
96+
name="$(basename "$apk" .apk)"
97+
echo "=== $name"
98+
if ! runApk "$apk"; then
99+
echo "FAILED: $name"
100+
failed=1
101+
fi
102+
done
103+
104+
if [[ $failed -ne 0 ]]; then
105+
echo "Nav3 Built-in Back Stack Mappers R8 check failed."
106+
exit 1
107+
fi
108+
echo "Nav3 Built-in Back Stack Mappers R8 check passed."
109+
}
110+
111+
case "${1:-}" in
112+
build) build "${2:?Usage: $0 build DIR}" ;;
113+
run) run "${2:?Usage: $0 run DIR}" ;;
114+
"")
115+
apkDir="${moduleDir}/build/r8-check-apks"
116+
build "$apkDir"
117+
run "$apkDir"
118+
;;
119+
*)
120+
echo "Usage: $0 [build DIR | run DIR]"
121+
exit 1
122+
;;
123+
esac
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# nav3-built-in-back-stack-mappers-r8-check
2+
3+
Checks that `BackStackEntryMapper.SerialName` works after R8 obfuscation:
4+
5+
- Serial names, `/unknown` results, and warning reasons match expectations.
6+
- The compatibility probe succeeds conclusively.
7+
- R8 actually obfuscates a representative class.
8+
9+
The app uses kotlinx.serialization's consumer rules and its own `proguard-rules.pro`; the SDK supplies no keep rules for app serialization
10+
classes.
11+
12+
## Run
13+
14+
Boot an emulator or device, then run:
15+
16+
```bash
17+
./scripts/test-nav3-built-in-back-stack-mappers-r8.sh
18+
```
19+
20+
Versions come from `gradle/dependency-matrix.versions.toml`. Both floor and latestStable run in R8 full mode; floor also runs in compat mode
21+
to cover older named-companion rules. Each APK must report `RESULT=PASS`.
22+
23+
CI also runs the module's unit tests against latestStable.
24+
25+
## Handle a failing update
26+
27+
Dependabot updates `kotlinxSerialization-latestStable` after a three-day cooldown.
28+
29+
**Compilation failure:** if the release requires newer Kotlin, leave the PR open, file a linked Kotlin upgrade issue, and merge after the
30+
upgrade and passing checks. Closing the PR tells Dependabot to ignore that version. The release remains untested here; apps with newer
31+
compilers may still work.
32+
33+
**Test failure:** investigate changes to serializer lookup, serial descriptors, or R8 rules. Fix incorrect names or unexpected `/unknown`
34+
results, then merge the fix with the update.
35+
36+
Preserve support for the floor in either case; see "Version floor" in the `kotlinx` package README.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Checks BackStackEntryMapper.SerialName in R8-minified release builds. See README.md.
2+
plugins {
3+
id("com.android.application")
4+
alias(libs.plugins.kotlin.android)
5+
alias(libs.plugins.kotlin.serialization)
6+
id("io.sentry.spotless")
7+
id("io.sentry.jdk-target")
8+
}
9+
10+
val sentryVersion = libs.versions.sentry.get()
11+
12+
// Defaults to the oldest supported version, which has the fewest R8 rules. The test script also
13+
// runs with -Pnav3CheckKotlinx=latestStable.
14+
val kotlinxVersion =
15+
when (val role = providers.gradleProperty("nav3CheckKotlinx").getOrElse("floor")) {
16+
"floor" -> matrixLibs.versions.kotlinxSerialization.floor.get()
17+
"latestStable" -> matrixLibs.versions.kotlinxSerialization.latestStable.get()
18+
else -> error("nav3CheckKotlinx must be floor or latestStable, but was $role.")
19+
}
20+
21+
// kotlinx.serialization 1.10.0+ supplies R8 rules for named companions, so naming must succeed
22+
// without app keep rules. Older releases may return the name or /unknown, depending on R8.
23+
val namedCompanionAlwaysNamed =
24+
kotlinxVersion.split('.', '-').let { parts ->
25+
val major = parts[0].toInt()
26+
val minor = parts.getOrNull(1)?.toIntOrNull() ?: 0
27+
major > 1 || (major == 1 && minor >= 10)
28+
}
29+
30+
android {
31+
namespace = "io.sentry.tests.nav3.r8"
32+
compileSdk = libs.versions.compileSdk.get().toInt()
33+
compileSdkMinor = libs.versions.compileSdkMinor.get().toInt()
34+
35+
defaultConfig {
36+
applicationId = "io.sentry.tests.nav3.r8"
37+
minSdk = libs.versions.minSdk.get().toInt()
38+
targetSdk = libs.versions.targetSdk.get().toInt()
39+
versionCode = 1
40+
versionName = "1.0"
41+
buildConfigField("boolean", "NAMED_COMPANION_ALWAYS_NAMED", "$namedCompanionAlwaysNamed")
42+
}
43+
44+
signingConfigs {
45+
getByName("debug") {
46+
storeFile = file("../../debug.keystore")
47+
storePassword = "android"
48+
keyAlias = "androiddebugkey"
49+
keyPassword = "android"
50+
}
51+
}
52+
53+
buildTypes {
54+
release {
55+
isMinifyEnabled = true
56+
proguardFiles(
57+
getDefaultProguardFile("proguard-android-optimize.txt"),
58+
"proguard-rules.pro",
59+
)
60+
signingConfig = signingConfigs.getByName("debug")
61+
}
62+
}
63+
64+
buildFeatures { buildConfig = true }
65+
66+
androidComponents.beforeVariants { it.enable = it.buildType == "release" }
67+
}
68+
69+
dependencies {
70+
implementation("io.sentry:sentry-android-navigation3:$sentryVersion")
71+
// Lets the check initialize Sentry with a logger that records the mapper's messages.
72+
implementation("io.sentry:sentry-android-core:$sentryVersion")
73+
// compileOnly in sentry-android-navigation3; always available for apps that use Nav3.
74+
implementation(libs.androidx.compose.runtime)
75+
76+
// Strict, so that no other dependency can raise the version the check runs against.
77+
implementation(matrixLibs.kotlinxSerialization.core) { version { strictly(kotlinxVersion) } }
78+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# kotlinx.serialization's named-companion rules for R8 full mode, which also work in compat mode.
2+
# Needed for the serialization floor (1.7.3); kotlinx.serialization 1.10.0+ supplies its own.
3+
# See https://docs.sentry.io/platforms/android/integrations/navigation/#troubleshooting-kotlinxserialization-mappers
4+
# https://github.com/Kotlin/kotlinx.serialization#android
5+
-keepattributes InnerClasses
6+
7+
-if @kotlinx.serialization.Serializable class
8+
io.sentry.tests.nav3.r8.NamedCompanionKeyWithRules
9+
{
10+
static **$* *;
11+
}
12+
-keepnames class <1>$$serializer {
13+
static <1>$$serializer INSTANCE;
14+
}
15+
16+
-keepclasseswithmembers, allowshrinking, allowobfuscation, allowaccessmodification class
17+
io.sentry.tests.nav3.r8.NamedCompanionKeyWithRules
18+
{
19+
*;
20+
}

0 commit comments

Comments
 (0)