50

In my buildType I see this:

buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
}

I have some questions:

  1. Why there are two files?
  2. What is the difference between them?
  3. Where should I write my rules?
Zenoo
  • 12,670
  • 4
  • 45
  • 69
MBH
  • 16,271
  • 19
  • 99
  • 149

3 Answers3

53

The getDefaultProguardFile('proguard-android.txt') method obtains the default ProGuard settings from the Android SDK tools/proguard/ folder. The proguard-android-optimize.txt file is also available in this Android SDK folder with the same rules but with optimizations enabled. ProGuard optimizations perform analysis at the bytecode level, inside and across methods to help make your app smaller and run faster. Android Studio adds the proguard-rules.pro file at the root of the module, so you can also easily add custom ProGuard rules specific to the current module.

Please refer this: https://developer.android.com/studio/build/shrink-code

Meaning that you should add your custom proguard file into proguard-rules.pro,if you want to separate some rules to many files,you can do it and declare them after this:

proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
Paul Woitaschek
  • 6,717
  • 5
  • 33
  • 52
starkshang
  • 8,228
  • 6
  • 41
  • 52
  • 14
    If your open proguard-android.txt, you will find the following information: `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.` – Bevor Feb 08 '19 at 15:20
7

The getDefaultProguardFile('proguard-android.txt') will retrieve the ProGuard settings that are stored in the Android SDK in tools/proguard

The proguard-rules.pro is a file that is located at the root of the module. The purpose is to allow you to add custom rules (ProGuard) that are specific to the module.

For more Information

myselfmiqdad
  • 2,518
  • 2
  • 18
  • 33
1

The other answers are still mostly right, but since they were written some things have changed.

If you open <android-sdk-root>/sdk/tools/proguard/proguard-android.txt then you'll see this comment:

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.

What does this mean exactly?

It means that the sdk/tools/proguard/proguard-android.txt file is irrelevant - the rules written inside it are not actually used. However, it does NOT mean that the line proguardFiles getDefaultProguardFile('proguard-android.txt') is now useless.

If you use proguardFiles getDefaultProguardFile('proguard-android.txt') with AGP version 2.2 or higher (which I think is everyone at this point), then the proguard-android.txt file used will be the one that ships inside AGP itself. Google mention this in the release notes for AGP 2.2:

getDefaultProguardFile now returns the default ProGuard files that Android plugin for Gradle provides and no longer uses the ones in the Android SDK.

How can I see the rules?

If you want to see what rules are applied in the new proguard-android.txt file from within AGP, then you can check the proguard-android.txt file that AGP generates when it builds your project. Be aware that this file is only generated when minify runs: therefore this file won't exist in your project until you build your project in a build variant with minifyEnabled set to true. AGP will generate the file in your project in the <project-root>/build/intermediates/default_proguard_files/global/ directory.

What if I can't find the file?

Then you can take the hard route and see how the proguard-android.txt file is generated by looking at the AGP source code. The following instructions are what worked for me for version 3.0.0:

1 - Find the gradle source files

For this I followed this brilliant answer: https://stackoverflow.com/a/44122888/6007104

To summarise:

  • Open the directory gradle/caches/modules-2/files-2.1/com.android.tools.build/gradle-core/ on your machine
  • In here are listed the downloaded AGP versions - open the directory you're interested in
  • Dig around within this directory for a file named gradle-core-X-sources.jar, where X is the version you want. For me that was gradle-core-3.0.0-sources.jar.
  • Copy that jar file somewhere safe, and then unzip it to see the AGP source files hidden inside.

2 - Find the rules

Now you have the source files, you might think it's as simple as finding the file named proguard-android.txt. Unfortunately, that file doesn't exist - it's dynamically built. Instead, you'll want to find the code within the AGP source where the proguard-android.txt file is built. For me that was in com/android/build/gradle/ProguardFiles.java. It turns out that (at time of writing), the rules of proguard-android.txt are just the rules defined in the com/android/build/gradle/proguard-common.txt file.

Luke Needham
  • 3,373
  • 1
  • 24
  • 41
  • This should be the accepted answer now (July 2023). Instead of taking the hard route to view the elusive `proguard-android.txt`, can I simply ignore it and override it with my own `proguardFiles 'proguard.cfg'` statement in `build.gradle`? – Introspective Jul 18 '23 at 20:07