Generate Signed APK: Errors while building APK Android Studio
我无法使用
生成签名 APK
应用级别:build.gradle
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
buildscript { repositories { mavenCentral() } dependencies { classpath ‘com.android.tools.build:gradle:2.1.0’ } } apply plugin: ‘com.android.application’ repositories { android { defaultConfig { release { configurations { dependencies { |
消息视图
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
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/ elease//classes.jar Information:BUILD FAILED Information:Total time: 7.45 secs Information:1 error Information:0 warnings Information:See complete output in console |
你得到
1
2 3 4 5 6 7 8 9 10 11 12 |
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’. |
资源收缩只能与代码收缩结合使用。
minifyEnabled 是一个 Android 工具,可以在您构建应用程序时减小它的大小。
1
2 3 4 5 6 7 8 9 10 11 |
android {
buildTypes { |
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.
请阅读有关缩减代码和资源的官方指南
建议
使用最新版本
1
2 3 4 |
compileSdkVersion 25
buildToolsVersion ‘25.0.1’ targetSdkVersion 25 compile ‘com.android.support:appcompat-v7:25.1.0’ // set other 25.1.0 |
注意
1
|
YouTubeRecyclerViewFragment.java uses or overrides a deprecated API.
|
使用备用最新版本。
首先检查你是否真的需要使用shrinkResources
如果是这样,请按照 IntelliJ Amiya 上面提到的开发者链接 https://developer.android.com/studio/build/shrink-code.html#shrink-resources 中的建议进行操作,您必须像下面这样使用
1
2 3 4 5 6 7 8 9 10 11 |
android {
buildTypes { |
清理项目并重新开始生成签名的 Build/Apk。
它对我来说工作正常。
尝试使用终端通过 gradle 命令生成签名的 APK,查看堆栈跟踪,它会为你提供详细的日志,哪里有问题。
In Windows
1
|
$ gradle –stacktrace assembleRelease
|
In Ubuntu
1
|
$./gradlew –stacktrace assembleRelease
|
如果你在此处发布堆栈跟踪仍然有问题
可能是您的某个库(尤其是可以使用网络的库(
使用 Keytool 二进制或 exe 生成私有密钥库。以下链接中的说明。然后,您可以使用此密钥库为您的应用程序签名。安装 Java 时会安装 Keytool。
http://docs.oracle.com/cd/E19509-01/820-3503/ggfen/index.html
R.raw.Keep(xml 文件)
1
2 3 |
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" tools:shrinkMode="strict" /> |
不要忘记从 .java 源文件中引用此资源。有关更多详细信息,请参阅 https://developer.android.com/studio/build/shrink-code.html
如果您担心 apk 文件的大小,那么缩小图像文件也是一种好习惯。也就是将 RGB 通道转换为索引通道,这样可以节省多达 50% 的空间。
注意:…..YouTubeRecyclerViewFragment.java 使用或覆盖了已弃用的 API。
如果您无法在 Android Studio 中对 apk 进行签名,请使用
手动对其进行签名
1
|
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.keystore my_application.apk alias_name
|
如果你没有密钥,用
生成它
您也可以像手册那样手动签名:
1
2 3 |
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
1
|
apksigner verify my-app.apk
|
这个
请再次进行干净的构建发布以获取所有步骤的完整日志,您可能还希望将
干净的构建还可以解决一些奇怪的问题,即 gradle/other-tool 无法正确更新某些文件并重用旧的不正确的文件 – 很少发生。
还尝试关闭缩小(不是解决方案,只是实验),是否有帮助(定位问题实际上是使用 proguard 缩小而不是其他地方)。
当然,如果在缩小过程中出现与 proguard 相关的错误,请尝试遵循链接答案中的建议。
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/269022.html