可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am trying to Build my Android application with Gradle in console.
But getting below error about task ':app:transformClassesAndResourcesWithProguardForRelease':
build.gradle:
buildscript {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.google.gms:google-services:3.0.0'
}
}
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
app/build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion '24.0.2'
defaultConfig {
applicationId "com.XXX.XXX"
minSdkVersion 14
targetSdkVersion 24
versionCode 1
versionName "0.1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile('com.squareup.retrofit2:retrofit:2.1.0') {
exclude module: 'okhttp'
}
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.squareup.retrofit2:converter-moshi:2.1.0'
compile 'moe.banana:moshi-jsonapi:2.2.0'
compile 'com.squareup.moshi:moshi-adapters:1.3.1'
compile 'com.google.android.gms:play-services-maps:9.6.0'
compile 'com.android.support:multidex:1.0.1'
compile 'com.google.firebase:firebase-core:9.6.0'
compile 'com.google.firebase:firebase-crash:9.6.0'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'
gradle-wrapper.properties
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
./gradlew build --stacktrace
This is the exception I am receiving:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task
':app:transformClassesAndResourcesWithProguardForRelease'.
回答1:
Try adding this code to your proGuard rules, it worked for me
-ignorewarnings
-keep class * {
public private *;
}
The answer was posted here:
Execution failed for task ':app:transformClassesAndResourcesWithProguardForRelease
回答2:
add this code to ..your-project/app/proguard-rules.pro
-ignorewarnings
your signed apk will be generated successfully...
Update :
That's better to fix your warning messages using -dontwarn
or -keep
keys on your proguard-rules.pro
... Because if you use (maybe your libraries) java reflection in your code the application will be crashed...
回答3:
It worked for me I also had to add following in a pro-gaurd.txt file
#### -- Picasso --
-dontwarn com.squareup.picasso.**
#### -- OkHttp --
-dontwarn com.squareup.okhttp.internal.**
#### -- Apache Commons --
-dontwarn org.apache.commons.logging.**
-ignorewarnings
-keep class * {
public private protected *;
}
回答4:
I have changed nothing just comment
// shrinkResources true
// minifyEnabled true
you don't want to change any proguard file I have already searched for this issue after 2days wasted
回答5:
buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
Please remove above mentioned code in your gradle. This worked for me.
This is only for given problem.
回答6:
I got the same error message when I was trying to build release build in android after install react-native-firebase.
These are the steps I followed,
- Do all the configurations they mention in the react-native-firebase documentation.
Build an android app using this command
./gradlew assembleRelease
Got this error message.
FAILURE: Build failed with an exception.
- What went wrong: Execution failed for task ':app:transformClassesAndResourcesWithProguardForRelease'.
Job failed, see logs for details
Resolved this issue by changing this code line in android/app/build.gradle
minifyEnabled true
to this
minifyEnabled enableProguardInReleaseBuilds
This was the only change I did. It works for me.
回答7:
Use this code in 'proguard-rules.pro' may be resolve.
-keep class * {
public private protected *;
}
or
-keep class * {*;}
回答8:
In Android Studio click "Rebuild Project" in the "Build" menu.
回答9:
I have struggled with proguard-rules.pro for quite some time and am by no means a pro here!
I am posting my file to show that you should not -keep class * without any parameters as this turns off all obfuscation. You can however protect every class that extends from a certain class or any class that implements a certain interface. You can also protect any class that has a constructor having specific elements.
Please note that I did not comment every single line as I am not 100% sure of what everything does - it's more like an educated guess.
My project includes ksoap2 (okhttp3, okio, XmlPull) that's what many rules are for.
Maybe this can be a starting point for you - you still may need to put some additional rules for your classes in.
-android
-dontpreverify
-repackageclasses ''
-allowaccessmodification
-optimizations !code/simplification/arithmetic
-keepattributes *Annotation*
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
-keep public class * extends android.app.Activity
-keepclasseswithmembers class * extends com.way4net.oner.lifa.plugin.ThemedFragment
-keepclasseswithmembers class * extends com.way4net.oner.lifa.plugin.ThemedActivity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keepattributes Signature #there were 1 classes trying to access generic signatures using reflection emfehlung von proguard selbst
-keep public class * extends android.view.View {
public <init>(android.content.Context);
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
public void set*(...);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers class * extends android.content.Context {
public void *(android.view.View);
public void *(android.view.MenuItem);
}
-keepclassmembers class * implements android.os.Parcelable {
static ** CREATOR;
}
-keepclassmembers class **.R$* {
public static <fields>;
}
-keepclassmembers class * {
@android.webkit.JavascriptInterface <methods>;
}
-dontwarn okhttp3.**
-dontwarn okio.**
-dontwarn android.support.v4.**
#-dontwarn javax.annotation.**
#-dontwarn org.xmlpull.v1.**
-dontnote android.net.http.*
-dontnote org.apache.commons.codec.**
-dontnote org.apache.http.**
-dontnote okhttp3.**
-dontnote org.kobjects.util.**
-dontnote org.xmlpull.v1.**
-keep class okhttp3.** {
*;
}
-keep class org.xmlpull.v1.XmlSerializer {
*;
}
-keep class org.xmlpull.v1.XmlPullParser{
*;
}
-dontwarn org.xmlpull.v1.XmlPullParser
-keep class org.xmlpull.v1.XmlSerializer {
*;
}
-dontwarn org.xmlpull.v1.XmlSerializer
-keep class org.kobjects.** { *; }
-keep class org.ksoap2.** { *; }
-keep class okio.** { *; }
-keep class org.kxml2.** { *; }
-keep class org.xmlpull.** { *; }
回答10:
add
-ignorewarnings
in your proguard file and keep only those class that you did not want to obfuscate.
Some of the libraries suggest to keep some of their classes if you are using
proguard rules. Visit your libraries for details
回答11:
got this issue due to warning from Android
See comment:
https://github.com/flutter/flutter/issues/40218#issuecomment-531047713
add the following rule to /android/app/proguard-rules.pro:
-dontwarn android.**