How to include a proguard configuration in my Android library (AAR) - android

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

Is Proguard necessary for gradle 3.2.1?

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)

remove android-support-v7-appcompat resources

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?

Android - What is the purpose of multiDexKeepProguard ? How does it compare to proguardFiles close in gradle?

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.

Merge Library project's proguard rules [duplicate]

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

Avoid shrinking/removing wearable micro app from apk file

I activated the resouce shrinking in my build.gradle but now my embeded wearable app is stripped out. How can I avoid that my micro app is removed, because it is unused?
Skipped unused resource res/raw/android_wear_micro_apk.apk: 382310 bytes
Since I want to shrink the other not used resouces I'm using this DSL:
buildTypes {
release {
shrinkResources true
// ...
}
}
I would guess that I need to use proguard but I have no idea how to achieve that. I checked of cause the documentation, but I didn't get it how protect a single member variable.
Are you referencing the R.raw.apkpath? Looking at the Packaging Wearable Apps training mentions rawPathResId in the res/xml/wearable_app_desc.xml
On a side note enabling proGuard is simple with Gradle
buildTypes {
release {
runProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
This is Bug 78620 and was fixed in the gradle build tools 0.14.1.

Categories

Resources