android - 如果选择签名 v2,则生成的 APK 未签名

标签 android signed-apk

我正在尝试为我的应用程序生成一个签名的 APK,如果我只使用签名 V1,它就可以工作。 当我使用签名 V2 然后使用 keytool 检查 apk 时,输出是:

keytool -list -printcert -jarfile app-release.apk
Not a signed jar file

这是build.gradle:

def getProps(path) {
    Properties props = new Properties()
    props.load(project.rootProject.file(path).newDataInputStream())
    return props
}

android {
    ...
    signingConfigs {
        debug {
            try {
                Properties props = getProps('./local.properties')
                storeFile file(props.getProperty('DEBUG_STORE_FILE', ''))
                keyAlias props.getProperty('DEBUG_KEY_ALIAS', '')
                keyPassword props.getProperty('DEBUG_STORE_PASSWORD', '')
                storePassword props.getProperty('DEBUG_STORE_PASSWORD', '')
                v1SigningEnabled true
                v2SigningEnabled false // enabling this generates unsigned apk
            }
            catch (ex) {
                throw new InvalidUserDataException("You should define RELEASE_STORE_FILE, RELEASE_KEY_ALIAS, RELEASE_STORE_PASSWORD in local.properties.")
            }
        }
        release {
            try {
                Properties props = getProps('./local.properties')
                storeFile file(props.getProperty('RELEASE_STORE_FILE', ''))
                keyAlias props.getProperty('RELEASE_KEY_ALIAS', '')
                keyPassword props.getProperty('RELEASE_STORE_PASSWORD', '')
                storePassword props.getProperty('RELEASE_STORE_PASSWORD', '')
                v1SigningEnabled true
                v2SigningEnabled false // enabling this generates unsigned apk
            }
            catch (ex) {
                throw new InvalidUserDataException("You should define RELEASE_STORE_FILE, RELEASE_KEY_ALIAS, RELEASE_STORE_PASSWORD in local.properties.")
            }
        }
    }

    defaultConfig {
        ...
        // Only productionRelease flavour uses signingConfigs.release;
        // other flavours(i.e. productionDebug, developmentDebug, developmentRelease)
        // use signingConfigs.debug
        // https://stackoverflow.com/questions/30898611/gradle-signing-flavors-with-different-keys-on-android
        signingConfig signingConfigs.release
    }
    buildTypes {
        release {
            ...
        }
        debug {
            ...
            signingConfig signingConfigs.debug
        }
    }
    // Dimensions: environment can be one of [development, production]
    flavorDimensions "environment"
    productFlavors {
        development {
            ...
            signingConfig signingConfigs.debug
            ...
        }
        production {
            dimension "environment"
        }
    }
    ...
}

我还从头开始创建了一个新的 Android 项目,它也有同样的问题。

请注意,我有另一个 Android 项目可以生成一个同时选择 V1 和 V2 的签名 APK。

为什么添加签名 V2 会导致生成未签名的 APK?

最佳答案

Signature v2是在Android 7.0(24)引入的,所以当你的minSdkVersion为24或更高时,像这样同时启用v1和v2:

signingConfigs {
        debug {
            v1SigningEnabled true
            v2SigningEnabled true
        }
    }

签名的 apk 不会有 v1,它只是用 v2 签名。这可以通过 apksigner 像这样验证:

java -jar apksigner.jar verify -v your-signed-apkfile.apk

enter image description here

否则keytool无法识别signature v2,所以提示Not a signed jar file。而只有签名v2的签名apk也是一个可用apk,它只能安装在Android 7.0或更高版本上。

如果你想同时用v1和v2签名,你应该修改minSdkVersion为23或更低。

希望对您有所帮助。

关于android - 如果选择签名 v2,则生成的 APK 未签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58878407/

相关文章:

Android 每个 Layout 元素都需要一个 XLMNS 定义吗?

android - html web 仅在移动 View 中显示标题

android - 如何通过命令行对 apk 进行签名

android - 任务 ':app:transformClassesAndResourcesWithProguardForProductionRelease' 执行失败

android - 在 teamcity Android 中使用 keystore 生成已发布的 apk

java - Kotlin 中不存在 GridView.LayoutParams?

android - 将数据从 Activity 发送到 Tab View fragment

android - 如何调整 Android 位图中图像的大小?

android - 条目名称 'classes.dex' 发生冲突

android-studio - 生成签名的 APK 未显示在 Android Studio 中