How to use new Android 2.0 Shrinker and set useProguard false? - android

I upgraded to Android Studio 2.0 Beta 5. I'm using gradle tools 2.0.0-beta5. On the Android dev blogspot site, there is a video explaining how to use the new Shrinker for debug builds (at 3:14)
https://www.youtube.com/watch?list=PLWz5rJ2EKKc_w6fodMGrA1_tsI3pqPbqa&v=xxx3Fn7EowU
I'm trying to build my project with the debug buildType as explained in the video:
debug {
minifyEnabled true
useProguard false
}
I'm getting a bunch of warnings and then this this error when building:
Error:Execution failed for task 'app:transformClassesWithNewClassShrinkerForMyAppNameGoesHereDebug'.
Warnings found during shrinking, please use -dontwarn or -ignorewarnings to suppress them.
With Proguard, I would add the appropriate -dontwarn necessary in the proguard project file. Is there a Shrinker project file somewhere that I can add -dontwarn statements?

I found some documentation on this. Turns out it uses the same configuration files as Proguard. Here is the relevant part from the doc:
The built-in shrinker can only remove dead code, it does not obfuscate or optimize. It can be configured using the same files as ProGuard, but will ignore all flags related to obfuscation or optimization. Unlike ProGuard, we support using the built-in shrinker together with Instant Run.
Also, here is a sample configuration for reference:
buildTypes {
debug {
minifyEnabled true
useProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

Related

Gradle : DSL element 'useProguard' is obsolete and will be removed soon

Since the 3.5 update of Android Studio, I have this warning when building my app :
DSL element 'useProguard' is obsolete and will be removed soon. Use
'android.enableR8' in gradle.properties to switch between R8 and
Proguard..
Removing "useProguard" from build.gradle fixed the problem for me, like:
release {
minifyEnabled true
//useProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
Update 2022, or Details; R8 is nowadays:
By default enabled,
And it simply replaces ProGuard,
But supports existing .pro files (and there should be no need to reconfigure).
Also, any way to disable R8, or use ProGuard instead, is deprecated (or even removed).
But debug flavors can set debuggable true, and continue to be debuggable line-by-line.
set the following in your project's gradle.properties file
android.enableR8=true
R8 also has full mode that is not directly compatible with Proguard. In order to try that out you can additionally set the following in your gradle.properties file
android.enableR8.fullMode=true
This turns on more optimizations, that can further reduce app size. However, you might need a few extra keep rules to make it work.
At a glance, when you build you project using Android Gradle plugin 3.4.0 or higher, the plugin no longer uses ProGuard to perform compile-time code optimization. Instead, the plugin works with the R8 compiler by default to handle the Shrink, obfuscate, and optimize your app. However, you can disable certain tasks or customize R8’s behavior through ProGuard rules files.
In fact, R8 works with all of your existing ProGuard rules files, so updating the Android Gradle plugin to use R8 should not require you to change your existing rules.
When you use Android Studio 3.4 or Android Gradle plugin 3.4.0 and higher, R8 is the default compiler that converts your project’s Java bytecode into the DEX format that runs on the Android platform. However, when you create a new project using Android Studio, shrinking, obfuscation, and code optimization is not enabled by default. You may enable them using the below code -
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'
}
}
...
}
For the more adventurous, R8 also has full mode. In order to try that out you can additionally set the following in your gradle.properties file.3
android.enableR8.fullMode=true
This turns on more optimizations, that can further reduce app size. However, you might need a few extra keep rules to make it work. Learn more here - https://youtu.be/uQ_yK8kRCaA
R8 is the default tool available in Android Studio 3.4 onwards. There is no need to explicitly enable R8. Just remove the useProguard true line from the app/build.gradle file.
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

When using R8 on Android, do I need to uninstall my existing Proguard?

Do I need to remove Proguard related code from Gradle when using R8?
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
You don't actually remove the proguard rules, as R8 works with Proguard rules in compat mode. For more details, please refer to https://android-developers.googleblog.com/2018/11/r8-new-code-shrinker-from-google-is.html.
R8 is available with Android Studio 3.3 beta and works with Proguard rules. To try it, set the following in your project's gradle.properties file:
android.enableR8=true
But for the full mode, it is not directly compatible with Proguard.
Edit #1
Check here for how to migrate Proguard to R8: Android/java: Transition / Migration from ProGuard to R8?

What's the difference between "minifyEnabled" and "useProguard" in the Android Plugin for Gradle?

I see that the Android Plugin for Gradle has a minifyEnabled property as well as a useProguard property, as follows:
android {
buildTypes {
debug {
minifyEnabled true
useProguard false
}
release {
minifyEnabled true
useProguard true
}
}
}
What's the difference between these two properties? Or, rather, what's the meaning of each?
Quoting from tools.android.com:
Built-in shrinker
Version 2.0 of Android Plugin for Gradle ships with an experimental
built-in code shrinker, which can be used instead of ProGuard. The
built-in shrinker supports fast incremental runs and is meant to speed
up iteration cycles. It can be enabled using the following code
snippet:
android {
buildTypes {
debug {
minifyEnabled true
useProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt')
}
}
}
The built-in shrinker can only remove dead code, it does not obfuscate or optimize. It can be configured using the same files as
ProGuard, but will ignore all flags related to obfuscation or
optimization.
Unlike ProGuard, we support using the built-in shrinker together with
Instant Run: depending on the project, it may significantly decrease
the initial build and install time. Any methods that become reachable
after a code change will appear as newly added to the program and
prevent an Instant Run hotswap.
You don't need useProguard true anymore.
Code shrinking with R8 is enabled by default when you set the minifyEnabled property to true.
When you build your project using Android Gradle plugin 3.4.0 or higher, the plugin no longer uses ProGuard to perform compile-time code optimization. Instead, the plugin works with the R8 compiler to handle the tasks according to the official document.
If you want to use ProGuard instead of R8. Add this line in the gradle.properties file
android.enableR8=false
I set minifyEnabled true for my release buildType and it removed an entire enum which it thougt to be unused code i guess. This made my app crash due to a NoSuchFieldException. Took me 4 hours to find the reason for this crash. 0/10 can not recommend minifyEnabled.
Just enable minifyEnabled will have code both optimized and obfuscated.
This is because useProguard true is default so no need to set it explicitly.
See also:
Obfuscation in Android Studio

proguard gradle debug build but not the tests

I enabled proguard for the debug build using:
android {
buildTypes {
debug {
runProguard true
proguardFile 'proguard-debug.txt'
}
release {
runProguard true
proguardFile 'proguard-project.txt'
zipAlign true
}
}
}
The problem I'm experiencing when I do this is that the gradle build wants to proguard the tests during the proguardDebugTest task as well. I can't seem to modify to get access to this particular task. Is there a way I can proguard the debug apk but not the test apk?
runProguard is old. It was replaced with minifyEnabled
With minifyEnabled (and other changes in new versions of gradle) you will may encounter issues where the proguard config works for your debug apk but not for the instrumentation tests. The apk created for instrumentation tests will use its own proguard file, so changing your existing proguard file will have no effect.
In this case, you need to specify the proguard file to use on the instrumentation tests. It can be quite permissive because it's not affecting your debug and release builds at all.
// inside android block
debug {
shrinkResources true // removes unused graphics etc
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
testProguardFile('test-proguard-rules.pro')
}
Introduce a new build type "derived" from debug specific to the test app that disables ProGuard again like
android {
buildTypes {
debugTest.initWith(debug)
debugTest {
minifyEnabled false
}
}
}
and use that build type for the test app by assigning its name to the testBuildType property
android {
testBuildType 'debugTest'
}
Put
gradle.projectsEvaluated {
proguardDebugTest.enabled = false
}
it in your build script.
There are two things to know here:
The general Gradle feature to enable / disable tasks.
The Android Gradle plugin specific deferred creation of tasks in afterEvaluate, so you need to also defer disabling of the task to afterEvaluate.
EDIT:
One small note: It disables the task but fails the build. This is because the :preDexDebugTest task wont run with proguard on. The best solution i've found so far is to have debug specific proguard config. More details here. Create a separate proguard config file, include the regular proguard file like so:
-include proguard.cfg
and add test config. For me it was:
-dontwarn org.mockito.**
-dontwarn sun.reflect.**
-dontwarn android.test.**

Obfuscation in Android Studio

Is there any obfuscation tool to use with Android Studio? IntelliGuard plugin is declared to be supported by the Studio, but it doesn't work actually due to missing AntSupport plugin. I wan't able to find one in the repository. Any ideas?
P.S. Android Studio build process is based on Gradle, so I wouldn't expect to see Ant support there at all. May be I'm wrong.
Basic Obfuscation
To obfuscate code in Android studio just go to your build.gradle file in your Android Studio project:
Change the minifyEnabled property from false to true
This is a basic Obfuscation.
After generating the apk you can see the obfuscation result by decompiling the apk with any software. This page could help you:
http://www.decompileandroid.com/
In the obfuscation result you will see classes with name: a,b,c....
And the obfuscation variables and methods will have also names like aa,c,ac...
Normal obfuscation:
To obfuscate the code in a more complex form you could go to your root directory app and create a .pro file. For example in the following picture I have created the file: proguard-rules-new.pro. In the same directory you should see a file called proguard-rules.pro
Now add the file you have created to the build.gradle file
And edit the .pro file you have create with your own custom proguard rules
First enable minifyEnabled in your build.gradle file, like
minifyEnabled true
After this, add below lines in progurad-rules.txt file
-keep class yourpackage.** { *; }
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose
For checking that its working fine go to:
http://www.javadecompilers.com/apktool website so that you can verify after decompilation.
It will work and your classes will be hidden completely.
Update: R8 is by default enabled in android studio version 3.4.0 and above
In android studio 3.4+, R8 is enabled by default so no need to add additional property though you can opt for deep optimizations by adding fullMode property in gradle.properties as:
android.enableR8.fullMode=true
You can disable R8 and enable proguard by adding following properties in gradle.properties as:
android.enableR8 = false
useProguard = true
Android September 2018 release a new tool R8 shrinker and obfuscation tool.
R8 - R8 is a java code shrinker and minifying tool that converts java byte code to optimized dex code
For AS version below 3.4.0.
Open gradle.properties
Add android.enableR8 = true
as
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
android.enableR8 = true
Minimum Requirements:
Android studio 3.2 September 2018 release or above
Java 8
R8 Tool
R8 supports Proguard:
Keep in mind, R8 is designed to work with your existing ProGuard rules, so you’ll likely not need to take any actions to benefit from R8. However, because it’s a different technology to ProGuard that’s designed specifically for Android projects, shrinking and optimization may result in removing code that ProGuard may have not. So, in this unlikely situation, you might need to add additional rules to keep that code in your build output.
To Disable R8 in AS 3.4.0 and above:
# Disables R8 for Android Library modules only.
android.enableR8.libraries = false
# Disables R8 for all modules.
android.enableR8 = false
Note: For a given build type, if you set useProguard to false in your app module's build.gradle file, the Android Gradle plugin uses R8 to shrink your app's code for that build type, regardless of whether you disable R8 in your project's gradle.properties file.
Proguard is well-supported on Android studio. You have to configure Gradle to run it. Instructions: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Running-ProGuard
after setting minifyEnabled to true there are two version of apk you can get, so that you have to add debug option in your build.gradle to obfuscate debug one:
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
sync, build and build apk

Categories

Resources