I am trying to enable minifying in an Android app using ProGuard.
When I set minifyEnabled to false it builds but as soon as I set it to true:
buildTypes {
debug {
minifyEnabled false
jniDebuggable true
versionNameSuffix '_DEBUG'
zipAlignEnabled true
}
release {
minifyEnabled true
proguardFile getDefaultDexGuardFile('dexguard-release.pro')
proguardFile 'dexguard-project.txt'
jniDebuggable false
signingConfig signingConfigs.release
}
I get a gradle syncing error:
ERROR: minifyEnabled is set to 'true' for variant <name of the variant>.
And stacktrace states:
org.gradle.api.ProjectConfigurationException: A problem occurred configuring project ':MainApp'
Caused by: org.gradle.api.GradleException: Errors occurred during DexGuard plugin configuration.
What does DexGuard have to do here?
Dexguard's documentation says:
DexGuard expects its inputs to be unobfuscated, so make sure none of the variants you specify here have the minifyEnabled options set to true.
You can't use both Dexugard and Proguard in the same library. You have to either disable Dexguard or Proguard.
Dexguard is an improved (and paid) version of Progurad. I don't see any reason for using Proguard when you have Dexguard.
Related
I'm building release APK with flutter run --release but I'm getting this exception
Failed to register native method io.flutter.view.FlutterNativeView.nativeRunBundleAndSource(JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V in /data/app/com.example.myapp-1/base.apk
However, debug variant runs normally so if I run the app just with flutter run everything is fine.
By the way, I'm executing flutter clean before avery build.
What is the cause of this?
Found the solution on my own, but I'm posting this answer for people who are having the same problem.
Turns out that build.gradle was causing exception
shrinkResources true // for this to work minifyEnabled must be set to true
minifyEnabled true // if set to true apk will not build
Solved it by using only proGuard so snippet below is working buildTypes section of app-level build.gradle
buildTypes {
release {
debuggable false
useProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
Why minify is not enabled by default in the gradle file of android?
Why proguard doesn't come with strictest rule by default in gradle file in android?
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
For debugging you want minifyEnabled false. Otherwise you won't be able to debug through your code while you are testing. That is why they have it false by default.
It should only be true for release version.
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.
Error:A problem was found with the configuration of task ':app:packageRelease'.
File '/Volumes/Data/Android apps/NammaKarnataka-master/app/build/intermediates/res/resources-release-stripped.ap_' specified for property 'resourceFile' does not exist.
The following error message occurred while I tried to generate signed apk on Android studio 2.2.
What additional information is required in order to solve this issue?
Add shrinkResources, set to false, to the release clause:
buildTypes {
release {
signingConfig signingConfigs.release
minifyEnabled false
shrinkResources false //ADD THIS
zipAlignEnabled true
debuggable false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
Command flutter clean helps me.
So in a few words ProGuard doesn't obfuscate sources when I build alternative buildType from Android Studio but works when I use "Generate Signed APK..." option to create apk file.
And some more details here: Android Studio 2.1.1, Gradle version: 2.10, plugin version .2.1.0
I've 3 build types with the following configuration:
buildTypes {
release {
minifyEnabled true
...
proguardFile 'proguard-rules.pro'
proguardFile getDefaultProguardFile('proguard-android.txt')
signingConfig signingConfigs.release
}
releaseDebug {
debuggable true
minifyEnabled true
...
proguardFile 'proguard-rules.pro'
proguardFile getDefaultProguardFile('proguard-android.txt')
signingConfig signingConfigs.release
}
debug {
debuggable true
minifyEnabled false
...
proguardFile getDefaultProguardFile('proguard-android.txt')
testProguardFile 'proguard-rules-test.pro'
signingConfig signingConfigs.release
}
}
I run application directly from Android Studio and have such results:
release - obfuscated
releaseDebug - NOT obfuscated
debug - not obfuscated
When I use "Generate Signed APK..." option:
release - obfuscated
releaseDebug - obfuscated
debug - not obfuscated
Is it a build system issue or I missed something?
P.S. Just for clarification, minifyEnabled is already enabled for releaseDebug build type and ProGuard is working but not in this particular case. This is not related to debug mode.
Finally, after some tests, I recognized that this issue is caused by debuggable true statement in releaseDebug configuration.
So Android Studio (or Gradle) will not use ProGuard obfuscation if you use debuggable true and minifyEnabled true statements in your alternative build type.