Skip to content

Java.Lang.Exception: android.view.View_IOnClickListenerImplementor #8337

Closed
dotnet/java-interop
#1149

Description

@gmck

Android application type

.NET Android (net7.0-android, etc.)

Affected platform version

VS 2022 17.8.0 Preview 2

Description

net8.0-android - (no option to choose net8 above) . - Deploying to Pixel 6 - Android 14 beta.

The release version builds ok, it had some build errors, but they were simple to fix. The app starts ok but then fails at run time when I try to open my GoogleSignInFragment with the error below.

If a debug build everything works ok.

If I replace the button click with IView.IOnClickListener in the release build and use the following, the error goes away and I can sign in.

public void OnClick(View? v)
{
 ...
}

It appears as though, R8 is stripping something away, but I'm not sure how to fix it.

I do have a proguard.cfg which has the following.

-keep class com.google.android.gms.common.SignInButton
-keep class com.google.android.gms.auth.api.signin.GoogleSignInOptions { *; }
-keep class com.google.android.gms.auth.api.signin.internal.SignInHubActivity {*; }

According to my comments, this was added in June 2020, so I'm not sure it is even required now. I now can't remember how I came up with that, but it was crashing back then and the above fixed it.

After I got the GoogleSignInFragment working I then tried the SubscriptionFragment and that also failed with the same error.

Can you please suggest what is required?

Prior to working on this app, I first converted one of my GitHub test apps to net8.0-android and that is working fine, but obviously, that doesn't have any buttons. I could add a button to one of its fragments and upload it there if you need a sample.

Steps to Reproduce

See above

Did you find any workaround?

See above

Relevant log output

Unhandled Exception from source=AndroidEnvironment

