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.92k stars 526 forks source link

Related to "proguard rules embedded in binding projects are lost" #4449 #5310 #8523

Open awattar opened 9 months ago

awattar commented 9 months ago

Android application type

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

Affected platform version

VSMac 17.6.7 (build 417), VSWin 17.8.0, .NET 7.0.402

Description

When building MS.Android net7 Platfrom/Maui app with ProGuard/R8 enabled in release rules from dependent binding library containing library level ProGuard are not taken into account or malformed.

App crashes on runtime.

Related to #4449 and #5310.

Steps to Reproduce

  1. Remove bin/obj from relevant projects/subprojects.
  2. Clean project in VSMac/VSWin/CLI.
  3. Restore project in VSMac/VSWIn/CLI.
  4. Build project in VSMac/VSWin/CLI.
  5. Deploy project in VSMac/VSWIn/CLI.
  6. Run app in VSMac/VSWin/CLI.
  7. Runtime crash.

Going straight to VS deploy (CLI -t:Run) for the removed bin/obj folders also doesn't work.

It seems that restore + build causes creation of malformed mapping.txt (in bin framework/platform/architecture subfolder) file. FIle is significantly smaller than the working file +- 20%.

Did you find any workaround?

App must be explicitly rebuild (instead of build) or additional --no-incremental option must be added to CLI dotnet build. It seems that some targets related to the ProGuard/R8 are not invoked fully or in the wrong moment for builds after project restore. Probably those builds are treated as incremental and due to some performance optimisations not all rules are taken into account.

Relevant log output

No response

jonathanpeppers commented 9 months ago

Can you share a sample project that shows this problem? Thanks!

awattar commented 9 months ago

@jonathanpeppers Will try to share something but first will check it on net8.

awattar commented 9 months ago

Was able to reproduce it on NET8. Will work on sample to share.

awattar commented 3 months ago

Will retest on latest workloads and let you know.

awattar commented 3 months ago

@jpobst @jonathanpeppers I've retested on everything being updated (dotnet, workloads, VSMac, VSWin, etc.) and it can be easily reproduced on VSMac, VSWin and CLI builds.

I've uploaded samples and mappings files in the private comment in this ticket https://developercommunity.visualstudio.com/t/Code-sample-for-https:githubcomxamar/10666925

jonathanpeppers commented 2 months ago

@awattar sorry for the delay. I'm able to run XamarinForms\Examples\SampleAppSuite\SampleAppSuite.Droid.csproj in Release mode and make it to this screen:

image

Are there more specific steps to see the problem here? Can you share the steps in terms of dotnet build commands?

A tester was also unsuccessful in reproducing the issue.

awattar commented 2 months ago

@jonathanpeppers tried to reach you on Discord - but no response to my DM.

You need to run into "incremental" mode. Please try this scenario:

  1. Remove bin/obj folders in Droid subfolder.
  2. Open VSMac and restore NuGets for solution using "Restore NuGet Packages" option.
  3. Go to terminal then cd to Droid solution subfolder with Droid csproj and run this command there: dotnet build -t:Run -c Release -f net8.0-android34.0 while only one physical Android device is attached or add -p:AdbTarget="-s insert_SERIAL_here" when more than one physical device is connected.

App will deploy and run and will crash immediately with log entries suggesting proguard issue - also mapping.txt will have different size than normal.

jonathanpeppers commented 2 months ago

Is VS Mac required to cause the problem here? VS Mac is retiring next month:

You need to run into "incremental" mode

The steps above are a fresh build. In what way is it incremental?

awattar commented 1 month ago

@jonathanpeppers No VSMac is not required but this is a 100% repro.

This is a "incremental" build in the way that if you add --no-incremental to the above command it will work - see my initial description.

I can provide another scenario if you can't work on this one.

jonathanpeppers commented 1 month ago

Proguard/R8 rules do not do anything in Debug mode. So, do the above steps need to be done in Release mode?

--no-incremental is equivalent to -t:Rebuild:

So, can your steps above be reduced to:

awattar commented 1 month ago

@jonathanpeppers Yes Release was specified in the previous post https://github.com/dotnet/android/issues/8523#issuecomment-2226534031 to make proguard/R8 work.

Regarding the steps you have provided I'm not quite sure - I believe it has something to do with separate initial Restore - so it builds some cache or sth from referenced NuGets then it treats consecutive build and -t:Run incrementally so it does not fully recreate proguard mappings.

Please try scenario with VSMac if you can.