How to Use the Proguard in Android Studio

How to enable ProGuard obfuscation in Android Studio?

I figured out the problem:

Open up the proguard-rules.pro for your project and add this to the bottom:

-dontwarn java.nio.file.Files
-dontwarn java.nio.file.Path
-dontwarn java.nio.file.OpenOption
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement

Basically how I solved it was this I tried to run my app in 'release' mode and got a bunch of errors similar to this guy here: https://github.com/square/okio/issues/144

I pretty much followed what he said and fixed it.

Hope this can help others with generating their APK's!

visit more detail here :

Error:Execution failed for task ':app:packageRelease'. > Unable to compute hash of /../AndroidStudioProjects/../classes.jar

How to use the ProGuard in Android Studio?

You're probably not actually signing the release build of the APK via the signing wizard. You can either build the release APK from the command line with the command:

./gradlew assembleRelease

or you can choose the release variant from the Build Variants view and build it from the GUI:

IDE main window showing Build Variants

How can I test if ProGuard works correctly before I publish an app to Google Play?

Install and run the minified release version of your app (see here or here for info on installing AAB files) that you upload to Google Play, not the debug version.

If you're just hitting "Run" in Studio, you're installing the debug version that doesn't (by default) have Proguard or other minification run on it. If you instead use the minified release version before uploading it to Google Play, you'll get the same behavior you will after uploading: Google Play isn't running any "extra" Proguard tasks on it after you upload.

You can also use the Alpha/Beta testing tracks in Play to test the full Play experience without publishing to a wider audience or fiddling with bundletool.

Enabling ProGuard and code minification and obfuscation

When missing classes are reported at runtime for a build using R8, usually the issue it that some of the code use reflection in a way such that R8 cannot determine that a class is used by the application.

Sometimes this reflection can be in a library that the app does not have control over, and where the code is unknown. One way of moving forward in such a case is to add a -keep rule on the missing class keeping all members, and continue to do so until the app can run.

In the concrete case for this question the konsume-xml library was missing a few classes javax.xml.stream.FactoryFinder and com.bea.xml.stream.MXParserFactory at runtime, and the following rules brought them back:

-keep class javax.xml.stream.FactoryFinder {
*;
}
-keep class com.bea.xml.stream.MXParserFactory {
*;
}

Note, that this approach is no silver bullet, and in many cases actual knowledge about the reflection used in the library will be required.

In all circumstances, when issues like this happen it is always a good idea to reach out to the library developer so they can add these rules to their library. The library developer might also be able to make the rules more precise based on their knowledge on the actual use of reflection in the library. This issue was opened as a result of this question.

How to prepare a proguard file and whats included in it?

In your gradle file set true to minifyEnabled

You can define if proguard is enabled in debug, release or both

buildTypes {
release {
minifyEnabled true
proguardFiles 'proguard-rules.pro'
}
debug {
minifyEnabled false
proguardFiles 'proguard-rules.pro'
}
}

You can also set the proguardFiles to config him, check this site to see the docs about it, look this example:

# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/balysv/Documents/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

-optimizationpasses 5
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontpreverify
-verbose

If you want use a custom dictionary for code obfuscation, set this config with your dictionary file:

-obfuscationdictionary proguard-dic.txt
-classobfuscationdictionary proguard-dic.txt
-packageobfuscationdictionary proguard-dic.txt

The dictionary file is a simple text file with the labels you want to use to obfuscate your code, 1 label per line.

Pro-guard Obfuscation not working in android studio

To enable ProGuard in Android Studio. Below is the sample how to enable default ProGuard in Android Studio.

1) Go to the build.gradle file of app

2) enable the proguard minifyEnabled true and useProguard true

3) enable shrinkResources true to reduce the APK size by shrinking resources.

4) proguardFiles getDefaultProguardFile('proguard-android.txt') to enable the default one. If you want to use your own proguard file then use the below rules.

buildTypes {
release {
debuggable false
useProguard true
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}

debug {
debuggable true
useProguard true
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}


Related Topics



Leave a reply



Submit