Is Proguard necessary for gradle 3.2.1? - android

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)

Related

How to have different gradle.properties files for release/debug builds?

We need to have gradle.properties files with different configs for release and debug builds, because some of the features we use are experimental and they break some things. Is that possible?
Example of our gradle.properties file
org.gradle.daemon=true
org.gradle.jvmargs=-Xmx1500m -XX:MaxPermSize=512m
org.gradle.parallel=true
kotlin.incremental=true
android.enableD8=true
As per official document:
You can create and configure build types in the module-level build.gradle file inside the android block. When you create a new module, Android Studio automatically creates the debug and release build types for you. Although the debug build type doesn't appear in the build configuration file, Android Studio configures it with debuggable true. This allows you to debug the app on secure Android devices and configures APK signing with a generic debug keystore.
You can add the debug build type to your configuration if you want to add or change certain settings. The following sample specifies an applicationIdSuffix for the debug build type, and configures a "staging" build type that is initialized using settings from the debug build type.
You can use same build.gradle for release and debug modes like:
buildTypes {
release {
// Do whatever you want to do in release mode
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
// Do whatever you want to do in debug mode
applicationIdSuffix ".debug"
debuggable true
}
/**
* The `initWith` property allows you to copy configurations from other build types,
* then configure just the settings you want to change. This one copies the debug build
* type, and then changes the manifest placeholder and application ID.
*/
staging {
initWith debug
manifestPlaceholders = [hostName:"internal.example.com"]
applicationIdSuffix ".debugStaging"
}
}
Ref: Source
buildTypes {
release {
shrinkResources true
minifyEnabled true
debuggable false
signingConfig signingConfigs.releaseConfig
}
debug1 {
debuggable true
signingConfig signingConfigs.debug
}
debug2 {
debuggable true
signingConfig signingConfigs.debug
}
}
You came different build variants like above and on Android studios select the build variants option(usually in the bottom left corner). select which variant you want to build the apk and run the app.

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?

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

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

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