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

Apk crashes when enabling "Publish trimmed" without "fast-deployment debug" #8430

Open mosammo opened 1 year ago

mosammo commented 1 year ago

Android application type

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

Affected platform version

VS 2022 17.6.2+, .Net for Android 7+

Description

Enabling "Publish trimmed" without "fast-deployment debug" with Xamain forms apk causes the apk to crash without showing any errors.

Expected Behavior: Apk should work normally without crashing. especially that it works without crashing if we disabled "Publish Trimmed" or if we enable "Fast-deployment debug".

Steps to Reproduce

Steps to Reproduce:

  1. Build and publish this demo project AndroidApp27.zip
  2. Apk crashes without any errors when building in (release with trimming) but doesn't crash in (debug with trimming and fast deployment) and also doesn't crash when building (without trimming)

Did you find any workaround?

No

Relevant log output

No response

grendello commented 1 year ago

@mosammo from your description it appears that trimming is removing some type that your application uses. In order to be certain, we will need two kinds of logs from you:

  1. Build log. In order to create it, build your application from the VS developer prompt using the command below:

    dotnet build -c Release -bl

    and attach the resulting msbuild.binlog file

  2. The Android logcat output. From the VS developer prompt, follow these steps:

    1. Build and install the app: dotnet build -c Release -t Install
    2. Type: adb logcat -G 16M
    3. Type: adb shell setprop debug.mono.log default,assembly,mono_log_level=debug,mono_log_mask=all
    4. Type: adb logcat -c
    5. Start the application and wait for it to crash, then give it one more second
    6. Type: adb logcat -d > logcat.txt

    and attach the resulting logcat.txt file.

mosammo commented 1 year ago

Please find the logs attached: "msbuild.binlog" and the "logcat.txt" (saved the logcat 3 times after 3 crashes just to make sure everything is logged) logs.zip

grendello commented 1 year ago

@mosammo thank you for the logs. It seems that assemblies are packaged into the apk but then they don't appear to be found in it on device. Could you repeat the steps in 2. above, but replace the installation step with:

dotnet build -c Release -t Install -bl

and attach the resulting msbuild.binlog

dellis1972 commented 1 year ago

@mosammo

Generally speaking using PublishTrimmed in a Debug configuration might cause some issues. Its not a configuration we would expect to use when debugging.

I would take a look at https://learn.microsoft.com/en-us/dotnet/maui/android/linking?tabs=vs. It talks about maui, but it applies to typical .net andorid apps too. Specifically sections on preserving code and assemblies.

What I suspect is happening is some of your types are being removed and your app crashed when using PublishTrimmed, so you turned that setting on in debug mode to see if you can debug the issue in the IDE. I'm not sure that will work. That you can do is look at the adb logcat output and find the point where its crashing in Release mode. It should mention the types which are missing (because the linker removed them), then you can follow the advice in the links provided to add additional instructions to the linker to preserve the code which is needed.

mosammo commented 1 year ago

@dellis1972 @grendello I followed the same steps on the real project and got the following logcat error "failed to load assembly System.Private.CoreLib.dll" although I already have <TrimmerRootAssembly Include="System.Private.CoreLib" /> in the project file so i don't know why it's not included in the published release apk please find the logs from the real project : logs 1810 main.zip image

dellis1972 commented 1 year ago

The msbuild.binlog contains the following error.

The imported project "C:\Program Files\dotnet\sdk\7.0.400-preview.23330.10\Microsoft\Portable\v4.6\Microsoft.Portable.CSharp.targets" was not found. Confirm that the expression in the Import declaration "C:\Program Files\dotnet\sdk\7.0.400-preview.23330.10\Microsoft\Portable\v4.6\Microsoft.Portable.CSharp.targets" is correct, and that the file exists on disk.

Are you trying to build a PCL library with dotnet ? I'm not sure that is even supported. Looks like you are trying to mix sdk style projects with the old classic project system. You will need to upgrade all your projects to be sdk style (the short which uses <Project Sdk="Microsoft.NET.Sdk"> ) in order to upgrde to .net 7.

mosammo commented 1 year ago

The project was previously portable but i already converted it and the android project to .net SDK, so all projects in the solution are now .net sdk. image I got the error "Microsoft.Portable.CSharp.targets" before but after clean and rebuild the error was gone. Here's a clean binary log after clean and rebuild

logs 1018 -2.zip

dellis1972 commented 1 year ago

Thanks for the logs. One slight issue is it wasn't from a clean build. So some of the targets were skipped. The result is we cannot figure out what was added to the apk in this instance.

Any chance you can do the same again but this time from a completely clean build ?

mosammo commented 1 year ago

Sure, I'll do anything needed. This time manually deleted both "bin" and "obj" folders from all projects, then cleaned solution then rebuilt solution. logs 1018 -3.zip

dellis1972 commented 1 year ago

@grendello something weird is going on. The blob is not making it into the apk. Its not even in the list of files given to the BuildApk task.

dellis1972 commented 1 year ago

Hmm UseAssemblyStore is false.

mosammo commented 11 months ago

any clue?

mosammo commented 9 months ago

is there a way to not trim the platform specific blob and only trim the main "assemblies.blob"? if not is there a way the we can replace the "assemblies.blob" of the not trimmed apk with the "assemblies.blob" of the trimmed apk? as i checked "assemblies.blob" alone is 60 MB and in trimmed apk it's 30 MB so if we can only trim "assemblies.blob" it will be enough for most people having this trimming issue image