Android libraries, per the AAR file spec, includes a 'proguard.txt' file.
My understanding is that this file declares how the library correctly can be obfuscated and minified. In my case I need to preserve some API-classes.
How can I declare the library's proguard.txt file in the library's build.gradle? And will this file be automatically read when creating an application (APK) that uses my library?
I didn't find this information in Android's Gradle Plugin User Guide.
In your Gradle file's default configuration closure, specify your Proguard file with consumerProguardFiles instead of proguardFiles. For example:
defaultConfig {
consumerProguardFiles 'proguard.txt'
}
ProGuard artefact
[ProGuard workflow]
Artefact not minified, Consumer solve it
Library is open-sourced but as a library developer you can provide a ProGuard file which will be take into account by consumer(app) by demand(minifyEnabled true in consumer). consumerProguardFiles in you library build.gradle. It adds proguard.txt file(is the same as .pro) in an artefact
For example your library is open-source and application developer wants to minify all
android {
defaultConfig {
//consumerProguardFiles '<file_path>'
consumerProguardFiles 'proguard-rules.pro'
}
buildTypes {
release {
minifyEnabled false
}
}
//...
}
Artefact is minified
Library is closed-source - you are able to use the next possibility:
android {
buildTypes {
release {
minifyEnabled true
//proguardFiles project(':<project_name>').file('<file_path>')
proguardFiles 'proguard-rules.pro'
}
}
//...
}
*Please note that:
minifyEnabled true and proguardFiles project both should be set.
If you use single minifyEnabled true or <file_path> is wrong - classes.jar is empty.
If single proguardFiles project - no effect
As for build process on the example of library - application - all .class files will be merged into single archive with .dex extension
Related
I am doing this first time. Using Facebook SDK for android app.
I am following this tutorial.
https://developers.facebook.com/docs/sharing/android/
My app is gradle 3.2.1
Do I need to use ProGuard here?
What code should I write between the given two codes on this link :
https://developer.android.com/studio/build/shrink-code.html?fbclid=IwAR3hmG6hOtzfyiHa3Sehxa4o2j9vq9sPrk8ZVbr-WWyUDakiskFMZQgloJM
android {
buildTypes {
release {
// Enables code shrinking, obfuscation, and optimization for only
// your project's release build type.
minifyEnabled true
// Enables resource shrinking, which is performed by the
// Android Gradle plugin.
shrinkResources true
// Includes the default ProGuard rules files that are packaged with
// the Android Gradle plugin. To learn more, go to the section about
// R8 configuration files.
proguardFiles getDefaultProguardFile(
'proguard-android-optimize.txt'),
'proguard-rules.pro'
}
}
...
}
And another code:
android {
...
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile(
'proguard-android-optimize.txt'),
// List additional ProGuard rules for the given build type here. By default,
// Android Studio creates and includes an empty rules file for you (located
// at the root directory of each module).
'proguard-rules.pro'
}
}
flavorDimensions "version"
productFlavors {
flavor1 {
...
}
flavor2 {
proguardFile 'flavor2-rules.pro'
}
}
}
There are some more small codes below it, which one should I add?
Please explain.
As you add ProGuard files you have to add this
useProguard true
in your gradle File(module)
For minifying my app I've done below steps:
In Gradle:
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
I've added required proguard rules in proguard-rules.pro
Also I've added
defaultConfig {
resConfigs "en"
}
And I removed unused resources from my project:
Refactor/Remove Unused Resources
Now when I generate signed app and open my app (with WinRAR) I see that still there are some resources in drawable that I don't use in my app.
like abc_btn_check_to_on_mtrl_015(checkbox icon) abc_ic_star_black_48dp(rating icon) etc.
I'm using appcompat , design , cardview and recyclerview library.
Can I remove them too? How?
I have created a multidex app. But in regards to proguard i have the following in build.gradle:
android {
defaultConfig {
...
multiDexEnabled true
}
productFlavors {
dev {
// Enable pre-dexing to produce an APK that can be tested on
// Android 5.0+ without the time-consuming DEX build processes.
minSdkVersion 21
}
prod {
// The actual minSdkVersion for the production version.
minSdkVersion 14
}
}
buildTypes {
release {
minifyEnabled true
*** proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.android.support:multidex:1.0.1'
}
My question is about the progardFiles vs using multiDexKeepProguard. The documentation states:
File multiDexKeepProguard
Text file with additional ProGuard rules to be used to determine which
classes are compiled into the main dex file.
If set, rules from this file are used in combination with the default
rules used by the build system.
So if i do not use the multiDexKeepProguard then my classes still get compiled but may not end up in the main dex file, is that correct ? I am not clear how this differs from proguardFiles.
Android documentation also references this.
If you're enabling proguard in your application it's usually necessary to define proguard rules. proguardFiles are meant to be the instructions for progurard to minify or obfuscate your app.
multiDexKeepProguard is specifically for telling multidex which files are important to load at app startup and therefore what to keep in the main dex. As far as i'm aware, it just uses the proguard syntax as a convenience. This is optional and will usually only be set if there is an issue at runtime.
I would like to publish my app to multiple market. the version code should be same in all market and my app has IAP so i should have multiple flavor and one manifest per flavor.
And also i need the mapping file for the crash report(for example in tracepot).
My problem is: How can i have one mappings.txt file for multiple flavor?
build.gradle:
android {
productFlavors {
Market1 {
...
}
Market2 {
....
}
signingConfigs {
debug {
....
}
release {
....
}
}
buildTypes {
debug {
debuggable true
signingConfig signingConfigs.debug
}
release {
debuggable false
signingConfig signingConfigs.release
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
Yes, the current settings in your build.gradle will apply the proguard file to all release builds of all flavors. You already have it there.
Update after the conversation with OP in comments below:
Now i understood the question better. You are trying to have a single mapping.txt file and are not talking about the proguard-rules.pro. The answer is No, you cannot have a single file for multiple generated apks. Proguard rules are applied to each flavor separately and each time it generates a separate mapping file. There might not be a any differences in your mapping.txt files right now because you may not be using different sourceSets (Java classes). But, if you have different sourceSets, the mappings.txt file will not be the same. Probably you have only minimal difference between your flavors right now like different drawables with same name, If i'm assuming right.
Android libraries, per the AAR file spec, includes a 'proguard.txt' file.
My understanding is that this file declares how the library correctly can be obfuscated and minified. In my case I need to preserve some API-classes.
How can I declare the library's proguard.txt file in the library's build.gradle? And will this file be automatically read when creating an application (APK) that uses my library?
I didn't find this information in Android's Gradle Plugin User Guide.
In your Gradle file's default configuration closure, specify your Proguard file with consumerProguardFiles instead of proguardFiles. For example:
defaultConfig {
consumerProguardFiles 'proguard.txt'
}
ProGuard artefact
[ProGuard workflow]
Artefact not minified, Consumer solve it
Library is open-sourced but as a library developer you can provide a ProGuard file which will be take into account by consumer(app) by demand(minifyEnabled true in consumer). consumerProguardFiles in you library build.gradle. It adds proguard.txt file(is the same as .pro) in an artefact
For example your library is open-source and application developer wants to minify all
android {
defaultConfig {
//consumerProguardFiles '<file_path>'
consumerProguardFiles 'proguard-rules.pro'
}
buildTypes {
release {
minifyEnabled false
}
}
//...
}
Artefact is minified
Library is closed-source - you are able to use the next possibility:
android {
buildTypes {
release {
minifyEnabled true
//proguardFiles project(':<project_name>').file('<file_path>')
proguardFiles 'proguard-rules.pro'
}
}
//...
}
*Please note that:
minifyEnabled true and proguardFiles project both should be set.
If you use single minifyEnabled true or <file_path> is wrong - classes.jar is empty.
If single proguardFiles project - no effect
As for build process on the example of library - application - all .class files will be merged into single archive with .dex extension