18

I am unable to generate Signed APK using minifyEnabled true and shrinkResources true

App Level : build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
    }
}
apply plugin: 'com.android.application'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 23
    buildToolsVersion '22.0.1'

    defaultConfig {
        applicationId "......."
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    configurations {
        compile.exclude group: "org.apache.httpcomponents", module: "httpclient"
    }
}

dependencies {
    compile 'com.android.support:design:23.0.1'
    compile 'com.android.support:cardview-v7:23.0.1'
    compile 'com.android.support:recyclerview-v7:23.0.1'
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.google.apis:google-api-services-youtube:v3-rev149-1.20.0'
    compile 'com.google.http-client:google-http-client-android:1.20.0'
    compile 'com.google.api-client:google-api-client-android:1.20.0'
    compile 'com.google.api-client:google-api-client-gson:1.20.0'
    compile files('libs/YouTubeAndroidPlayerApi.jar')
    compile 'com.github.clans:fab:1.6.2'
}

MessageView

Information:Gradle tasks [:app:assembleRelease]
:app:preBuild UP-TO-DATE
:app:preReleaseBuild UP-TO-DATE
:app:checkReleaseManifest
:app:preDebugBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72301Library UP-TO-DATE
:app:prepareComAndroidSupportCardviewV72301Library UP-TO-DATE
:app:prepareComAndroidSupportDesign2301Library UP-TO-DATE
:app:prepareComAndroidSupportRecyclerviewV72301Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42301Library UP-TO-DATE
:app:prepareComGithubClansFab162Library UP-TO-DATE
:app:prepareReleaseDependencies
:app:compileReleaseAidl
:app:compileReleaseRenderscript
:app:generateReleaseBuildConfig
:app:generateReleaseAssets UP-TO-DATE
:app:mergeReleaseAssets
:app:generateReleaseResValues UP-TO-DATE
:app:generateReleaseResources
:app:mergeReleaseResources
:app:processReleaseManifest
:app:processReleaseResources
:app:generateReleaseSources
:app:processReleaseJavaRes UP-TO-DATE
:app:compileReleaseJavaWithJavac
Note: .....YouTubeRecyclerViewFragment.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: ....GetPlaylistAsyncTask.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
:app:compileReleaseNdk UP-TO-DATE
:app:compileReleaseSources
:app:proguardRelease UP-TO-DATE
:app:dexRelease
:app:shrinkReleaseResources
Removed unused resources: Binary resource data reduced from 741KB to 402KB: Removed 45%
Note: If necessary, you can disable resource shrinking by adding
android {
    buildTypes {
        release {
            shrinkResources false
        }
    }
}
:app:validateExternalOverrideSigning
:app:packageRelease FAILED
Error:Execution failed for task ':app:packageRelease'.
> Unable to compute hash of ....\app\build\intermediates\classes-proguard\release\classes.jar
Information:BUILD FAILED
Information:Total time: 7.45 secs
Information:1 error
Information:0 warnings
Information:See complete output in console
Zoe
  • 27,060
  • 21
  • 118
  • 148
Sophie
  • 2,594
  • 10
  • 41
  • 75
  • Can you edit your answer with the source codes in `YouTubeRecyclerViewFregment` ? Seems you are using some deprecated APIs – Ye Min Htut Dec 25 '16 at 19:32

8 Answers8

13

You are getting

Removed unused resources: Binary resource data reduced from 741KB to 402KB: Removed 45%
Note: If necessary, you can disable resource shrinking by adding
android {
    buildTypes {
        release {
            shrinkResources false
        }
    }
}
:app:validateExternalOverrideSigning
:app:packageRelease FAILED
Error:Execution failed for task ':app:packageRelease'.

Resource shrinking works only in conjunction with code shrinking.

minifyEnabled is an Android tool that will decrease the size of your application when you go to build it .

android {

    buildTypes {
        release {
            shrinkResources true // This must be first 
            minifyEnabled true   // This must be after shrinkResources 
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-rules.pro'
        }
    }
}

If you haven't already built your app using minifyEnabled for code shrinking, then try that before enabling shrinkResources, because you might need to edit your proguard-rules.pro file to keep classes or methods that are created or invoked dynamically before you start removing resources.

Please read official Guideline about Shrink Your Code and Resources

Advice

Use latest version

compileSdkVersion 25
buildToolsVersion '25.0.1'
targetSdkVersion 25
compile 'com.android.support:appcompat-v7:25.1.0' // set other 25.1.0

NOTE

YouTubeRecyclerViewFragment.java uses or overrides a deprecated API.

Use Alternate Latest Version .

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
3

First check whether you really need to use shrinkResources

If so, do as suggested in developers link https://developer.android.com/studio/build/shrink-code.html#shrink-resources mentioned above by IntelliJ Amiya, you have to use like below

android {

    buildTypes {
        release {
            shrinkResources true  // -- always add this above minifyEnabled --
            minifyEnabled true   
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-rules.pro'
        }
    }
}
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
Sri Kanth
  • 311
  • 4
  • 18
3

Clean project and start again generate signed Build/Apk. its working fine for me.

Rajneesh Tyagi
  • 299
  • 2
  • 5
1

Try to generate signed APK using terminal by gradle command see your stack trace that will give you detailed log where has the problem.

In Windows

$gradle clean

$  gradle --stacktrace assembleRelease

In Ubuntu

$./gradlew clean

$./gradlew --stacktrace assembleRelease 

if you have still problem post you stack trace here

Rahul Devanavar
  • 3,917
  • 4
  • 32
  • 61
0

This :app:proguardRelease UP-TO-DATE .. makes this log not complete enough to say if this answer will help or not.

Please do clean + build release again to have full log with all steps, you may want also to add --info to gradle options, or even --debug to get more diagnostic messages in gradle build log.

Clean+build may also fix some weird problem of gradle/other-tool not correctly updating some file and reusing older incorrect one - rarely happens.

Also try to switch minification OFF (not solution, just experiment), whether it helps (to localize the problem is really with proguard minification and not elsewhere).

And of course if there are proguard related errors during minification, try to follow advice from the linked answer.

Community
  • 1
  • 1
Ped7g
  • 16,236
  • 3
  • 26
  • 63
0

If you cannot sign the apk in Android Studio, manually signed it with

jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.keystore my_application.apk alias_name

if you not have the key, generated it with keytool -genkey -v -keystore my-release-key.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000

You also can manually signed as the manual do:

keytool -genkey -v -keystore my-release-key.jks-keyalg RSA -keysize 2048 -validity 10000 -alias app
zipalign -v -p 4 my-app-unaligned.apk my-app.apk
apksigner sign --ks my-release-key.jks my-app.apk

check the signed apk

apksigner verify my-app.apk
LF00
  • 27,015
  • 29
  • 156
  • 295
0

Use Keytool binary or exe to generate a private keystore. Instructions in below link. You can then sign your app using this keystore. Keytool gets installed when you install Java.

http://docs.oracle.com/cd/E19509-01/820-3503/ggfen/index.html

0

Maybe one of your libraries (especially libraries which can use networking(httpClient, okHttp etc)) conflicts.. Try adding all of your libraries to a new project(do not add any code or component to your project).. If error occurs in that project too so the problem is in one of the libraries.. Try to uncomment libraries one by one..

Samir Alakbarov
  • 1,120
  • 11
  • 21