Skip to content

Commit cc83d6b

Browse files
runningcodeclaude
andauthored
build(android): Run Error Prone and NullAway on the Android modules (#6493)
* build(android): Run Error Prone and NullAway on the Android modules net.ltgt.errorprone dropped Android support in 3.0.0, which we have used since 6.7. Since then it only enables Error Prone and adds it to the processor path for Gradle's JVM source sets. On sentry-android-core and sentry-uitest-android the errorprone dependencies and the NullAway ERROR setting had no effect, and the Java compile tasks ran plain javac. Use the AGP variant API to add the errorprone configuration to each variant's annotation processor configuration and enable Error Prone on its JavaCompile task. Both APIs exist in AGP 9.0.0, the oldest version in the AGP matrix. Pass -XDandroidCompatible=true so Error Prone treats these modules as Android code: it skips checks that don't apply there, such as DefaultCharset, and runs its Android checks. sentry-uitest-android has no hand-written Java, so exclude the view binding classes AGP generates, which trip LabelledBreakTarget. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(android): Resolve NullAway findings in AndroidConnectionStatusProvider NullAway treats AtomicReference.get() as nullable, so the snapshot reads added in #6537 fail once NullAway runs on sentry-android-core. The snapshot is never set to null, so read it through one accessor that suppresses the finding. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 16e51ee commit cc83d6b

3 files changed

Lines changed: 43 additions & 17 deletions

File tree

‎sentry-android-core/build.gradle.kts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,19 @@ android {
7474
}
7575
}
7676

