proguard-rules.pro does not seem to work with R8 - android

I upgraded my Android Studio to 3.4 earlier today, and I am using the default shrinker R8 for the first time. I copied the contents of proguard-project.txt of a library project to its proguard-rules.pro. proguard-project.txt worked flawlessly for this project that generates an aar file for use by other app projects.
File proguard-rules.pro does not seem to be used. The project has the following in its build.gradle:
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),
'proguard-rules.pro'
signingConfig signingConfigs.Release
}
debug {
signingConfig signingConfigs.Debug
}
}
proguard-rules.pro has the following:
# Preserve all public classes, and their public and protected fields and methods.
-keep public class * {
public protected *;
}
The public methods' names are not preserved at all:
Could anyone offer a tip on how to fix this?

Add this line to gradle.properties
android.enableR8 = true
And try below code inside your proguard-rules.pro
-keep public class ** {
public *;
protected *;
}
Edit #1
Check here for how to migrate Proguard to R8: Android/java: Transition / Migration from ProGuard to R8?

Related

Proguard Invalid class reference: android/support/v4/app/Fragment

I've been trying to get Proguard to work in this libgdx app of mine for hours, and I can't get past this error when I try running it:
Error:Execution failed for task
':android:transformClassesWithNewClassShrinkerForDebug'. >
com.android.build.gradle.shrinker.ClassLookupException: Invalid class
reference: android/support/v4/app/Fragment
I've tried adding a ton of different rules to proguard-rules.pro, nothing seems to work. Here are some of the rules I've tried:
dontwarn android.support.**
keep class android.support.v4.* { *; }
keep public class * extends android.support.v4.
keep public class * extends android.app.Fragment
dontwarn **CompatHoneycomb
keep class android.support.v4.** { *; }
optimizationpasses 5
keepattributes SourceFile,LineNumberTable
keep class android.support.** { *; }
Here's what my buildTypes look like in my Android build.gradle
buildTypes {
debug {
minifyEnabled true
useProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
Edit
This is definitely a LibGDX problem, I tried it with a blank LibGDX project and I get the exact same error.

Android Proguard have some trouble with AVG Antivirus

I made an android app that always worked fine. Suddenly, the AVG Antivirus reported that my app is a malware. After a couple hours, I found the issue:
In the build.gradle, if I use the config below to generate signed APK, AVG reports (the APK) as malware:
buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
But if I comment the last line, no threat is found:
buildTypes {
release {
shrinkResources true
minifyEnabled true
//proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
My proguard-rules.pro has nothing special:
-keepattributes Signature
-keepattributes *Annotation*
-keepattributes EnclosingMethod
-keep class io.codetail.animation.arcanimator.** { *; }
-keep class com.example.viewholders.** {
*;
}
-keep class com.android.vending.billing.**
-keep class cn.pedant.SweetAlert.Rotate3dAnimation {
public <init>(...);
}
-keepclassmembers class com.example.models.** {
*;
}
I'm using Android Studio 2.2.2 and my project uses Firebase. The AVG Antivirus version that I have in my android device is 5.9.0.1.224656.
The "malware" identified by AVG is Android/gp oi bccfdd.
Is there something that I can do to solve this problem?
Finally I got a solution. Just to change this line:
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
With this:
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
According to Android Studio User Guide:
"Tip: For even more code shrinking, try the proguard-android-optimize.txt file that's in the same location. It includes the same ProGuard rules, but with other optimizations that perform analysis at the bytecode level—inside and across methods—to reduce your APK size further and help it run faster."
Unfortunately, as I realized, this can cause false detections of malware by antivirus.

Differences between debuggable and not debuggable builds in android studio?

This question is related to a problem with my firebase, having the "debuggable true" option, which impact has in my program apart of enable testing?
When I configure another build or just the same but with "debuggable=false" the firebase can't be serialized. Why is this happening? Thanks
I though the proguard could be the solution but I'm using this build and changing debuggable to false is the problem.
Update
With proguard enabled or not, the result is the same with these permissions (I dont want any change in my code):
-dontwarn com.firebase.**
-dontoptimize
-keep class acr.acr_app.** { *; }
-keep class com.firebase.** { *; }
Gradle.Build
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
debuggable true
signingConfig signingConfigs.release
}
Udpate 2
I updated the libraries with no effect, somebody can help me a bit? How run app compile the app?

gradle generate error when minifying the code

when i try to set the minifyEnabled to true and try to sync gradle file i receive the follwoing error:
Error:Cause: com/android/build/gradle/tasks/AndroidProGuardTask
why i am receiving this error and how to solve it, I cant enable proGaurd in Android Studio.
build.gradle:
buildTypes {
debug {
debuggable true
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
are you using any external library files in your project? If yes, the you need to add -keep in the proguard-rules.pro file.
I have added the following code to the file:
-keep public class org.jsoup.** {
public *;
}
my proguard-rules.profile can be found here.
You can also make sure the error is because of this by disabling the minifyEnabled to false in the proguard-rules.pro file.
add this to your proguard
-dontwarn package name of you library.**
in your case use package name of Jama
for example
-dontwarn org.slf4j.**
-dontwarn org.apache.log4j.**
-dontwarn org.apache.commons.logging.**
if you can paste more error or stacktrace I will try to help more

How to use the ProGuard in Android Studio?

This is my first project in Android Studio, and the code of my apps are not obfuscated.
Im using this configuration in build.gradle file:
I'm using the Build > Generate Signed APK... with the Run Proguard checked.
And, when I have tested using the Apk_OneClick.v4.2, my code is completly easy to read:
Please, help-me. :(
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:
You can configure your build.gradle file for proguard implementation. It can be at module level or the project level.
buildTypes {
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
The configuration shown is for debug level but you can write you own build flavors like shown below inside buildTypes:
myproductionbuild{
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
Better to have your debug with minifyEnabled false and productionbuild and other builds as minifyEnabled true.
Copy your proguard-rules.txt file in the root of your module or project folder like
$YOUR_PROJECT_DIR\YoutProject\yourmodule\proguard-rules.txt
You can change the name of your file as you want. After configuration use one of the three options available to generate your build as per the buildType
Go to gradle task in right panel and search for assembleRelease/assemble(#your_defined_buildtype) under module tasks
Go to Build Variant in Left Panel and select the build from drop down
Go to project root directory in File Explorer and open cmd/terminal and run
Linux ./gradlew assembleRelease or assemble(#your_defined_buildtype)
Windows gradlew assembleRelease or assemble(#your_defined_buildtype)
You can find apk in your module/build directory.
More about the configuration and proguard files location is available at the link
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Running-ProGuard
NB.: Now instead of
runProguard false
you'll need to use
minifyEnabled false
Here is Some of Most Common Proguard Rules that you need to add in proguard-rules.pro file in Android Sutdio.
ButterKnife
-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }
-keepclasseswithmembernames class * {
#butterknife.* <fields>;
}
-keepclasseswithmembernames class * {
#butterknife.* <methods>;
}
Retrofit
-dontwarn retrofit.**
-keep class retrofit.** { *; }
-keepattributes Signature
-keepattributes Exceptions
OkHttp3
-keepattributes Signature
-keepattributes *Annotation*
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**
-keep class sun.misc.Unsafe { *; }
-dontwarn java.nio.file.*
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
Gson
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }
Code obfuscation
-keepclassmembers class com.yourname.models** { <fields>; }
Try renaming your 'proguard-rules.txt' file to 'proguard-android.txt' and remove the reference to 'proguard-rules.txt' in your gradle file. The getDefaultProguardFile(...) call references a different default proguard file, one provided by Google and not that in your project. So remove this as well, so that here the gradle file reads:
buildTypes {
release {
runProguard true
proguardFile 'proguard-android.txt'
}
}
The other answers here are great references on using proguard. However, I haven't seen an issue discussed that I ran into that was a mind bender. After you generate a signed release .apk, it's put in the /release folder in your app but my app had an apk that wasn't in the /release folder. Hence, I spent hours decompiling the wrong apk wondering why my proguard changes were having no affect. Hope this helps someone!
Enable ProGuard
[ProGuard flow]
android {
buildTypes {
release {
minifyEnabled true
proguardFiles 'proguard-rules.pro'
}
}
}
[ProGuard artefact]

Categories

Resources