Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[generator] restore [RegisterAttribute] for *Implementor types #1149

Merged

Conversation

jonathanpeppers
Copy link
Member

Fixes: dotnet/android#8337

A customer's app with the code:

SetContentView(Resource.Layout.activity_main);
FindViewById<Button>(Resource.Id.asd).Click += MainActivity_Click;

Crashes with -c Release -p:AndroidLinkMode=r8 with:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.companyname.New_folder/crc64abe8cc9139195b67.MainActivity}: java.lang.ClassNotFoundException: android.view.View_IOnClickListenerImplementor
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3644)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3781)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:138)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2306)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:7918)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Caused by: java.lang.ClassNotFoundException: android.view.View_IOnClickListenerImplementor
    at crc64abe8cc9139195b67.MainActivity.n_onCreate(Native Method)
    at crc64abe8cc9139195b67.MainActivity.onCreate(MainActivity.java:30)
    at android.app.Activity.performCreate(Activity.java:8342)
    at android.app.Activity.performCreate(Activity.java:8321)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1417)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3625)
    ... 12 more

Which, can be solved by adding your own proguard rules like:

-keep class android.view.View_IOnClickListenerImplementor { *; }

In a91ae7f, we thought (incorrectly):

Additionally, stop emitting the [Register] attribute for
*Implementor classes:

[global::Android.Runtime.Register ("mono/android/view/View_OnFocusChangeListenerImplementor")]
partial class IOnFocusChangeListenerImplementor {/* … */}

The [Register] attribute is not needed, because *Implementor
classes are generated internal implementation details.

proguard_xamarin.cfg has the entry:

-keep class mono.android.** { *; <init>(...); }

So we could do android.**, but that would certainly preserve way too much!

For now, let's just restore [Register] to revisit this issue at a later date -- maybe .NET 9?

Fixes: dotnet/android#8337

A customer's app with the code:

    SetContentView(Resource.Layout.activity_main);
    FindViewById<Button>(Resource.Id.asd).Click += MainActivity_Click;

Crashes with `-c Release -p:AndroidLinkMode=r8` with:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.companyname.New_folder/crc64abe8cc9139195b67.MainActivity}: java.lang.ClassNotFoundException: android.view.View_IOnClickListenerImplementor
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3644)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3781)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:138)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2306)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7918)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
    Caused by: java.lang.ClassNotFoundException: android.view.View_IOnClickListenerImplementor
        at crc64abe8cc9139195b67.MainActivity.n_onCreate(Native Method)
        at crc64abe8cc9139195b67.MainActivity.onCreate(MainActivity.java:30)
        at android.app.Activity.performCreate(Activity.java:8342)
        at android.app.Activity.performCreate(Activity.java:8321)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1417)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3625)
        ... 12 more

Which, can be solved by adding your own `proguard` rules like:

    -keep class android.view.View_IOnClickListenerImplementor { *; }

In a91ae7f, we thought (incorrectly):

> Additionally, stop emitting the `[Register]` attribute for
> `*Implementor` classes:
>
>    [global::Android.Runtime.Register ("mono/android/view/View_OnFocusChangeListenerImplementor")]
>    partial class IOnFocusChangeListenerImplementor {/* … */}
>
> The `[Register]` attribute is not needed, because `*Implementor`
> classes are generated internal implementation details.

`proguard_xamarin.cfg` has the entry:

    -keep class mono.android.** { *; <init>(...); }

So we could do `android.**`, but that would certainly preserve way too
much!

For now, let's just restore `[Register]` to revisit this issue at a
later date -- maybe .NET 9?
@jonathanpeppers jonathanpeppers force-pushed the TheReturnOfTheRegisterAttribute branch from 55fdc0e to ca40f3a Compare October 9, 2023 21:39
jonathanpeppers added a commit to jonathanpeppers/xamarin-android that referenced this pull request Oct 9, 2023
@jonathanpeppers
Copy link
Member Author

Added a test at: dotnet/android#8407

@jonpryor jonpryor merged commit ed63d89 into dotnet:main Oct 9, 2023
jonathanpeppers added a commit to dotnet/android that referenced this pull request Oct 10, 2023
Changes: dotnet/java-interop@8e63cc8...ed63d89

* [generator] restore `[RegisterAttribute]` for *Implementor types

Other changes:

* Added test to verify `Lmono/android/view/View_OnClickListenerImplementor;` exists
  after a build using `AndroidLinkMode=r8`.

dotnet/java-interop#1149
jonathanpeppers added a commit to dotnet/android that referenced this pull request Oct 10, 2023
Changes: dotnet/java-interop@8e63cc8...ed63d89

* [generator] restore `[RegisterAttribute]` for *Implementor types

Other changes:

* Added test to verify `Lmono/android/view/View_OnClickListenerImplementor;` exists
  after a build using `AndroidLinkMode=r8`.

dotnet/java-interop#1149
jonathanpeppers added a commit to dotnet/android that referenced this pull request Oct 17, 2023
Changes: dotnet/java-interop@8e63cc8...ed63d89

* [generator] restore `[RegisterAttribute]` for *Implementor types

Other changes:

* Added test to verify `Lmono/android/view/View_OnClickListenerImplementor;` exists
  after a build using `AndroidLinkMode=r8`.

dotnet/java-interop#1149
@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Java.Lang.Exception: android.view.View_IOnClickListenerImplementor
3 participants