Skip to content

[R8] [Optimization] Public fields in bindings are not protected by proguard rules #13159

Open

Description

@TimBurik

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)

  1. Android project with TrimMode=full and AndroidLinkTool=r8
  2. Access any native public field (like enum value) via binding
  3. 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 **[] *;
}

Relevant log output

Activity

  1. added
    Area: App+Library Build
    needs-triage
    on Oct 15, 2026
  2. added 2 commits that reference this issue on Oct 23, 2026
    707d5dc
    e72b270
  3. removed
    needs-triage
    on Nov 19, 2026
  4. seanrodda commented on Nov 20, 2026

    @seanrodda

    Is a backport of #13239 to release/10.0.1xx planned? AndroidR8ObfuscationMode=private-members shipped to .NET 10 in 36.1.118 (#12752) without this fix, so .NET 10 apps hit it too. A fresh dotnet new maui app (MAUI 10.0.110, SDK 10.0.404) that calls SecureStorage.Default.SetAsync crashes with NoSuchFieldError on AES256_SIV when built with R8 and private-members.

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

    Area: App+Library Build

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions