Repository navigation
Description
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
gmck commentedon Sep 19, 2023
@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
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?
gmck commentedon Sep 19, 2023
@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?
moljac commentedon Oct 3, 2023
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...
Is this really native Android?
IsomethingImplementorlooks like bindings, so R8 has not much to do with those.... thinking ...
Maybe ACWs (Android Callable wrappers) are stripped too...
moljac commentedon Oct 3, 2023
@jpobst
Your thoughts?
gmck commentedon Oct 3, 2023
@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
jpobst commentedon Oct 3, 2023
@jonathanpeppers, @dellis1972
This seems like an issue with building (and linking) the application. Maybe something with https://github.com/xamarin/xamarin-android/blob/main/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/GenerateProguardConfiguration.cs?
dellis1972 commentedon Oct 3, 2023
I'll see if I can repo
gmck commentedon Oct 4, 2023
@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.
dellis1972 commentedon Oct 4, 2023
I think we updated R8 in .net 8 compared to .net 7, so its possible that its more aggressive in what it removes.
gmck commentedon Oct 4, 2023
@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.
dellis1972 commentedon Oct 5, 2023
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
-keepstuff for all the java classes we need that are used by the bindings.@jpobst do you think
JavaCallableWrapperGeneratorshould be someout producing a-keeplist in addition to the Java code? Since its called after the linker it should contain only the code that is needed.14 remaining items
tipa commentedon Oct 9, 2023
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_IOnClickListenerImplementordellis1972 commentedon Oct 9, 2023
well in that case is not the fact that the
proguard-android.txtis missing.There has always been situations where the end user has to add certain
-keepoptions when trying to build a Release build withr8. I'm not sure how much we should put in the defaultproguard.cfgas we might end up keeping stuff that really doesn't need to be kept.@jonpryor @jonathanpeppers do you have any thoughts on this?
tipa commentedon Oct 9, 2023
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
jpobst commentedon Oct 9, 2023
Interesting, I was using your
NavigationGraph8Net8to test, with 2 changes to make it .NET 7:net8.0-android->net7.0-androidandroid:targetSdkVersion="33"inAndroidManifest.xmlsince I couldn't get34to compile for other reasons.[generator] restore `[RegisterAttribute]` for *Implementor types
jpobst commentedon Oct 9, 2023
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:to:
Because of this, the Proguard rule we ship to keep these classes no longer works:
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 🐛!
[generator] restore `[RegisterAttribute]` for *Implementor types
[generator] restore `[RegisterAttribute]` for *Implementor types (#1149)
Import proguard rules from proguard-android.txt
Import proguard rules from proguard-android.txt
Import proguard rules from proguard-android.txt
Import proguard rules from proguard-android.txt
[generator] restore `[RegisterAttribute]` for *Implementor types (#1149)