dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.93k stars 528 forks source link

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

Open tipa opened 1 year ago

tipa commented 1 year ago

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: https://github.com/xamarin/xamarin-android/issues/3789

jpobst commented 1 year ago

This is likely the same as: https://github.com/xamarin/xamarin-android/issues/8337

jonathanpeppers commented 1 year ago

The runtime crash related to View_IOnClickListenerImplementor should be fixed by: https://github.com/xamarin/java.interop/pull/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. 
Blue101black commented 9 months ago

https://github.com/actions/runner-images/issues/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 commented 9 months ago

@Blue101black a fix for the warning is: https://github.com/xamarin/xamarin-android/pull/8641

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

Blue101black commented 9 months ago

@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 commented 9 months ago

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