Skip to content

Commit ed63d89

Browse files
[generator] restore [RegisterAttribute] for *Implementor types (#1149)
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>(...); } We could do `-keep class android.**` [^0], 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? [^0]: Why is `View_IOnClickListenerImplementor` in the `android.view` package?! Because package name generation for Java Callable Wrappers is special-cased for `Mono.Android` to simply [lowercase the namespace name][0], which applies only if the type *doesn't* have `[RegisterAttribute]`! [0]: https://github.com/xamarin/java.interop/blob/8e63cc8da1882cadc7db329bfa6cafb6f914769f/src/Java.Interop.Tools.TypeNameMappings/Java.Interop.Tools.TypeNameMappings/JavaNativeTypeManager.cs#L200-L209
1 parent 8e63cc8 commit ed63d89

File tree

6 files changed

+6
-0
lines changed

6 files changed

+6
-0
lines changed

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/Common/WriteDuplicateInterfaceEventArgs.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ public partial class AnimationEndEventArgs : global::System.EventArgs {
157157

158158
}
159159

160+
[global::Android.Runtime.Register ("mono/java/code/AnimatorListenerImplementor")]
160161
internal sealed partial class AnimatorListenerImplementor : global::Java.Lang.Object, AnimatorListener {
161162

162163
object sender;

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/JavaInterop1/WriteDuplicateInterfaceEventArgs.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public partial class AnimationEndEventArgs : global::System.EventArgs {
4747

4848
}
4949

50+
[global::Android.Runtime.Register ("mono/java/code/AnimatorListenerImplementor")]
5051
internal sealed partial class AnimatorListenerImplementor : global::Java.Lang.Object, AnimatorListener {
5152

5253
object? sender;

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteDuplicateInterfaceEventArgs.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ public partial class AnimationEndEventArgs : global::System.EventArgs {
157157

158158
}
159159

160+
[global::Android.Runtime.Register ("mono/java/code/AnimatorListenerImplementor")]
160161
internal sealed partial class AnimatorListenerImplementor : global::Java.Lang.Object, AnimatorListener {
161162

162163
object sender;

tests/generator-Tests/expected.xaji/Core_Jar2Xml/Android.Views.View.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public unsafe void OnClick (global::Android.Views.View v)
111111

112112
}
113113

114+
[global::Android.Runtime.Register ("mono/android/view/View_OnClickListenerImplementor")]
114115
internal sealed partial class IOnClickListenerImplementor : global::Java.Lang.Object, IOnClickListener {
115116
public unsafe IOnClickListenerImplementor () : base (IntPtr.Zero, JniHandleOwnership.DoNotTransfer)
116117
{

tests/generator-Tests/expected.xaji/GenericArguments/Com.Google.Android.Exoplayer.Drm.IExoMediaDrm.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ public byte[] P4 {
162162

163163
}
164164

165+
[global::Android.Runtime.Register ("mono/com/google/android/exoplayer/drm/ExoMediaDrm_OnEventListenerImplementor")]
165166
internal sealed partial class IExoMediaDrmOnEventListenerImplementor : global::Java.Lang.Object, IExoMediaDrmOnEventListener {
166167

167168
object sender;

tools/generator/SourceWriters/InterfaceEventHandlerImplClass.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public InterfaceEventHandlerImplClass (InterfaceGen iface, CodeGenerationOptions
2727
type.Nullable = opt.SupportNullableReferenceTypes;
2828
Fields.Add (new FieldWriter { Name = "sender", Type = type });
2929
}
30+
Attributes.Add (new RegisterAttr (jni_class, additionalProperties: iface.AdditionalAttributeString ()) { UseGlobal = true });
3031

3132
AddConstructor (iface);
3233
AddMethods (iface, opt);

0 commit comments

Comments
 (0)