I have added minifyEnabled=true to my 'release' build. The app runs correctly. And the androidTest apk runs correctly (all tests pass).
If I add any of the following to proguard-rules.pro:
-keepattributes LineNumberTable
-keepattributes LocalVariableTable
-keepattributes LocalVariableTypeTable
the app apk will build without error, but while building the androidTest apk I get ~4000 R8 "already has a mapping" errors for task:
:app:transformClassesAndResourcesWithR8ForReleaseAndroidTest
The (truncated) error log is
It appears that the methods that are getting the error are in 3rd party libraries (included as dependencies).
Thanks in advance for any help.
I solved the problem by adding an additional buildType for testing the 'release' configuration ('releaseTest'). It inherits from 'release', and sets debuggable=true.
buildTypes {
debug {
...
}
release {
...
minifyEnabled true // enable code shrinking & obfuscation
shrinkResources true // enable resource shrinking
...
}
releaseTest {
// inherit from 'release' buildType
initWith release
// for dependencies that don't know what 'releaseTest' is
matchingFallbacks = ['release']
debuggable true
}
I faced the same issue and adding
-dontoptimize
into my proguard-rules.pro just works for me.
Related
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.
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.
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'
I have an Android library project and an app, which uses that library project. In the app's build.gradle file I have ProGuard obfuscate the whole app including the library project code:
//from the app project
buildTypes{
release {
runProguard true
proguardFile 'proguard-project.txt'
}
debug{
runProguard false
}
}
All works well, the lib project and the app project both get obfuscated just fine.
However, I want to build my lib project "standalone", so I can distribute it without the app project. This of course means obfuscating the lib project on its own.
I cannot do this by using the buildTypes code in the lib-projects build.gradle and simply running proGuard, since this would break the app-project build, because then building the app would compile its code agains already-obfuscated lib-project code... :-)
What I need is a possibility to run ProGuard only if I build the lib-project in "standalone" mode, e.g. by passing a parameter, or executing a special task.
Can anyone point me in the right direction?
solved it myself. In the lib-project's build file I have two buildtypes and I publish them both:
publishNonDefault true
and the build types:
buildTypes {
debug {
debuggable true
runProguard false
}
release {
runProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
}
}
this causes the debug and release APK to be created. In my app project's build file I then add an explicit dependency to the debug configuration by stating the dependency as follows:
dependencies{
compile project(path: ':FancyLibProject', configuration: 'debug')
//...
}
Of course in the app-project I also have two build types debug and release, which in turn invoke ProGuard in the same way as for the lib-project.
Note: I omitted the signing configs for the release build - without those the APK will cannot be installed!
I am using android studio to build debug and release application.
When i build debug/release application
./gradlew assembleDebug
./gradlew assembleRelease
both build are created perfectly and run as well. Shows appropriate dialog box for debug or release
now i have added proguard details in build.gradle:
signingConfigs {
myConfig {
storeFile file("keystore.jks")
storePassword "abc123!"
keyAlias "androidreleasekey"
keyPassword "pqrs123!"
}
}
buildTypes {
release {
runProguard true
proguardFile getDefaultProguardFile('proguard-android-optimize.txt')
signingConfig signingConfigs.myConfig
}
}
productFlavors {
defaultFlavor {
proguardFile 'proguard-rules.txt'
}
}
Now it shows error in event log as
Warning: there were 7 unresolved references to classes or interfaces.
You may need to add missing library jars or update their versions.
If your code works fine without the missing classes, you can suppress
the warnings with '-dontwarn' options.
(http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedclass)
Warning: there were 2 unresolved references to program class members.
Your input classes appear to be inconsistent.
You may need to recompile the code.
(http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedprogramclassmember)
:Flash Sales:proguardDefaultFlavorRelease FAILED
If i turn the runProguard option to false then its running.
I have these questions:
1) is it ok to release apk with runProguard = false?
2) How to use dontwarn while creating release build?
When I add new lib to project Generally this How I need to define for Progaurd.
Let's say I am using Twitter4J lib then I add dontwarn this way.
-keep class twitter4j.** { *; }
-dontwarn twitter4j.**
Put the -dontwarn lines in the proguard-rules.pro or proguard-rules.txt file.
Had to google this elsewhere since none of the answers included this info. Check the other answers for the specific lines you need for your case.