Repository navigation
Description
Android framework version
net11.0-android (Preview)
Affected platform version
.NET 11.0.100-rc2
Description
It seems like public/protected fields in Java classes and enums, which managed code is accessing via bingings, are not protected by the generated proguard rules. This issue seems to be related to the optimization from #12270, although most likely not caused by it.
Here are details that we discovered in our project, which is using native Facebook SDK binding and Xamarin.AndroidX.Media3.ExoPlayer nuget. During the build the following rules are generated in proguard_project_references.cfg:
-keep class com.facebook.LoggingBehavior
-keepclassmembers class com.facebook.LoggingBehavior {
}
-keep class androidx.media3.exoplayer.ExoPlaybackException
-keepclassmembers class androidx.media3.exoplayer.ExoPlaybackException {
}
com.facebook.LoggingBehavior is actually an enum, which values we are using via bindings, specifically
FacebookSdk.AddLoggingBehavior(LoggingBehavior.AppEvents);
FacebookSdk.AddLoggingBehavior(LoggingBehavior.Cache);
FacebookSdk.AddLoggingBehavior(LoggingBehavior.DeveloperErrors);
FacebookSdk.AddLoggingBehavior(LoggingBehavior.GraphApiDebugInfo);
FacebookSdk.AddLoggingBehavior(LoggingBehavior.GraphApiDebugWarning);
FacebookSdk.AddLoggingBehavior(LoggingBehavior.IncludeAccessTokens);
FacebookSdk.AddLoggingBehavior(LoggingBehavior.IncludeRawResponses);
FacebookSdk.AddLoggingBehavior(LoggingBehavior.Requests);
But because none of the enum values are protected by the generated rules, and with optimization enabled R8 decided to remove one of the values (seems like the only value with no explicit internal usage in Facebook SDK, at least after trimming). In the R8 "print usage" output there's the following record:
com.facebook.LoggingBehavior:
public static final enum com.facebook.LoggingBehavior CACHE
The case with androidx.media3.exoplayer.ExoPlaybackException is a bit different. We are using it's public Type field of type int like that:
if (exception is ExoPlaybackException error)
{
switch (error.Type)
{
...
}
}
In this case R8 does not remove the field completely, but performs another type of optimization (extracted from the proguard.map):
androidx.media3.exoplayer.ExoPlaybackException -> androidx.media3.exoplayer.ExoPlaybackException:
# {"id":"sourceFile","fileName":"ExoPlaybackException.java"}
boolean isRecoverable -> g
int type -> type
# {"id":"com.android.tools.r8.residualsignature","signature":"B"}
So the type of the field was optimized to be byte instead of int in DEX, while the binding code is still expecting int:
public int Type {
get {
return _members.InstanceFields.GetInt32Value ("type.I", this);
}
}
The issue probably become noticeable only after enabling R8 optimizations. We double-checked that with older .NET 10.0.200 same public fields are also not protected by default:
-keep class com.facebook.LoggingBehavior
-keepclassmembers class com.facebook.LoggingBehavior {
*** valueOf(...);
*** values(...);
}
-keep class androidx.media3.exoplayer.ExoPlaybackException
-keepclassmembers class androidx.media3.exoplayer.ExoPlaybackException {
*** getRendererException(...);
*** getSourceException(...);
*** getUnexpectedException(...);
*** createForRemote(...);
*** createForRenderer(...);
*** createForRenderer(...);
*** createForSource(...);
*** createForUnexpected(...);
*** createForUnexpected(...);
}
Steps to Reproduce
Seems like the issue can be reproduced on any .NET and Android workload version, but may lead to runtime crashes only in setup with R8 optimization enabled (11.0.100-rc.2/10.0.402)
- Android project with
TrimMode=fullandAndroidLinkTool=r8 - Access any native public field (like enum value) via binding
- Build project and check generated
proguard_project_references.cfg
Did you find any workaround?
Custom proguard rules file to protect public fields and enums:
-keepclassmembers enum * {
*;
}
-keepclassmembers class * {
public protected !static <fields>;
public protected static !final <fields>;
public protected static final ** *;
public protected static final **[] *;
}
Activity
test(android): Preserve AccessibilityNodeInfoCompat.setChecked()
test(android): Preserve AccessibilityNodeInfoCompat.setChecked()
[release/11.0.1xx] [R8] Preserve bound Java fields in generated keep …
[tests] Cover R8 bound Java field preservation (#13248)
test(android): Preserve AccessibilityNodeInfoCompat.setChecked()
AndroidR8ObfuscationMode=private-members: generated keep rules omit bound Java fields →NoSuchFieldErrorat runtime #13266 as a duplicate of this issuetest(android): Preserve AccessibilityNodeInfoCompat.setChecked()
seanrodda commentedon Nov 20, 2026
Is a backport of #13239 to
release/10.0.1xxplanned?AndroidR8ObfuscationMode=private-membersshipped to .NET 10 in 36.1.118 (#12752) without this fix, so .NET 10 apps hit it too. A freshdotnet new mauiapp (MAUI 10.0.110, SDK 10.0.404) that callsSecureStorage.Default.SetAsynccrashes withNoSuchFieldErroronAES256_SIVwhen built with R8 andprivate-members.[release/10.0.1xx] [R8] Preserve bound Java fields (#13740)