Using proguard with espresso/androidTest - android

I'm trying to configure proguard to use it with my espresso UI test flavor. The thing is Proguard tends to ignore my debug proguard config.
This is how the config looks:
buildTypes {
debug {
minifyEnabled true
proguardFiles 'proguard-debug.pro'
testProguardFile 'proguard-debug.pro'
signingConfig signingConfigs.release
}
}
I added testProguardFile but it doesn't seem to work on androidTest. I'm running mockDebug flavor variant. When I just run the app it works fine, but when I try to run test which is located in adnroidTest it won't run due to proguard warnings, like the proguard file wasn't processed at all and the file is pretty straight forward:
proguard-debug.pro
-dontobfuscate
-dontoptimize
-dontwarn
Before someone starts to advise me turning off proguard for debug builds: I need to have it enabled because of multidex.

If you want your test build as close as possible from the real deal, try this one:
# build.gradle
debug {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
testProguardFile 'proguard-test.pro'
}
and
# proguard-test.pro:
-include proguard-rules.pro
-keepattributes SourceFile,LineNumberTable
On other hand, if you need it only because multidex, if your are using minSdkVersion < 21, ProGuard is tied to multidex flag and run automatically.

You also need to add the default proguard rules:
proguardFiles getDefaultProguardFile('proguard-android.txt')
This line can be removed, as it is a duplicate:
testProguardFile 'proguard-debug.pro'

Related

ProGuard error: Unknown option '-if' in line 16 of file 'C:\...\proguard.txt'

I'm having some problems in building an Android app with ProGuard enabled.
This is the complete error:
Unknown option '-if' in line 16 of file 'C:\Users\Re\.gradle\caches\transforms-2\files-2.1\86eec96acf5ede717d441880a214115a\proguard.txt'
This is my build.gradle:
buildTypes {
debug {
manifestPlaceholders = [enableCrashReporting: "true"]
}
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
manifestPlaceholders = [enableCrashReporting: "true"]
}
}
And this is my proguard-rules.pro:
-ignorewarnings
-keep class * {
public private *;
}
I thought it could be a cache problem, so I've tried to Invalidate cache and restart Android Studio and I've tried also to clean the build cache with gradlew cleanBuildCachebut the problem persists.
If i disable ProGuard the build is successfull. What could it be?
Ok, I've solved removing some unused libraries.
I don't know which one was causing the problem but it has solved.

build failing on play-services:11.8.x with pro guard parser error

So it looks like there is a bug in the latest play-services to be deployed.
Does anyone know how to work around this issue?
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':myappname:transformClassesWithAndroidGradleClassShrinkerForDevelopmentDebug'.
> ProGuard configuration parser error: /Users/myusername/.gradle/caches/transforms-1/files-1.1/play-services-base-11.8.0.aar/d2ad9e16677fda9cf07a1280a66e91ca/proguard.txt line 3:88 no viable alternative at input '<fields>'
So more information. seems the problem is in the core module:
Error:Execution failed for task ':myappname:transformClassesWithAndroidGradleClassShrinkerForDevelopmentDebug'.
> ProGuard configuration parser error: /Users/myusername/.gradle/caches/transforms-1/files-1.1/play-services-base-11.8.0.aar/d2ad9e16677fda9cf07a1280a66e91ca/proguard.txt line 3:88 no viable alternative at input '<fields>'
EDIT:
The contents of the file that is causing that error is:
# b/35135904 Ensure that proguard will not strip the mResultGuardian.
-keepclassmembers class com.google.android.gms.common.api.internal.BasePendingResult {
com.google.android.gms.common.api.internal.BasePendingResult.ReleasableResultGuardian <fields>;
}
It seems the default shrinker has changed. Adding the configuration to turn on ProGuard seemed to work.
buildTypes {
release {
debuggable false
minifyEnabled true
useProguard true
...
}
debug {
debuggable true
minifyEnabled true
useProguard true
...
}
}
In addition to the above solution (which works): the issue seems related to Instant Run as well.
If you disable Instant Run, you can build your app without changing your build.gradle.
Probably, the default shrinker has changed only when building for Instant Run.
This solution helped me:
First, in app/build.gradle change useProguard to 'true'
Second, in proguard rules add line '-dontobfuscate'
buildTypes {
release {
debuggable false
minifyEnabled true
useProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
...
}
debug {
debuggable true
minifyEnabled true
useProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
...
}
}
proguard-rules.pro
-dontobfuscate
So, minify would be work, but code wouldn't obfuscate.
I am noticing that if you disable Instant Run the build still fails with the same error (if you have minify enabled but Proguard disabled to shrink your code to avoid multi-dex in the debug build). If you follow Brill Pappin answer you must enable Instant Run(and install libraries as prompted) to hit any breakpoints while debugging.
It seems enabling the shrinker as described in the Google docs now only works if you are using Instant Run with the Google Play Play Services.

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?

Android: minification obfuscating dynamically accessed classes

I am using Samsung's Motion library to create a pedometer for Samsung phones. When I create an APK without minification in the gradle configuration file the system works. However when I try to apply minification before releasing to the store,
buildTypes {
release {
minifyEnabled true
signingConfig signingConfigs.config
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
debuggable false
jniDebuggable false
}
I got errors claiming the some libraries (e.g. android.hardware.motion) do not exist.
I have checked and:
android.hardware.motion is not a standard library (probably existing only in Samsung phones?) in android.hardware
these classes are accesses dynamically (maybe because non standard?), e.g. Class.forName("android.hardware.scontext.SContextManager");
If I remove the line
minifyEnabled true
I got no error, so it must be the code obfuscation.
I have tried to add to my proguard rules file statements such as
-keep class android.hardware.** { *; }
-keep class com.samsung.android.sdk.** { *; }
but this does not seem to work.
Any idea? Thanks!
The missing classes are indeed only available on the actual device.
In order to let ProGuard process your application, you will have to include the following configuration:
-dontwarn com.samsung.android.sdk.motion.**
-dontnote com.samsung.android.sdk.motion.**
This will ignore the warnings and notes originating from the samsung sdk.

Proguard not obfuscating in Android Studio

I am trying to export signed apk, obfuscated with proguard. But after process, I can see my code and my class names in decompiler tool. Spent 1 day , but can't understand what I missed.
In my gradle file I specified :
buildTypes {
release {
apply plugin: 'maven'
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
proguard-rules.pro is empty.
Should I specify anything else to enable proguarding ? After decompilation
Change minifyEnabled to true to enable proguard in your build configuration.

Categories

Resources