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

[Xamarin.Android.Build.Tasks] redistribute `proguard-android.txt` from Android gradle plugin #9485

Closed jonathanpeppers closed 5 days ago

jonathanpeppers commented 2 weeks ago

Context: https://discord.com/channels/732297728826277939/732297837953679412/1303383272918618163

A customer found their app grew in size when building on Windows versus macOS.

The difference being a file that was passed to the <R8/> task on Windows, but not on macOS:

C:\Users\[username]\AppData\Local\Android\Sdk\tools\proguard\proguard-android.txt

tools is ancient, as any Android SDK should be using cmdline-tools now.

Let's stop using this file as a default set of proguard rules.

But we need to use the right set of proguard rules, reading the text of the old one:

This file is no longer maintained and is not used by new (2.2+) versions of the # Android plugin for Gradle. Instead, the Android plugin for Gradle generates the # default rules at build time and stores them in the build directory.

I found the source code for the gradle task that generates the new files at:

In order to get this file, we can setup an empty gradle project using the Android gradle plugin:

plugins {
    id 'com.android.application' version '8.7.0'
}

Which, hopefully, dependabot is able to update this version.

Then we can run the task:

.\gradlew extractProguardFiles

This outputs files such as:

src\proguard-android\build\intermediates\default_proguard_files\global\proguard-android-optimize.txt-8.7.0
src\proguard-android\build\intermediates\default_proguard_files\global\proguard-android.txt-8.7.0
src\proguard-android\build\intermediates\default_proguard_files\global\proguard-defaults.txt-8.7.0

For now, I simply redistributed the proguard-android.txt file, but we could consider making proguard-android-optimize.txt useable in the future. I believe the only difference is the -allowaccessmodification flag, which users could put in their own proguard rules.

jonathanpeppers commented 2 weeks ago

Removing the line in this PR is fine for main, but it seems like the rules here might be needed:

As mentioned here:

The Android SDK doesn't seem to redistribute any proguard rules anymore, but they have it in their repo?

jonathanpeppers commented 2 weeks ago

Hmm:

This file is no longer maintained and is not used by new (2.2+) versions of the # Android plugin for Gradle. Instead, the Android plugin for Gradle generates the # default rules at build time and stores them in the build directory.

So, we might have to dig up what the Android Gradle plugin does and port the logic to C#?

jonpryor commented 2 weeks ago

We should not merge this PR until better understand the Android Gradle plugin behavior.

jonathanpeppers commented 2 weeks ago

I think I should:

Some links:

jonathanpeppers commented 1 week ago

I was going to add a TPN, like:

using System;
using System.Collections.Generic;
using System.IO;

namespace Xamarin.Android.Prepare;

[TPN]
class android_build_tools_gradle_TPN : ThirdPartyNotice
{
    // https://mvnrepository.com/artifact/com.android.tools.build/gradle
    static readonly Uri    url         = new Uri ("https://android.googlesource.com/platform/tools/base");

    public override string LicenseFile => CommonLicenses.Apache20Path;
    public override string Name        => "google/com.android.tools.build/gradle";
    public override Uri    SourceUrl   => url;
    public override string LicenseText => String.Empty;

    public override bool   Include (bool includeExternalDeps, bool includeBuildDeps) => includeExternalDeps;
}

When running -t:Prepare I saw no changes?!?

Then I found we already have an entry for this:

https://github.com/dotnet/android/blob/70948d5eaa72091389940fdd5a0eb828c85bdd60/build-tools/xaprepare/xaprepare/ThirdPartyNotices/Xamarin.Android.Build.Tasks.cs#L7-L16

So, maybe we are good and no TPN changed needed?

jonpryor commented 6 days ago

Should we update the existing TPN URL to? https://android.googlesource.com/platform/tools/base/+/refs/heads/main/sdk-common/NOTICE

(explicit branch name of master vs. explicit commit)