Java.Lang.Exception: android.view.View_IOnClickListenerImplementor
                                                                                                       at Java.Interop.JniEnvironment.Types.TryFindClass(String , Boolean )
                                                                                                       at Java.Interop.JniEnvironment.Types.FindClass(String )
                                                                                                       at Java.Interop.JniType..ctor(String )
                                                                                                       at Java.Interop.JniPeerMembers.JniInstanceMethods..ctor(Type )
                                                                                                       at Java.Interop.JniPeerMembers.JniInstanceMethods.GetConstructorsForType(Type )
                                                                                                       at Java.Interop.JniPeerMembers.JniInstanceMethods.StartCreateInstance(String , Type , JniArgumentValue* )
                                                                                                       at Android.Views.View.IOnClickListenerImplementor..ctor()
                                                                                                       at Android.Views.View.__CreateIOnClickListenerImplementor()
                                                                                                       at Java.Interop.EventHelper.AddEventHandler[IOnClickListener,IOnClickListenerImplementor](WeakReference& , Func`1 , Action`1 , Action`1 )
                                                                                                       at Android.Views.View.add_Click(EventHandler )
                                                                                                       at com.glmsoftware.obdnowpros.Fragments.SubscriptionFragment.OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
                                                                                                       at AndroidX.Fragment.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_inflater, IntPtr native_container, IntPtr native_savedInstanceState)
                                                                                                       at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_L(_JniMarshal_PPLLL_L callback, IntPtr jnienv, IntPtr klazz, IntPtr p0, IntPtr p1, IntPtr p2)
                                                                                                      --- End of managed Java.Lang.Exception stack trace ---

Activity

  1. added
    Area: App Runtime
    needs-triage
    on Sep 15, 2023
  2. gmck commented on Sep 19, 2023

    @gmck
    Author

    @grendello

    I forgot to update this.

    On another fragment, I got the same exception but for the OnLongClickListener and had to add the following
    to the proguard.cfg

    -keep class android.view.View_IOnClickListenerImplementor
    -keep class android.view.View_IOnLongClickListenerImplementor
    

    Adding these allowed me to revert my code to what it was previously.

    Can you explain why these two are necessary in the Net8 build when they weren't before in the Net7 or the X.A. builds?

  3. gmck commented on Sep 19, 2023

    @gmck
    Author

    @grendello

    From #8285

    .NET's HttpClient? If yes, then part of the slowdown might be caused by dotnet/runtime#89880.

    Do you know if the fix in that issue you referred to made it into the Net8 RC1 release?

  4. moljac commented on Oct 3, 2023

    @moljac
    Contributor

    Can you please suggest what is required?

    Believe me I would like more than you to have more time to work on samples to learn and understand and to boost my growth mindset...

    android.view.View_IOnClickListenerImplementor

    Is this really native Android? IsomethingImplementor looks like bindings, so R8 has not much to do with those.

    ... thinking ...

    Maybe ACWs (Android Callable wrappers) are stripped too...

  5. moljac commented on Oct 3, 2023

    @moljac
    Contributor

    @jpobst

    Your thoughts?

  6. gmck commented on Oct 3, 2023

    @gmck
    Author

    @moljac, @jpobst

    I've just updated a sample (added a button click event to the MaterialWidgetsFragment) on my GitHub which demonstrates the problem. Build it as a debug release and it works fine. Crashes with the same exception in release build. See notes in the readme for further details.

    https://github.com/gmck/NavigationGraph8Net8

  7. self-assigned this
    on Oct 3, 2023
  8. dellis1972 commented on Oct 3, 2023

    @dellis1972
    Contributor

    I'll see if I can repo

  9. gmck commented on Oct 4, 2023

    @gmck
    Author

    @dellis1972, @moljac, @jpobst

    I updated the sample app above adding the lines from the previous message to a new proguard.cfg and that fixes the crash seen in the release build. I'd still like to know why it was necessary in the .Net8 project compared to the .Net7 project where it wasn't.

  10. dellis1972 commented on Oct 4, 2023

    @dellis1972
    Contributor

    I think we updated R8 in .net 8 compared to .net 7, so its possible that its more aggressive in what it removes.

  11. gmck commented on Oct 4, 2023

    @gmck
    Author

    @dellis1972, @Mojac, @jpobst

    I find this to be confusing. Is it R8 or IILink responsible?

    First, why would Xamarin.Android C# programmers expect a fragment containing a click event using a C# event handler like in the example fail to open.

    It is not exactly intuitive to think that the app needs an IView.IOnClickListener interface and Click() method instead. So it's not obvious that the failure is caused by the fact that you have used a C# event handler. It is not as though it fails when you click the button, cause you never get that far before it crashes.

    If you've used IView.IOnClickListener in the past or seen equivalent Android code, then I guess after seeing the exception you would realise, but I'd reckon most wouldn't.

    I just added the same event handler to the Net7 version of the same app and it too failed with the same exception, so I don't think it is related Net8 RC1 release.

  12. dellis1972 commented on Oct 5, 2023

    @dellis1972
    Contributor

    Its probably not intuative.

    I guess its down to how the bindings work. C# programmers like events like OnClick. The android side uses the Listener pattern to handle interactions, so the binding will setup the Listener on the Java side and then include a C# event on the C# side to make it easier on the C# programmer. I guess the prooblem there is all that is abstracted away, but I'm not sure what we can do about that in the case of the Listener being stripped away.

    Using R8 and even proguard has always been a case of "Run it and see what crashes" since they might remove some things that are needed. There is a linker step which is designed to produce the -keep stuff for all the java classes we need that are used by the bindings.

    @jpobst do you think JavaCallableWrapperGenerator should be someout producing a -keep list in addition to the Java code? Since its called after the linker it should contain only the code that is needed.

  13. 14 remaining items

  14. tipa commented on Oct 9, 2023

    @tipa

    It also didn't fix the android.view.View_IOnClickListenerImplementor issues. Sample repo:
    New folder.zip

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.companyname.New_folder/crc64abe8cc9139195b67.MainActivity}: java.lang.ClassNotFoundException: android.view.View_IOnClickListenerImplementor

  15. dellis1972 commented on Oct 9, 2023

    @dellis1972
    Contributor

    well in that case is not the fact that the proguard-android.txt is missing.

    There has always been situations where the end user has to add certain -keep options when trying to build a Release build with r8. I'm not sure how much we should put in the default proguard.cfg as we might end up keeping stuff that really doesn't need to be kept.

    @jonpryor @jonathanpeppers do you have any thoughts on this?

  16. tipa commented on Oct 9, 2023

    @tipa

    There has always been situations where the end user has to add certain -keep options when trying to build a Release build with r8.

    I'm aware of that, the weird thing is just that it wasn't necessary with .NET7. My attached example project above built with NET7 does not crash

  17. jpobst commented on Oct 9, 2023

    @jpobst
    Contributor

    It is very interesting that you can get your net7.0 version to work for I had exactly the same problem with my .Net7 version.

    Interesting, I was using your NavigationGraph8Net8 to test, with 2 changes to make it .NET 7:

    • net8.0-android -> net7.0-android
    • Setting android:targetSdkVersion="33" in AndroidManifest.xml since I couldn't get 34 to compile for other reasons.
  18. jpobst commented on Oct 9, 2023

    @jpobst
    Contributor

    Thanks to a full team effort 😅, we believe we have tracked down the regression.

    This optimization we did: dotnet/java-interop#1105, removed the [Register] attribute from our generated *Implementor (and other) C# classes. This moved and renamed the Java classes from:

    package mono.android.view;
    public class View_OnClickListenerImplementor { ... }

    to:

    package android.view;
    public class View_IOnClickListenerImplementor { ... }

    Because of this, the Proguard rule we ship to keep these classes no longer works:

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

    We are discussing the best fix given the timeline for .NET 8 GA, likely putting the [Register] attributes back (PR).

    Thanks everyone for your reports and test cases, they definitely helped track down this hard to find 🐛!

  19. locked and limited conversation to collaborators on Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Area: App Runtimeneeds-triage

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions