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'
}
}
Related
I was reading an article about Proguard in Android Studio their is a line
Proguard comes out of the box in android Studio
Can anyone please explain this to me? If you are interested to read the completed article I'll put the link below.
Article Link
It means that proguard is bundled together with your default android project and you do not need to download it from outside via gradle or maven
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.txt'),
'proguard-rules.pro'
}
}
...
}
Ref:More on proguard usage in android
I am trying to view APK contents using Android Studio's APK analyzer tool.
According to https://developer.android.com/studio/build/apk-analyzer, APK would contain dex files from which class information can be viewed.
With my sample application, I am able to see .java classes directly in the APK. Refer APK files.
I want to avoid having the JAVA classes as part of APK to reduce APK size and for security reasons.
Use like this in your build.gradle -
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'
}
}
...
}
Do read google Docs for better understanding. You will get a clear picture.
Follow this -
https://developer.android.com/studio/build/shrink-code
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?
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
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