Closed Bug 2037826 Opened 6 months ago Closed 6 months ago

Static final fields are now unmodifiable

Categories

(Firefox for Android :: Tooling, task)

All
Android
task

Tracking

()

RESOLVED FIXED

People

(Reporter: mcarare, Assigned: mcarare)

References

Details

(Whiteboard: [fxdroid][group6] )

https://developer.android.com/about/versions/17/behavior-changes-17#static-final-fields

Apps running on Android 17 or higher that target Android 17 (API level 37) or higher cannot change static final fields. If an app attempts to change a static final field by using reflection, it will cause an IllegalAccessException. Attempting to modify one of these fields through JNI APIs (such as SetStaticLongField()) will cause the app to crash.

On the Android/ GV side:

Production code does not modify any static final field via reflection or JNI. So we should not be affected by the Android 17 static final field restriction on modifying them.

We do read some fields, but the reflection usages I found are all safe for one or more of the following reasons:

  • the field is not static final.
  • only a read (get) is performed rather than a write (set),
  • API version guard prevents the code from running on Android 17 (API 37).

production code:

  • gecko/media/Sample.java — field CryptoInfo.pattern (private instance field, not static final; read-only via get(); code path only reachable on API <= 30 due to Build.VERSION.SDK_INT >= 31 guard)

  • geckoview/GeckoSession.java — field Configuration.windowConfiguration (private instance field; read-only via get())

  • geckoview/OverscrollEdgeEffect.java — field EdgeEffect.mPaint (private instance field; read-only via get(); code path only reachable on API <= 28 due to Build.VERSION.SDK_INT < 29 guard)

test code:

  • browser/engine-gecko/src/test/java/mozilla/components/test/ReflectionUtils.kt — sets instance fields on GeckoView's WebExtension and ExtensionMetaData objects used for mocking in tests (non-static, non-final fields)

  • feature/customtabs/CustomTabConfigHelperTest.kt — field Intent.mExtras (private instance field, not static final)

  • browser/engine/system/SystemEngineSessionTest.kt — only reads field modifiers via getDeclaredField to assert isVolatile; no set() call

  • lib/crash/CrashReporterTest.kt — only reads field modifiers via getDeclaredField to assert isVolatile; no set() call

  • service/mars/MozAdsClientProviderTest.kt, MozAdsUseCasesTest.kt, MacTopSitesProviderTest.kt — field MozAdsClientProvider.client (declared as private var, a mutable non-final instance field on a Kotlin object)

on the JNI side:

The codebase's architecture for JNI communication is "safe by design" against this specific restriction. No production logic relies on modifying static final fields via JNI or reflection.

JNI Annotation Processor (CodeGenerator.java): Lines 700–710 (generateField method) explicitly prevent the generation of setter wrappers for any field marked as final:

    if (isFinal) {
      return;
    }

This means that:

  • For static final primitive fields and String fields, the code generator emits a static constexpr or static const C++ constant, bypassing JNI field access entirely.
  • For static final non-primitive fields (e.g., objects), it generates only a getter (Field<T>::Get), never a setter.

The Field<T>::Set path (which calls StaticSet) is only generated for non-final static fields. And no non-genrated C++ code calls SetStatic<Type>Field directly on any Java field.

To err on the safe side, even though we don't run Robolectric on SDK 36 yet ( and 37 is long away) we should refactor tests that rely on Robolectric's ReflectionHelpers.setStaticField to mock Build.VERSION.SDK_INT. ( ticket as a follow=up on this).

Blocks: 2037911

ReflectionUtils.setField would be subject to these changes only if it were to be used for static final fields. As long as it is strictly used for instance final fields (as is currently the case), it will continue to work as expected. However, as Android continues to tighten reflection access, it is recommended to eventually move towards using constructors or mockable interfaces where possible, though this is often difficult with GeckoView's data-only objects. Opening a follow-up for removing it at some point.

Blocks: 2038949
No longer blocks: 2038949
See Also: → 2038949

This sounds like a fair analysis to me!
I went through the list too and couldn't see anything that would break under the new Android 17 rules.
probably you already did this, but i went and dug out the old source code for CryptoInfo to check the compat switching was safe, it is here in case it is any use to anyone else. We should be good in this case too as the field is private but not final.
Thanks for opening the follow up too. I agree it is somewhat alarming how much we rely on reflection for things like accessing private fields; i would prefer to use other alternatives here, as reflection can be super brittle. In some cases we are using reflection to access fields in our own code - there are plenty of easier to maintain alternatives here (eg. using interfaces to expose an API but implementations for testing, or the @VisibleForTesting annotation)

I will also probably open a meta to cover all reflection cases, besides the already existing follow-up.
Thank you for looking into this!

Status: ASSIGNED → RESOLVED
Closed: 6 months ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.