Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Nov 20, 2024

Conversation

jonathanpeppers
Copy link
Member

@jonathanpeppers jonathanpeppers commented Nov 5, 2024

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 jonathanpeppers marked this pull request as ready for review November 5, 2024 22:01
@jonathanpeppers
Copy link
Member Author

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
Copy link
Member Author

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
Copy link
Member

jonpryor commented Nov 6, 2024

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

@jonathanpeppers jonathanpeppers marked this pull request as draft November 6, 2024 20:53
@jonathanpeppers
Copy link
Member Author

jonathanpeppers commented Nov 6, 2024

I think I should:

  • Setup a gradle project that brings in the Android gradle plugin
  • Make sure dependabot can bump the Android plugin, so we can keep it updated
  • Build the project with our product and ship proguard-android.txt
  • The removed line in this PR uses the new proguard-android.txt file instead

Some links:

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
…m Android gradle plugin

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:

* https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:build-system/gradle-core/src/main/java/com/android/build/gradle/ProguardFiles.java;l=116

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 jonathanpeppers force-pushed the dev/peppers/tools/proguard branch from 1682328 to 4ee6762 Compare November 7, 2024 15:22
@jonathanpeppers jonathanpeppers changed the title [Xamarin.Android.Build.Tasks] stop using proguard-android.txt from Android SDK tools [Xamarin.Android.Build.Tasks] redistribute proguard-android.txt from Android gradle plugin Nov 7, 2024
@jonathanpeppers jonathanpeppers marked this pull request as ready for review November 7, 2024 17:46
@jonathanpeppers
Copy link
Member Author

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:

[TPN]
class XamarinAndroidBuildTasks_AOSP : ThirdPartyNotice
{
static readonly Uri url = new Uri ("https://android.googlesource.com/platform/tools/base/+/d41d662dbf89f9b60ca6256415a059c0107749b8/sdk-common/NOTICE");
public override string LicenseText => String.Empty;
public override string LicenseFile => CommonLicenses.Apache20Path;
public override string Name => "android/platform/tools/base";
public override Uri SourceUrl => url;
}

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

@jonpryor
Copy link
Member

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)

@jonathanpeppers jonathanpeppers merged commit 6b57cc2 into main Nov 20, 2024
56 of 58 checks passed
@jonathanpeppers jonathanpeppers deleted the dev/peppers/tools/proguard branch November 20, 2024 21:50
jonathanpeppers added a commit that referenced this pull request Nov 20, 2024
…m Android gradle plugin (#9485)

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:

* https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:build-system/gradle-core/src/main/java/com/android/build/gradle/ProguardFiles.java;l=116

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.

Also, updated TPN url for:

https://android.googlesource.com/platform/tools/base/+/refs/heads/main/sdk-common/NOTICE
@github-actions github-actions bot locked and limited conversation to collaborators Dec 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants