为什么我的自定义构建类型会出现 apk 未签名错误

use*_*104 3 android android-gradle-plugin android-build-type

我有以下app.gradle配置:

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

    debug {
        debuggable true
        // more configs here
    }

    staging {
        externalNativeBuild {
            cmake {
                cppFlags "-DDEBUG_FLAG"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

构建staging类型给出错误:

您当前选择的变体的 apk 未签名。

use*_*104 6

显然,只有构建类型releasedebug配置了签名密钥。对于自定义构建类型,您必须通过以下方式手动设置:

1)使用debug的签名配置

staging {
    signingConfig signingConfigs.debug
    ...
}
Run Code Online (Sandbox Code Playgroud)

或者

2) 从配置了签名密钥的构建类型继承

staging {
    initWith debug
    ...
}
Run Code Online (Sandbox Code Playgroud)

或者

3)生成新密钥并创建您自己的签名配置

android {
    signingConfigs {
        keyStagingApp {
            keyAlias 'stagingKey'
            keyPassword 'stagingKeyPassword'
            storeFile file('../stagingKey.jks')
            storePassword 'stagingKeyPassword'
        }
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

然后像这样配置暂存:

staging {
    signingConfig signingConfigs.keyStagingApp
    ...
}
Run Code Online (Sandbox Code Playgroud)