84

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'.
Michele La Ferla
  • 6,775
  • 11
  • 53
  • 79
Mo Mirmousavi
  • 1,035
  • 1
  • 7
  • 13
  • This issue is coming due to pro-guard enable in the release config of the app. So the solution is either remove pro-guard or put the indicated code of snippet in the link https://readyandroid.wordpress.com/errorexecution-failed-for-task-apptransformclassesandresourceswithproguardforrelease/ – Ready Android May 02 '18 at 13:34

12 Answers12

123

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

Community
  • 1
  • 1
John Spax
  • 1,468
  • 1
  • 11
  • 10
65

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...

ultra.deep
  • 1,699
  • 1
  • 19
  • 23
  • @Richi, I don't have a *proguard-rules.pro* file. Should I create one manually? It looks like I'm generating on in runtime `release { shrinkResources true minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro', 'proguard.cfg' }` – AdamHurwitz Feb 05 '19 at 03:39
  • @AdamHurwitz Yes, you will have to create this file manually. – CopsOnRoad May 12 '19 at 09:46
  • 1
    It's worked in my case to.But I guess it's declared to something as irrelevant.I mean you may get the release apk, but the real problem is still there. – Dorbagna Jun 14 '19 at 10:02
8

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 *;
}
Ramesh R
  • 7,009
  • 4
  • 25
  • 38
Haris Durrani
  • 119
  • 1
  • 9
  • 10
    WARNING: Please don't use this answer if you want to keep your code private/protected – Bilal Ahmed Oct 02 '18 at 07:14
  • As I understand it "-keep class *" will basically turn off obfuscation and removal of classes. If you want to do this you could easily go to build.gradle and set minifyEnabled to false and remove the proguardFiles line. – FrankKrumnow Dec 21 '18 at 12:54
  • Is there now way to have minify enabled in the release @FrankKrumnow? – AdamHurwitz Feb 05 '19 at 03:21
  • Adam I am no proguard - pro myself and struggle with it quite a lot. But please have a look at my new post down there. It contains examples how you can protect certain groups of classes - like classes extending android Views. Maybe this can be a starting point. – FrankKrumnow Feb 06 '19 at 14:54
7

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

Brijesh Tanwar
  • 143
  • 1
  • 9
  • All the rest of the suggestions were a fail, this one worked. In my case I had to turn on multidex due to the inclusion of a 3rd party library, as well as upgrade to SDK 27. This error suddenly showed up. Thanks for this answer! – Stephen McCormick Jul 16 '18 at 15:22
  • 5
    minifyEnabled launch some kind of encryption for your code. Hacker has easier work when you won't keep this on. It's highly risky to disable it on production-apk file! – Adrian Grygutis Nov 08 '18 at 11:55
  • Don't use this solution. It is risky – Jack Nov 13 '18 at 13:19
  • @Gibs what risk in this? – Brijesh Tanwar Nov 15 '18 at 08:24
  • @Brijesh Tanwar : what if i need encryption?? – Jack Nov 27 '18 at 04:25
  • @Gibs I think then you have to embrace the pain in the a** that is proguard and filter through it's hundreds of unnessessary warnings and notes to find what's breaking the process without having any clue what you do - just like me ;) – FrankKrumnow Dec 21 '18 at 12:58
  • This is not resolving the problem since it basically disables ProGuard – Yannick Jun 28 '19 at 19:06
3
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.

Ayaz Muhammad
  • 138
  • 10
1

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.**

ken
  • 13,869
  • 6
  • 42
  • 36
0

Use this code in 'proguard-rules.pro' may be resolve.

-keep class * {
public private protected *;
}

or

-keep class * {*;}
  • note : above commands ignore obfuscates

  • can use this instead above for save obfuscates state

    -keepnames class * {*;}

Ali Bagheri
  • 3,068
  • 27
  • 28
0

In Android Studio click "Rebuild Project" in the "Build" menu.

Niclin
  • 1
  • 1
0

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.** { *; }
FrankKrumnow
  • 501
  • 5
  • 13
0

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,

  1. Do all the configurations they mention in the react-native-firebase documentation.
  2. Build an android app using this command

    ./gradlew assembleRelease

  3. 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.

Pranav P
  • 1,755
  • 19
  • 39
Janaka Pushpakumara
  • 4,769
  • 5
  • 29
  • 34
0

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

user3135923
  • 61
  • 1
  • 6
0

add this line to gradle properties

android.enableR8=true
Ambesh Tiwari
  • 648
  • 7
  • 11
  • Hi Ambesh. Thanks for your answer. Usually answers with an explanation are more welcomed there. Would you like to add an explanation to your answer? You may improve formatting of your answer as well. – MaxV Nov 25 '20 at 20:02