生成已签名的APK:构建APK时出现错误 Android Studio

18

我无法使用minifyEnabled trueshrinkResources true生成已签名的APK

应用级别: 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

你能否在YouTubeRecyclerViewFregment中编辑你的答案并附上源代码?看起来你正在使用一些已弃用的API。 - Ye Min Htut
8个回答

13

你正在获得

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" 只能与代码缩减一起使用。
"minifyEnabled" 是一种安卓工具,可在构建应用程序时减小其大小。
android {

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

如果您还没有使用minifyEnabled进行代码缩小,请在启用shrinkResources之前尝试一下,因为您可能需要编辑proguard-rules.pro文件以保留在开始删除资源之前动态创建或调用的类或方法。
请阅读有关缩小您的代码和资源的官方指南。 建议 使用最新版本。
compileSdkVersion 25
buildToolsVersion '25.0.1'
targetSdkVersion 25
compile 'com.android.support:appcompat-v7:25.1.0' // set other 25.1.0

注意

YouTubeRecyclerViewFragment.java uses or overrides a deprecated API.

使用备用最新版本。


1
这个回答是在2017年的,但在2023年对我帮助很大!哇 - 钟智强
1
钟智强离开了这个领域。看到大家的评论真是太好了。 - IntelliJ Amiya

4

清理项目并重新开始生成已签名的构建/APK。 对我来说运行得很好。


3

首先检查您是否真正需要使用shrinkResources

如果是这样,请按照IntelliJ Amiya在上面提到的开发者链接https://developer.android.com/studio/build/shrink-code.html#shrink-resources中建议的方法进行操作。您需要像下面这样去使用:

android {
buildTypes { release { shrinkResources true // -- 在minifyEnabled之前始终添加此项 -- minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } }

1
尝试使用终端通过gradle命令生成已签名的APK,查看堆栈跟踪以获取详细日志并找出问题所在。

在Windows中

$gradle clean

$  gradle --stacktrace assembleRelease

在Ubuntu中,执行以下命令:$./gradlew clean


$./gradlew --stacktrace assembleRelease 

如果您仍然遇到问题,请在此处发布您的堆栈跟踪。

0

这个 :app:proguardRelease UP-TO-DATE .. 使得这个日志不够完整,无法确定 this answer 是否有帮助。

请再次进行清理+构建发布以获取包含所有步骤的完整日志,您可能还想将 --info 添加到 gradle 选项中,甚至添加 --debug 以在 gradle 构建日志中获取更多诊断消息。

清理+构建也可以修复一些奇怪的问题,例如 gradle/其他工具未能正确更新某些文件并重用旧的不正确文件 - 这种情况很少发生。

另外,尝试关闭混淆(不是解决方案,只是实验),看看是否有帮助(以确定问题是否真的与 proguard 混淆有关而不是其他地方)。

当然,如果在混淆过程中出现与 proguard 相关的错误,请尝试遵循链接答案中的建议。


0

0

也许你的某个库(尤其是可以使用网络的库(httpClientokHttp等))发生了冲突。试着将所有库添加到一个新项目中(不要向项目中添加任何代码或组件)。如果在该项目中出现错误,则问题可能出现在其中一个库中。尝试逐个取消注释库。


0

如果你无法在Android Studio中签署apk文件,可以手动进行签署

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

如果您没有密钥,请使用以下命令生成:keytool -genkey -v -keystore my-release-key.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000

您也可以像手动签名一样手动签名:

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

检查已签名的apk

apksigner verify my-app.apk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接