Skip to content

Before Android12: "NoSuchFieldError: No static field MODULE" from Guava's class-level @NullMarked annotations with Truth 1.4.5+ #1608

Open

Description

@johnjohndoe

TL'DR

I noticed that connectedDebugAndroidTest fails when I update truth from 1.4.4 to 1.4.5.

Environment

  • API 26
  • GitHub Action

Workflow

Job

Error

> Task :database:connectedDebugAndroidTest
Starting 9 tests on emulator-5554 - 8.0.0
com.google.common.collect.ObjectCountHashMap > initializationError[emulator-5554 - 8.0.0] FAILED 
	java.lang.NoSuchFieldError: No static field MODULE of type Ljava/lang/annotation/ElementType; in class Ljava/lang/annotation/ElementType; or its superclasses (declaration of 'java.lang.annotation.ElementType' appears in /system/framework/core-oj.jar)
	at java.lang.Class.getDeclaredAnnotations(Native Method)
com.google.common.collect.ObjectCountLinkedHashMap > initializationError[emulator-5554 - 8.0.0] FAILED 
	java.lang.NoSuchFieldError: No static field MODULE of type Ljava/lang/annotation/ElementType; in class Ljava/lang/annotation/ElementType; or its superclasses (declaration of 'java.lang.annotation.ElementType' appears in /system/framework/core-oj.jar)
	at java.lang.Class.getDeclaredAnnotations(Native Method)
com.google.common.util.concurrent.ImmediateFuture > initializationError[emulator-5554 - 8.0.0] FAILED 
	java.lang.NoSuchFieldError: No static field MODULE of type Ljava/lang/annotation/ElementType; in class Ljava/lang/annotation/ElementType; or its superclasses (declaration of 'java.lang.annotation.ElementType' appears in /system/framework/core-oj.jar)
	at java.lang.Class.getDeclaredAnnotations(Native Method)
com.google.common.util.concurrent.ListenableFutureTask > initializationError[emulator-5554 - 8.0.0] FAILED 
	java.lang.NoSuchFieldError: No static field MODULE of type Ljava/lang/annotation/ElementType; in class Ljava/lang/annotation/ElementType; or its superclasses (declaration of 'java.lang.annotation.ElementType' appears in /system/framework/core-oj.jar)
	at java.lang.Class.getDeclaredAnnotations(Native Method)
Tests on emulator-5554 - 8.0.0 failed: There was 4 failure(s).
Finished 9 tests on emulator-5554 - 8.0.0
> Task :database:connectedDebugAndroidTest FAILED

