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

R8 linking causes runtime crashes on new IDE installations due to missing proguard rules file #8397

Open
tipa opened this issue Oct 6, 2023 · 6 comments
Assignees
Labels
Area: App+Library Build Issues when building Library projects or Application projects.

Comments

@tipa
Copy link

tipa commented Oct 6, 2023

Android application type

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

Affected platform version

VS 2022 17.7.4

Description

I am using R8 linking in Release mode as it produces much smaller APK files:

  <PropertyGroup Condition="'$(Configuration)' == 'Release'">
    <AndroidLinkTool>r8</AndroidLinkTool>
    <TrimMode>full</TrimMode>
  </PropertyGroup>

On fresh installations of Visual Studio + the Android SDK, the tools/proguard/ folder is missing, causing runtime crashes, because too much is being linked out (e.g. android.view.View_IOnClickListenerImplementor)

Even if it is opt-in, I would expect the R8 linking to work, as it is also documented here: https://github.com/xamarin/xamarin-android/blob/main/Documentation/guides/D8andR8.md

Steps to Reproduce

  1. Freshly install VS 2022 incl. Android SDK (alternatively, delete existing C:\Program Files (x86)\Android\android-sdk\tools\proguard folder)
  2. Enable R8 using AndroidLinkTool property
  3. Build & run the app in Release mode

Did you find any workaround?

It is possible to workaround the problem by copying one of those files into the C:\Program Files (x86)\Android\android-sdk\tools\proguard folder and rename it to proguard-android.txt:
https://android.googlesource.com/platform/sdk/+/refs/heads/main/files/proguard-android.txt
https://android.googlesource.com/platform/sdk/+/refs/heads/main/files/proguard-android-optimize.txt

However, this workaround only seems to work when building with .NET7 - when using .NET8, the app crashes regardless

Relevant log output

C:\Program Files (x86)\Android\android-sdk\tools\proguard\proguard-android.txt : warning XA4304: ProGuard configuration file 'C:\Program Files (x86)\Android\an
droid-sdk\tools\proguard\proguard-android.txt' was not found. 

Possibly related bug report: #3789

@tipa tipa added Area: App+Library Build Issues when building Library projects or Application projects. needs-triage Issues that need to be assigned. labels Oct 6, 2023
@jpobst
Copy link
Contributor

jpobst commented Oct 6, 2023

This is likely the same as:
#8337

@dellis1972 dellis1972 removed the needs-triage Issues that need to be assigned. label Oct 9, 2023
dellis1972 added a commit to dellis1972/xamarin-android that referenced this issue Oct 9, 2023
Context dotnet#8337
        dotnet#8397

The latest Android sdk no longer ships with `proguard` as a result the
file in proguard-android.txt [1] is missing. As a result customers are
reporting errors such as

```
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 ---
```

This is probably because certain code is removed that in the latest version of R8 that used to be
left in place.
We should include the contents of this removed file in our own proguard_xamarin.cfg file.

[1] https://android.googlesource.com/platform/sdk/+/refs/heads/main/files/proguard-android.txt
@jonathanpeppers
Copy link
Member

The runtime crash related to View_IOnClickListenerImplementor should be fixed by: dotnet/java-interop#1149

Maybe we keep this open to address the warning, though:

C:\Program Files (x86)\Android\android-sdk\tools\proguard\proguard-android.txt : 
warning XA4304: ProGuard configuration file 'C:\Program Files (x86)\Android\android-sdk\tools\proguard\proguard-android.txt' was not found. 

dellis1972 added a commit to dellis1972/xamarin-android that referenced this issue Oct 12, 2023
Context dotnet#8337
        dotnet#8397

The latest Android sdk no longer ships with `proguard` as a result the
file in proguard-android.txt [1] is missing. As a result customers are
reporting errors such as

```
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 ---
```

This is probably because certain code is removed that in the latest version of R8 that used to be
left in place.
We should include the contents of this removed file in our own proguard_xamarin.cfg file.

[1] https://android.googlesource.com/platform/sdk/+/refs/heads/main/files/proguard-android.txt
dellis1972 added a commit to dellis1972/xamarin-android that referenced this issue Oct 23, 2023
Context dotnet#8337
        dotnet#8397

The latest Android sdk no longer ships with `proguard` as a result the
file in proguard-android.txt [1] is missing. As a result customers are
reporting errors such as

```
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 ---
```

This is probably because certain code is removed that in the latest version of R8 that used to be
left in place.
We should include the contents of this removed file in our own proguard_xamarin.cfg file.

[1] https://android.googlesource.com/platform/sdk/+/refs/heads/main/files/proguard-android.txt
dellis1972 added a commit to dellis1972/xamarin-android that referenced this issue Nov 7, 2023
Context dotnet#8337
        dotnet#8397

The latest Android sdk no longer ships with `proguard` as a result the
file in proguard-android.txt [1] is missing. As a result customers are
reporting errors such as

```
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 ---
```

This is probably because certain code is removed that in the latest version of R8 that used to be
left in place.
We should include the contents of this removed file in our own proguard_xamarin.cfg file.

[1] https://android.googlesource.com/platform/sdk/+/refs/heads/main/files/proguard-android.txt
dellis1972 added a commit to dellis1972/xamarin-android that referenced this issue Nov 16, 2023
Context dotnet#8337
        dotnet#8397

The latest Android sdk no longer ships with `proguard` as a result the
file in proguard-android.txt [1] is missing. As a result customers are
reporting errors such as

```
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 ---
```

This is probably because certain code is removed that in the latest version of R8 that used to be
left in place.
We should include the contents of this removed file in our own proguard_xamarin.cfg file.

[1] https://android.googlesource.com/platform/sdk/+/refs/heads/main/files/proguard-android.txt
@Blue101black
Copy link

actions/runner-images#8952

This update to Azure Devops and Github default window's images has removed the proguard directory as well.

Is their a way to config where xamarin grabs the proguard-android.txt file from? Because then I can just commit the .txt file into my repo to fix it.

This is resulting in a run-time crash when using the AirBnb lottie animations in my Xamarin App.

FATAL EXCEPTION: main
java.lang.NoSuchFieldError: no "Lcom/airbnb/lottie/RenderMode;" field "SOFTWARE" in class "Lcom/airbnb/lottie/RenderMode;" or its superclasses
 at crc64720bb2db43a66fe9.FragmentContainer.n_onCreateView(Native Method)
 at crc64720bb2db43a66fe9.FragmentContainer.onCreateView(Unknown Source:0)
 at android.support.v4.app.Fragment.performCreateView(SourceFile:21)
 at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:81)
 at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(SourceFile:39)
 at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:202)
 at android.support.v4.app.BackStackRecord.executeOps(SourceFile:168)
 at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(SourceFile:52)
 at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(SourceFile:89)
 at android.support.v4.app.FragmentManagerImpl.execPendingActions(SourceFile:77)
at android.support.v4.app.Fragment$1.run(SourceFile:63)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7179)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)

@jonathanpeppers
Copy link
Member

@Blue101black a fix for the warning is: #8641

On the original issue above there are links to proguard-android.txt, can you use that in your project for now?

@Blue101black
Copy link

@jonathanpeppers I managed to fix by committing the file to our repo, then as a PowerShell script before the Xamarin Build task I move the file to the directory specified above and that has fixed the issue. Azure DevOps Windows 2022 pipeline.

Glad to see a fix was put out. What needs to be done to get that official fix?

@jonathanpeppers
Copy link
Member

It will be in the next .NET 8 servicing release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: App+Library Build Issues when building Library projects or Application projects.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants