签名apk安装失败,都是signature version惹的祸

签名apk安装失败,都是signature version惹的祸

新建的一个项目由于引用了一些三方库,还没打过签名包,担心混淆会有问题,准备先打个签名包试一下,结果一打出来就遇到个很让人郁闷的问题:安装失败!!!

一开始我以为是混淆的问题,因为在打包过程中还遇到过几个错误和警告,所以重点都放在了这几个问题上,以为自己排除这些错误和警告的方法不对,导致虽然能打包成功但无法安装。折腾了好久也没有弄好。后来想了一下,签名包与调试包有两个不一样的地方,一是混淆,二是加了签名。所以决定先在debug包中用同样的混淆规则混淆代码看能不能安装,结果发现这样没问题,那说明并不是混淆的问题,那就只剩下签名了,签名会有什么问题呢?都是Android Studio自动完成的,人工的参与度太少了。硬着头皮再次 Generate Signed Apk… 仔细瞧了一下选项,注意到一个 Signature Versions ,这个选项很陌生,如下图:

签名apk安装失败,都是signature version惹的祸_第1张图片

之前是扫了一眼也不知道二者是什么意思,有什么区别,就惯性地选择了 V2(Full APK Signature),而且还以为这是个单选项,其实是可多选的。结果结果就悲剧了。这次因为特地留意了一下,就点旁边的链接signature help进去看了一下,下面是点进去的原文

APK Signature Scheme v2
Android 7.0 introduces APK Signature Scheme v2, a new app-signing scheme
that offers faster app install times and more protection against 
unauthorized alterations to APK files. By default, Android Studio 2.2
and the Android Plugin for Gradle 2.2 sign your app using both APK 
Signature Scheme v2 and the traditional signing scheme, which uses JAR 
signing.

Although we recommend applying APK Signature Scheme v2 to your app, 
this new scheme is not mandatory. If your app doesn't build properly
when using APK Signature Scheme v2, you can disable the new scheme.
The disabling process causes Android Studio 2.2 and the Android Plugin
for Gradle 2.2 to sign your app using only the traditional signing 
scheme. To sign with only the traditional scheme, open the module-level
build.gradle file, then add the line v2SigningEnabled false to your 
release signing configuration:

  android {
    ...
    defaultConfig { ... }
    signingConfigs {
      release {
        storeFile file("myreleasekey.keystore")
        storePassword "password"
        keyAlias "MyReleaseKey"
        keyPassword "password"
        v2SigningEnabled false
      }
    }
  }
Caution: If you sign your app using APK Signature Scheme v2 and make further changes
to the app, the app's signature is invalidated.
For this reason, use tools such as zipalign before signing your app
using APK Signature Scheme v2, not after.

For more information, read the Android Studio documents that describe
how to sign an app in Android Studio and how to configure the build 
file for signing apps using the Android Plugin for Gradle.

看完之后说实话也没觉得这跟我的安装失败有什么关系,不过此时已引起了我的高度注意,所以就在网上搜了一下,结果一看,还真是这里的问题。

大概的原因是:

v2是android7.0引入的一个签名机制,相比v1使得apk更安全及在安装时速度更快,但有可能会引起问题,具体可能会遇到什么问题并没有说,所以这个v2签名机制并不是强制性的,原文说如果不能正确构建则可以不用v2签名,只用v1签名。

网上的说法是:

1、只勾选v1签名并不会影响什么,但是在7.0上不会使用更安全的验证方式

2、只勾选V2签名,7.0以下会直接安装完显示未安装,7.0以上则使用了V2的方式验证

3、同时勾选V1和V2则所有机型都没问题

网上的说法没有在官方渠道上看到(或许是我没找到地方),但实际现象貌似就是这样的,我试了勾选v1、v2、v1+v2三种情况,v1和v1+v2的情况是可以在7.0以下机子上安装成功的,v2不行,7.0以上的机子没试过,因为我没有这么高级的货。。。

So,找到原因后,解决办法也就有了

  • 只勾选v1
  • 同时勾选v1和v2

参考链接:

https://stackoverflow.com/questions/42648499/difference-between-signature-versions-v1jar-signature-and-v2full-apk-signat

https://developer.android.com/about/versions/nougat/android-7.0#apk_signature_v2

https://blog.csdn.net/lvshuchangyin/article/details/62227286



由于水平有限,如果文中存在错误之处,请大家批评指正,欢迎大家一起来分享、探讨!

博客:http://blog.csdn.net/MingHuang2017

GitHub:https://github.com/MingHuang1024

Email: [email protected]

微信:724360018

你可能感兴趣的:(android开发)