77-
tasks.withType<JavaCompile>().configureEach {
78-
options.errorprone {
79-
check("NullAway", net.ltgt.gradle.errorprone.CheckSeverity.ERROR)
80-
option("NullAway:AnnotatedPackages", "io.sentry")
77+
// net.ltgt.errorprone only configures Gradle's JVM source sets, not Android variants, so put Error
78+
// Prone on each variant's processor path and enable it ourselves.
79+
androidComponents.onVariants { variant ->
80+
variant.annotationProcessorConfiguration.extendsFrom(configurations.errorprone.get())
81+
variant.configureJavaCompileTask { task ->
82+
// Tells Error Prone this is Android code, so it skips checks that don't apply there, such as
83+
// DefaultCharset (Android's default charset is always UTF-8), and runs its Android checks.
84+
task.options.compilerArgs.add("-XDandroidCompatible=true")
85+
task.options.errorprone {
86+
enabled = true
87+
check("NullAway", net.ltgt.gradle.errorprone.CheckSeverity.ERROR)
88+
option("NullAway:AnnotatedPackages", "io.sentry")
89+
}
8190
}
8291
}
8392

‎sentry-android-core/src/main/java/io/sentry/android/core/internal/util/AndroidConnectionStatusProvider.java‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ private void ensureNetworkCallbackRegistered() {
324324
public void onAvailable(final @NotNull Network network) {
325325
@NotNull ConnectionSnapshot current;
326326
do {
327-
current = snapshot.get();
327+
current = currentSnapshot();
328328
} while (!snapshot.compareAndSet(current, current.withNetwork(network)));
329329

330330
// Only forward the call on first registration + when the connection is
@@ -348,7 +348,7 @@ public void onUnavailable() {
348348

349349
@Override
350350
public void onLost(final @NotNull Network network) {
351-
if (!network.equals(snapshot.get().network)) {
351+
if (!network.equals(currentSnapshot().network)) {
352352
return;
353353
}
354354
clearCacheAndNotifyObservers();
@@ -376,7 +376,7 @@ private void clearCacheAndNotifyObservers() {
376376
@Override
377377
public void onCapabilitiesChanged(
378378
@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
379-
if (!network.equals(snapshot.get().network)) {
379+
if (!network.equals(currentSnapshot().network)) {
380380
return;
381381
}
382382
updateCacheAndNotifyObservers(networkCapabilities);
@@ -397,7 +397,7 @@ private void updateCacheAndNotifyObservers(
397397
@NotNull ConnectionSnapshot current;
398398
@NotNull ConnectionSnapshot updated;
399399
do {
400-
current = snapshot.get();
400+
current = currentSnapshot();
401401
if (!isSignificantChange(current.capabilities, networkCapabilities)) {
402402
return;
403403
}
@@ -482,9 +482,15 @@ private boolean hasSignificantTransportChanges(
482482
}
483483
}
484484

485+
// NullAway treats AtomicReference.get() as nullable, but the snapshot is never set to null.
486+
@SuppressWarnings("NullAway")
487+
private @NotNull ConnectionSnapshot currentSnapshot() {
488+
return snapshot.get();
489+
}
490+
485491
/** The current snapshot, refreshed first if it's stale. */
486492
private @NotNull ConnectionSnapshot freshSnapshot() {
487-
final @NotNull ConnectionSnapshot current = snapshot.get();
493+
final @NotNull ConnectionSnapshot current = currentSnapshot();
488494
return current.isFresh() ? current : refresh(current);
489495
}
490496

@@ -510,7 +516,7 @@ private boolean hasSignificantTransportChanges(
510516
if (snapshot.compareAndSet(current, refreshed)) {
511517
break;
512518
}
513-
current = snapshot.get();
519+
current = currentSnapshot();
514520
if (current.isFresh()) {
515521
return current;
516522
}
@@ -651,7 +657,7 @@ public void onForeground() {
651657

652658
// proactively update cache and notify observers on foreground to ensure connectivity
653659
// state is not stale
654-
final @NotNull ConnectionStatus status = statusOf(refresh(snapshot.get()));
660+
final @NotNull ConnectionStatus status = statusOf(refresh(currentSnapshot()));
655661
if (status == ConnectionStatus.DISCONNECTED) {
656662
// onLost is not called retroactively when we registerNetworkCallback (as opposed to
657663
// onAvailable), so we have to do it manually for the DISCONNECTED case
@@ -699,7 +705,7 @@ public void onBackground() {
699705
@TestOnly
700706
@Nullable
701707
public NetworkCapabilities getCachedNetworkCapabilities() {
702-
return snapshot.get().capabilities;
708+
return currentSnapshot().capabilities;
703709
}
704710

705711
/**

‎sentry-android-integration-tests/sentry-uitest-android/build.gradle.kts‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,22 @@ dependencies {
135135
androidTestImplementation(libs.okhttp.mockwebserver4)
136136
}
137137

138-
tasks.withType<JavaCompile>().configureEach {
139-
options.errorprone {
140-
check("NullAway", net.ltgt.gradle.errorprone.CheckSeverity.ERROR)
141-
option("NullAway:AnnotatedPackages", "io.sentry")
142-
option("NullAway:UnannotatedSubPackages", "io.sentry.uitest.android.databinding")
138+
// net.ltgt.errorprone only configures Gradle's JVM source sets, not Android variants, so put Error
139+
// Prone on each variant's processor path and enable it ourselves.
140+
androidComponents.onVariants { variant ->
141+
variant.annotationProcessorConfiguration.extendsFrom(configurations.errorprone.get())
142+
variant.configureJavaCompileTask { task ->
143+
// Tells Error Prone this is Android code, so it skips checks that don't apply there, such as
144+
// DefaultCharset (Android's default charset is always UTF-8), and runs its Android checks.
145+
task.options.compilerArgs.add("-XDandroidCompatible=true")
146+
task.options.errorprone {
147+
enabled = true
148+
check("NullAway", net.ltgt.gradle.errorprone.CheckSeverity.ERROR)
149+
option("NullAway:AnnotatedPackages", "io.sentry")
150+
option("NullAway:UnannotatedSubPackages", "io.sentry.uitest.android.databinding")
151+
// AGP generates the view binding classes, so their findings can't be fixed here.
152+
excludedPaths.set(".*/build/generated/.*")
153+
}
143154
}
144155
}
145156

0 commit comments

Comments
 (0)