Activity

  1. cpovirk commented on Oct 7, 2025

    @cpovirk
    Member

    Sorry, I wonder if we broke this when adding a class-level NullMarked annotation again in 0e4d97b#diff-d80853e620649805aca4f2cecfdffdb32e87ac0654dd4626bf0fb5ed114181f2R44. I'll have a look. Thanks for reporting.

  2. johnjohndoe commented on Oct 7, 2025

    @johnjohndoe
    Author

    Thank you for your quick response. - No worries. I can wait with a new version.

    Feel free to copy my simple CI setup to avoid future regressions,

  3. cpovirk commented on Oct 7, 2025

    @cpovirk
    Member

    Oops, the error messages are citing classes from Guava. That makes sense, since the bump from 1.4.4 to 1.4.5 picked up the @NullMarked annotations on Guava classes from Guava 33.4.1.

    After a different, less severe problem with class-level @NullMarked annotations in Truth itself, we switched to using @NullMarked at the package level (which isn't normally my first choice, since package-level annotations have some downsides) in Truth, maybe sprinkling a few class-level @NullUnmarked annotations (which don't trigger the same problem because NullUnmarked doesn't have @Target(MODULE)) to compensate.

    I would have hoped that I would have given similar thought to avoiding class-level annotations in Guava, but I don't as clear a memory of that now as I'd like. My guess would be that I figured we were safe after google/error-prone#4461 and google/error-prone#4462: Those PRs made it possible to use class-level @NullMarked annotations in Truth again without triggering the problem we'd seen reported.

    Naturally, changes to Error Prone can't save us from Android runtime errors.

    I got your setup running with an Android-35 emulator, which was the first thing I tried because I'd never run an Android emulator outside the confines of the Google-internal build system. That doesn't reproduce the error, presumably because ElementType.MODULE was eventually added to Android. I'm going to try again with Android 26 to match your tests. I expect that to reproduce the error. My hope is to figure out where the call to getDeclaredAnnotations is coming from to see if perhaps we can avoid that. The more thorough approach would be to update Guava's usage of @NullMarked to match Truth's package-level approach, but I'm hoping to at least be able to postpone that.

  4. cpovirk commented on Oct 7, 2025

    @cpovirk
    Member
    java.lang.NoSuchFieldError: No static field MODULE of type Ljava/lang/annotation/ElementType; in class Ljava/lang/annotation/ElementType; or its superclasses (declaration of 'java.lang.annotation.ElementType' appears in /system/framework/core-oj.jar)
    at java.lang.Class.getDeclaredAnnotations(Native Method)
    at org.junit.platform.commons.util.AnnotationUtils.findAnnotation(AnnotationUtils.java:156)
    at org.junit.platform.commons.util.AnnotationUtils.findMetaAnnotation(AnnotationUtils.java:198)
    at org.junit.platform.commons.util.AnnotationUtils.findAnnotation(AnnotationUtils.java:156)
    at org.junit.platform.commons.util.AnnotationUtils.findAnnotation(AnnotationUtils.java:137)
    at de.mannodermaus.junit5.internal.runners.AndroidJUnitPlatformTestTree.getSuiteDisplayName(AndroidJUnitPlatformTestTree.kt:90)
    at de.mannodermaus.junit5.internal.runners.AndroidJUnitPlatformTestTree.generateSuiteDescription(AndroidJUnitPlatformTestTree.kt:84)
    at de.mannodermaus.junit5.internal.runners.AndroidJUnitPlatformTestTree.<init>(AndroidJUnitPlatformTestTree.kt:36)
    at de.mannodermaus.junit5.internal.runners.AndroidJUnit5.generateTestTree(AndroidJUnit5.kt:74)
    at de.mannodermaus.junit5.internal.runners.AndroidJUnit5.access$generateTestTree(AndroidJUnit5.kt:23)
    at de.mannodermaus.junit5.internal.runners.AndroidJUnit5$testTree$2.invoke(AndroidJUnit5.kt:30)
    at de.mannodermaus.junit5.internal.runners.AndroidJUnit5$testTree$2.invoke(AndroidJUnit5.kt:30)
    at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:86)
    at de.mannodermaus.junit5.internal.runners.AndroidJUnit5.getTestTree(AndroidJUnit5.kt:30)
    at de.mannodermaus.junit5.internal.runners.AndroidJUnit5.getDescription(AndroidJUnit5.kt:32)
    at de.mannodermaus.junit5.internal.runners.JUnit5RunnerFactoryKt.hasExecutableTests(JUnit5RunnerFactory.kt:32)
    at de.mannodermaus.junit5.internal.runners.JUnit5RunnerFactoryKt.tryCreateJUnit5Runner(JUnit5RunnerFactory.kt:28)
    at de.mannodermaus.junit5.AndroidJUnit5Builder.runnerForClass(AndroidJUnit5Builder.kt:70)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70)
    at androidx.test.internal.runner.AndroidRunnerBuilder.runnerForClass(AndroidRunnerBuilder.java:146)
    at androidx.test.internal.runner.ScanningTestLoader.doCreateRunner(ScanningTestLoader.java:50)
    at androidx.test.internal.runner.TestLoader.getRunnersFor(TestLoader.java:64)
    at androidx.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:842)
    at androidx.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:663)
    at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:437)
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2074)
    

    It seems as if maybe https://github.com/mannodermaus/android-junit5 is trying to run some Guava classes (e.g., ObjectCountHashMap) as tests?

  5. cpovirk commented on Oct 7, 2025

    @cpovirk
    Member

    Gemini suggested I could filter the classes to check for test-ness with junit5.discovery.includeClassNamePatterns. Here's what I got a passing run with:

    diff --git a/app/build.gradle b/app/build.gradle
    index c9fd12bb..05da0c3f 100644
    --- a/app/build.gradle
    +++ b/app/build.gradle
    @@ -29,7 +29,10 @@ android {
             vectorDrawables.useSupportLibrary = true // allows using fillColor, fillType, strokeColor functionalities below Android 7.0 (API 24)
     
             testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    -        testInstrumentationRunnerArguments runnerBuilder: "de.mannodermaus.junit5.AndroidJUnit5Builder"
    +        testInstrumentationRunnerArguments(
    +            'runnerbuilder': 'de.mannodermaus.junit5.AndroidJUnit5Builder',
    +            'junit5.discovery.includeClassNamePatterns': '.*Test[s]?$'
    +        )
     
             // Build information
             resValue("string", "modification_time", "\"${modificationTime()}\"")
    diff --git a/database/build.gradle b/database/build.gradle
    index a3aba5af..a092d1df 100644
    --- a/database/build.gradle
    +++ b/database/build.gradle
    @@ -15,7 +15,10 @@ android {
             minSdk Android.minSdkVersion
             targetSdk Android.targetSdkVersion
             testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    -        testInstrumentationRunnerArguments runnerBuilder: "de.mannodermaus.junit5.AndroidJUnit5Builder"
    +        testInstrumentationRunnerArguments(
    +            'runnerbuilder': 'de.mannodermaus.junit5.AndroidJUnit5Builder',
    +            'junit5.discovery.includeClassNamePatterns': '.*Test[s]?$'
    +        )
         }
     
         compileOptions {

    I have no idea if that filter actually lets the build run the tests that you want; it could have filtered out everything AFAICT :) I would hope that it's OK. Another variant of the approach would be to filter to info.metadude. Either approach might even make tests run a little faster, since the runner can do less scanning.

    I should still figure out what to do about the broader problem: I've probably someday said something along the lines of "I'd hope that no Android code is reflecting on Guava," and now I have evidence that it happens—thankfully only in tests so far, but it's still an existence proof.

  6. changed the title [-]initializationError with truth 1.4.5[/-] [+]Before Android12: "NoSuchFieldError: No static field MODULE" from Guava's class-level `@NullMarked` annotations with Truth 1.4.5+[/+] on Oct 7, 2025
  7. added
    type=defect
    P3
    on Oct 7, 2025
  8. johnjohndoe commented on Oct 7, 2025

    @johnjohndoe
    Author

    I have no idea if that filter actually lets the build run the tests that you want; it could have filtered out everything AFAICT :) I would hope that it's OK.

    The easiest way to find out if the name pattern works, is to let a test fail on purpose in a temporary test run.

  9. cpovirk commented on Oct 8, 2025

    @cpovirk
    Member

    Ah, yes, I should have at least tried that to rule out the "filtered out everything" case, thanks.

    I had another belated realization: Reflecting on Guava classes probably works fine: The problem here happens because the test runner reflects on JSpecify classes so that it can check whether they are meta-annotated with (I assume) some test-related annotations. In other words, it sees @NullMarked on a class like ObjectCountHashMap, and it thinks: "Hmm, maybe @NullMarked is declared to be a custom testing annotation. To check, I'll read the annotations on NullMarked itself."

    This suggests a few things:

    • As I said earlier in this post: "Typical" reflection on Guava classes is actually still fine, even under Android. I think.
    • The test library that's triggering this could perhaps be changed to recognize NullMarked as a "poison" annotation, one that is known not to be a custom test annotation—and one that is better to not even ask about.
      • Alternatively/additionally, it could perhaps be made to catch NoSuchFieldError so as to also help with any similar annotations that might be declared by other artifacts. But catching errors is pretty gross. As just one point along those lines: I know that Android logs when it encounters at least some kinds of class-loading-related problems, even before throwing the error that we might subsequently swallow.
    • This is another potential downside to the pattern of "meta-annotations." That is of interest to us in the context of JSpecify.
  10. cpovirk commented on Oct 8, 2025

    @cpovirk
    Member

    Oh: The check for meta-annotations is happening in JUnit 5 proper. That can't hurt our chances to get a workaround in, since JUnit is a user of JSpecify :) (On the other hand, I'm not sure how much JUnit 5.x will change now that 6.0.0 has been released. And I don't know if the Android library can pick up 6.0.0.)

    Come to think of it: Does JUnit 5 (not 6) support Java 8? [edit: yes] If so, I would expect for Java 8 users to run into the same kind of problem as Android users. Maybe vanilla JUnit filters out clear non-tests earlier in the process? Or maybe the problem really can come up but doesn't in practice because anyone modern enough to use JUnit 5 isn't on Java 8 anymore?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3type=defect

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions