Skip to content

Commit c765b6c

Browse files
committed
test(nav3): Verify the SerialName mapper under R8
Add nav3-backstack-entry-mapper-r8-check, an app that maps representative back stack keys with BackStackEntryMapper.SerialName in an R8-minified release build and reports whether each name is right, and scripts/test-nav3-backstack-entry-mapper-r8.sh, which builds it against the kotlinx.serialization floor in R8 full and compat mode, and optionally other versions, 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.
1 parent 13ed74a commit c765b6c

11 files changed

Lines changed: 659 additions & 1 deletion

File tree

‎Makefile‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: all clean compile javadocs dryRelease update format checkFormat api assembleUiTestRelease assembleUiTestCriticalRelease runUiTestCritical check preMerge publish
1+
.PHONY: all clean compile javadocs dryRelease update format checkFormat api assembleUiTestRelease assembleUiTestCriticalRelease runUiTestCritical runNav3BackStackEntryMapperR8Test check preMerge publish
22

33
all: stop clean javadocs compile
44
assembleUiTests: assembleUiTestRelease
@@ -52,6 +52,10 @@ assembleUiTestCriticalRelease:
5252
runUiTestCritical:
5353
./scripts/test-ui-critical.sh
5454

55+
# Build and run the R8 check for BackStackEntryMapper.SerialName on a booted emulator
56+
runNav3BackStackEntryMapperR8Test:
57+
./scripts/test-nav3-backstack-entry-mapper-r8.sh
58+
5559
# Run tests and lint
5660
check:
5761
./gradlew check
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#!/usr/bin/env bash
2+
# Verifies the behavior of BackStackEntryMapper.SerialName in R8-minified builds on a booted
3+
# emulator or device. See
4+
# sentry-android-integration-tests/nav3-backstack-entry-mapper-r8-check/README.md.
5+
#
6+
# The app is built against kotlinxSerialization-floor in gradle/dependency-matrix.versions.toml, plus
7+
# each version in NAV3_EXTRA_KOTLINX_VERSIONS, in R8 full mode, and against the floor in R8 compat
8+
# mode too.
9+
#
10+
# Usage:
11+
# test-nav3-backstack-entry-mapper-r8.sh Build all APKs, then run them.
12+
# test-nav3-backstack-entry-mapper-r8.sh build DIR Build all APKs into DIR.
13+
# test-nav3-backstack-entry-mapper-r8.sh run DIR Install and run every APK built into DIR.
14+
#
15+
# The APKs go in a subdirectory of DIR that only this script writes to, so DIR may also hold
16+
# unrelated files, which neither command touches.
17+
set -euo pipefail
18+
19+
module="nav3-backstack-entry-mapper-r8-check"
20+
moduleDir="sentry-android-integration-tests/nav3-backstack-entry-mapper-r8-check"
21+
appId="io.sentry.tests.nav3.r8"
22+
tag="NAV3_R8_CHECK"
23+
24+
# Extra versions as "<role> <version>" lines. Set, e.g.,
25+
# NAV3_EXTRA_KOTLINX_VERSIONS="1.10.0 1.12.0" to test the floor, 1.10.0, and 1.12.0, all required.
26+
# Without it, only the floor is tested.
27+
extraKotlinxVersions=()
28+
for version in ${NAV3_EXTRA_KOTLINX_VERSIONS:-}; do
29+
extraKotlinxVersions+=("extra $version")
30+
done
31+
32+
copyApk() {
33+
cp "${moduleDir}/build/outputs/apk/release/nav3-backstack-entry-mapper-r8-check-release.apk" "$1"
34+
}
35+
36+
# Prints the subdirectory of DIR $1 that holds this script's APKs.
37+
apkDirIn() {
38+
echo "$1/${module}"
39+
}
40+
41+
build() {
42+
local outDir
43+
outDir="$(apkDirIn "$1")"
44+
mkdir -p "$outDir"
45+
# Removes APKs from earlier builds, which run() would otherwise run too (e.g., one for a release
46+
# candidate that's no longer tested). Only this script writes to outDir, so no other APKs are lost.
47+
rm -f "$outDir"/*.apk
48+
for mode in full compat; do
49+
local fullMode=true
50+
[[ "$mode" == "compat" ]] && fullMode=false
51+
echo "Building R8 $mode mode APK (kotlinx floor)..."
52+
./gradlew -p sentry-android-integration-tests ":${module}:assembleRelease" "-Pandroid.enableR8.fullMode=${fullMode}"
53+
copyApk "${outDir}/${mode}.apk"
54+
55+
# Compat mode keeps more than full mode, so it's only tested with the floor, where it changes
56+
# the outcome for named companion objects (kotlinx.serialization's own R8 rules only cover them
57+
# from 1.10.0 on).
58+
[[ "$mode" == "compat" ]] && continue
59+
60+
# Apps may ship a newer kotlinx than our compile floor, so rebuild against each.
61+
for entry in ${extraKotlinxVersions[@]+"${extraKotlinxVersions[@]}"}; do
62+
local role version
63+
read -r role version <<< "$entry"
64+
echo "Building R8 $mode mode APK (kotlinx ${version}, ${role})..."
65+
./gradlew -p sentry-android-integration-tests ":${module}:assembleRelease" \
66+
"-Pandroid.enableR8.fullMode=${fullMode}" "-Pnav3CheckKotlinxVersion=${version}"
67+
copyApk "${outDir}/${mode}-kotlinx-${version}.apk"
68+
done
69+
done
70+
}
71+
72+
# Installs and launches APK $1, prints what its check reports, and returns non-zero unless the check
73+
# passed. Failures to install or launch the APK return non-zero too, rather than exiting, so that
74+
# run() can apply the same advisory rule to them.
75+
runApk() {
76+
local apk="$1"
77+
adb uninstall "$appId" > /dev/null 2>&1 || true
78+
adb install "$apk" > /dev/null || return 1
79+
# Only read lines logged from this launch on, so a stale RESULT=PASS from the previous APK can't
80+
# hide this one's failure. Doesn't clear the log, which would also discard unrelated logs on a
81+
# development device. The timestamp is truncated to the second, so wait a second for it to
82+
# postdate earlier lines.
83+
local since
84+
sleep 1
85+
since="$(adb shell "date +'%m-%d %H:%M:%S.000'" | tr -d '\r')" || return 1
86+
adb shell am start -W -n "${appId}/.BackStackEntryMapperActivity" > /dev/null || return 1
87+
88+
local output=""
89+
for _ in $(seq 1 60); do
90+
output="$(adb logcat -d -T "$since" -s "${tag}:I" | grep "${tag}" || true)"
91+
if grep -q "RESULT=" <<< "$output"; then
92+
break
93+
fi
94+
if adb logcat -d -T "$since" -b crash | grep -q "$appId"; then
95+
break
96+
fi
97+
sleep 1
98+
done
99+
100+
if [[ -n "$output" ]]; then
101+
sed -E "s/^.*${tag}: //" <<< "$output"
102+
fi
103+
if ! grep -q "RESULT=PASS" <<< "$output"; then
104+
adb logcat -d -T "$since" -b crash | grep -A 20 "$appId" || true
105+
return 1
106+
fi
107+
}
108+
109+
run() {
110+
local apkDir
111+
apkDir="$(apkDirIn "$1")"
112+
local failed=0
113+
shopt -s nullglob
114+
local apks=("$apkDir"/*.apk)
115+
if [[ ${#apks[@]} -eq 0 ]]; then
116+
echo "No APKs found in $apkDir"
117+
exit 1
118+
fi
119+
120+
for apk in "${apks[@]}"; do
121+
local name
122+
name="$(basename "$apk" .apk)"
123+
echo "=== $name"
124+
if ! runApk "$apk"; then
125+
echo "FAILED: $name"
126+
failed=1
127+
fi
128+
done
129+
130+
if [[ $failed -ne 0 ]]; then
131+
echo "Nav3 BackStackEntryMapper R8 check failed."
132+
exit 1
133+
fi
134+
echo "Nav3 BackStackEntryMapper R8 check passed."
135+
}
136+
137+
case "${1:-}" in
138+
build) build "${2:?Usage: $0 build DIR}" ;;
139+
run) run "${2:?Usage: $0 run DIR}" ;;
140+
"")
141+
apkDir="${moduleDir}/build/r8-check-apks"
142+
build "$apkDir"
143+
run "$apkDir"
144+
;;
145+
*)
146+
echo "Usage: $0 [build DIR | run DIR]"
147+
exit 1
148+
;;
149+
esac
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# nav3-backstack-entry-mapper-r8-check
2+
3+
Verifies that `BackStackEntryMapper.SerialName` returns the expected names after R8 obfuscation. The SDK ships no keep rules for apps'
4+
kotlinx.serialization classes; this app uses the library's consumer rules and per-class rules in `proguard-rules.pro`.
5+
6+
The tests check serial names, including `/unknown` for keys the mapper can't name, which also shows that the mapper's compatibility probe
7+
passed: a failed probe records `/unknown` for every key. The app initializes Sentry with a logger that records the mapper's messages, and
8+
checks that the probe wasn't inconclusive and didn't fail (`ProbeLogChecks.kt`): an inconclusive name-lookup probe leaves names right but
9+
can't detect an incompatible kotlinx.serialization version in minified apps. It also checks that R8 actually renamed a representative
10+
class.
11+
12+
## How to run it
13+
14+
Run the tests against a booted emulator or device:
15+
16+
```bash
17+
./scripts/test-nav3-backstack-entry-mapper-r8.sh
18+
```
19+
20+
The script tests the kotlinx.serialization floor (`kotlinxSerialization-floor` in `gradle/dependency-matrix.versions.toml`) in R8 full and
21+
compat mode. Compat mode covers named-companion lookups with older consumer rules.
22+
23+
Each APK must report `RESULT=PASS`.
24+
25+
Set `NAV3_EXTRA_KOTLINX_VERSIONS="1.10.0 1.12.0"` to also test other versions, in R8 full mode.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 min version sentry-android-navigation3 supports, whose R8 rules are the most
13+
// limited. scripts/test-nav3-backstack-entry-mapper-r8.sh overrides it with
14+
// -Pnav3CheckKotlinxVersion=<version> to test newer versions.
15+
val kotlinxVersionOverride = providers.gradleProperty("nav3CheckKotlinxVersion").orNull
16+
val kotlinxFloor = matrixLibs.versions.kotlinxSerialization.floor.get()
17+
val kotlinxVersion = kotlinxVersionOverride ?: kotlinxFloor
18+
19+
// Whether the mapper can name a @Serializable class with a named companion object and no
20+
// named-companion R8 rules of the app's own: "named", "unknown", or "either".
21+
// - kotlinx.serialization's own R8 rules keep what its lookup needs from 1.10.0 on.
22+
// - The floor's rules don't, in R8 full and compat mode alike (verified on a device), so the floor
23+
// must record /unknown. Asserting it means a change in that behavior shows up here.
24+
// - Between the two, it depends on the version and R8 mode (e.g., 1.9.x's lookup succeeds in
25+
// compat mode only), so either outcome is accepted.
26+
val namedCompanionOutcome =
27+
kotlinxVersion.split('.', '-').let { parts ->
28+
val major = parts[0].toInt()
29+
val minor = parts.getOrNull(1)?.toIntOrNull() ?: 0
30+
when {
31+
major > 1 || (major == 1 && minor >= 10) -> "named"
32+
kotlinxVersion == kotlinxFloor -> "unknown"
33+
else -> "either"
34+
}
35+
}
36+
37+
android {
38+
namespace = "io.sentry.tests.nav3.r8"
39+
compileSdk = libs.versions.compileSdk.get().toInt()
40+
compileSdkMinor = libs.versions.compileSdkMinor.get().toInt()
41+
42+
defaultConfig {
43+
applicationId = "io.sentry.tests.nav3.r8"
44+
minSdk = libs.versions.minSdk.get().toInt()
45+
targetSdk = libs.versions.targetSdk.get().toInt()
46+
versionCode = 1
47+
versionName = "1.0"
48+
buildConfigField("String", "NAMED_COMPANION_OUTCOME", "\"$namedCompanionOutcome\"")
49+
}
50+
51+
signingConfigs {
52+
getByName("debug") {
53+
storeFile = file("../../debug.keystore")
54+
storePassword = "android"
55+
keyAlias = "androiddebugkey"
56+
keyPassword = "android"
57+
}
58+
}
59+
60+
buildTypes {
61+
release {
62+
isMinifyEnabled = true
63+
proguardFiles(
64+
getDefaultProguardFile("proguard-android-optimize.txt"),
65+
"proguard-rules.pro",
66+
)
67+
signingConfig = signingConfigs.getByName("debug")
68+
}
69+
}
70+
71+
buildFeatures { buildConfig = true }
72+
73+
androidComponents.beforeVariants { it.enable = it.buildType == "release" }
74+
}
75+
76+
dependencies {
77+
implementation("io.sentry:sentry-android-navigation3:$sentryVersion")
78+
// Lets the check initialize Sentry with a logger that records the mapper's messages.
79+
implementation("io.sentry:sentry-android-core:$sentryVersion")
80+
// compileOnly in sentry-android-navigation3; always available for apps that use Nav3.
81+
implementation(libs.androidx.compose.runtime)
82+
83+
// Strict, so that no other dependency can raise the version the check runs against.
84+
implementation(matrixLibs.kotlinxSerialization.core) { version { strictly(kotlinxVersion) } }
85+
}
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+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
<application android:label="Nav3 BackStackEntryMapper R8 check">
4+
<!-- The Activity initializes Sentry itself, with a logger that records the mapper's messages. -->
5+
<meta-data android:name="io.sentry.auto-init" android:value="false" />
6+
<activity android:name=".BackStackEntryMapperActivity" android:exported="true">
7+
<intent-filter>
8+
<action android:name="android.intent.action.MAIN" />
9+
<category android:name="android.intent.category.LAUNCHER" />
10+
</intent-filter>
11+
</activity>
12+
</application>
13+
</manifest>

0 commit comments

Comments
 (0)