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.94k stars 533 forks source link

Building .NET9 app in release with r8 ignores proguard rules #9566

Open randmaayan opened 2 days ago

randmaayan commented 2 days ago

Android framework version

net9.0-android

Affected platform version

.net9

Description

When trying to build the app in release mode with the following configs, the proguard rules created ignore my rules in the proguard.cfg file. Is there something I would need to configure in VSCode? (I'm looking at the obj/Release/net9.0-android/proguard/proguard_project_primary.cfg created)

<PropertyGroup>
    <DebugSymbols>true</DebugSymbols>
    <Optimize>true</Optimize>
    <ErrorReport>prompt</ErrorReport>
    <AndroidDexTool>d8</AndroidDexTool>
    <AndroidLinkTool>r8</AndroidLinkTool>
    <LangVersion>default</LangVersion>
    <AndroidLinkMode>SdkOnly</AndroidLinkMode>
    <AndroidProguardConfig>proguard.cfg</AndroidProguardConfig>
</PropertyGroup>

<ItemGroup>
    <ProguardConfiguration Include="proguard.cfg" />
</ItemGroup>

Steps to Reproduce

  1. add proguard file under the rootpath. 'proguard.cfg'
  2. run dotnet build -c Release -f net9.0-android -t:Run -v detailed
  3. check the obj/Release/net9.0-android/proguard/proguard_project_primary.cfg created.

Did you find any workaround?

No response

Relevant log output

dellis1972 commented 2 days ago

We will need a .binlog to investigate this further. Can you add -bl to the command and attach the resuting binlog (you will need to zip it first).

JoseCarlosLopezGomez commented 1 day ago

It's the same behavior here. I've seen people putting proguard.cfg in Platforms\Android\ folder but this also doesn't work in my project. Even if I add this configuration to .csproj: <ProguardConfiguration Include="Platforms\Android\proguard.cfg" />

jonathanpeppers commented 1 day ago

Android projects have will these wildcards by default, so you just have to name a file to match these patterns:

https://github.com/dotnet/android/blob/d5dab91761a50eaa6f20f391792bf0dd20f5b801/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/Sdk/AutoImport.props#L60-L62

Unfortunately, MAUI overrides Android wildcards with their own:

So, you will have to put <ItemGroup><ProguardConfiguration Include="..." /> for the file in a .NET MAUI project. If this doesn't work, we'll need a .binlog to investigate further.

JoseCarlosLopezGomez commented 19 hours ago

Looks weird but after changing: <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-android34.0|AnyCPU'"> to this (and fixing everything related) <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-android|AnyCPU'"> it's working now.

jonathanpeppers commented 17 hours ago

Looks weird but after changing: <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-android34.0|AnyCPU'"> to this (and fixing everything related) <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-android|AnyCPU'"> it's working now.

$(TargetFramework) is never expanded to include the 34.0 unless you list that yourself in $(TargetFrameworks) with an s. You can probably find that value in $(TargetPlatformVersion), but this is only set after your .csproj file or at the time <ItemGroup> are evaluated.


@randmaayan did adding <ProguardConfiguration Include="..." /> solve your issue? If so, we should move this to dotnet/maui, so they can fix up their default wildcards.

JoseCarlosLopezGomez commented 15 hours ago

Trying to set the Android target SDK in the .csproj doesn't work for me. However, setting it in the Android manifest does. <ProguardConfiguration Include="..." /> works for me.

dotnet-policy-service[bot] commented 15 hours ago

Hi @randmaayan. We have added the "need-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.