【问题标题】:gradlew assembleDebug requires my release key passwordgradlew assembleDebug 需要我的发布密钥密码
【发布时间】:2014-07-29 07:55:48
【问题描述】:

我按照the guide 的说明添加了签名设置。现在当我运行./gradlew assembleDebug 时,它需要我的密钥库和密钥密码,并且最后有两个 APK 文件:

  1. ./Main/build/outputs/apk/Main-debug.apk
  2. ./Main/build/outputs/apk/Main-debug-unaligned.apk

所以 Gradle 构建了我的模块的调试版本,但需要发布密钥。

构建模块的build.gradle文件如下。

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion '19.1.0'

    signingConfigs {
        release {
            storeFile file("my-release-key.keystore")
            storePassword System.console().readLine("\nKeystore password: ")
            keyPassword System.console().readLine("Key password: ")
            keyAlias "my_key"
        }
    }

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            runProguard true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            signingConfig signingConfigs.release
        }
    }
}

dependencies {
    compile 'com.android.support:support-v4:19.0.1'
    compile 'com.android.support:appcompat-v7:19.0.1'
    compile project(':Log-Wrapper')
    compile 'com.google.android.gms:play-services:+'
}

更新 #1。

https://stackoverflow.com/a/24281294/1065835 上的解决方案有效,我接受了答案。但我个人更喜欢使用here 描述的方法。发布密钥安全地存储在本地,编译发布版本时无需每次都输入密码。

【问题讨论】:

    标签: gradle build android-gradle-plugin release


    【解决方案1】:

    似乎我可以合并一些在这里和那里收集的建议......并且我得到了它的工作。

    问题似乎在于在 build.gradle 中编写 ANYWHERE 类似于:

    storePassword System.console().readLine("\nKeystore password: ")
    

    它将随时执行。

    我提出的解决方案是,在 buildTypes 块之前创建一个 signingCongif 块:

    signingConfigs {
        release {
            storeFile file("c://my.keystore")
            storePassword "" // REQUIRED otherwise cannot be overwritten
            keyAlias "myAlias"
            keyPassword "" // REQUIRED otherwise cannot be overwritten
        }
    }   
    
    buildTypes {
        [...]
    

    并以此调整 gradle 配置:

    gradle.taskGraph.whenReady { taskGraph ->
    if(taskGraph.hasTask(assembleRelease) || taskGraph.hasTask(installRelease)) {
        // Only execute when we are trying to assemble a release build
        def passKeystore = System.console().readLine("\nKeystore password: ")
        def passKey = System.console().readLine("\nKey password: ")
        android.signingConfigs.release.storePassword = passKeystore
        android.signingConfigs.release.keyPassword = passKey
    }
    

    }

    请注意:
    1. 此块位于“android {}”块之外和之前。
    2. 这仅在命令行执行中进行了测试。在没有可用控制台的情况下,似乎需要进行一些修复。

    【讨论】:

    • 还没有。既然这不是答案,请把它移到我的问题的 cmets 中好吗?
    • 我相信这不再适用于 Gradle 4.6(或者在设置值时它似乎有效,但在尝试实际签名时,无法读取密钥库)。
    【解决方案2】:

    似乎只有 3 个选项:

    1. 将我的签名配置提取到单独的伪项目
    2. 自动签名,但使用释放密钥对所有内容进行签名
    3. 在命令行手动输入密码

    这是我的解决方法,也是您实现完美平衡的第四个选择:

    apply plugin: 'com.android.application'
    
    gradle.taskGraph.whenReady { taskGraph ->
        if(taskGraph.hasTask(':app:assembleRelease')) {
            android.signingConfigs.release.storeFile = file(KEYSTORE)
            android.signingConfigs.release.storePassword = STOREPASS
            android.signingConfigs.release.keyAlias = KEY_ALIAS
            android.signingConfigs.release.keyPassword = KEYPASS
        }
    }
    
    android {
        ...
        defaultConfig {
            ...
        }
        signingConfigs {
            debug {
    
            }
            release {
    
            }
        }
        buildTypes {
        debug {
                ...
            }
            release {
                ...
                //signingConfig signingConfigs.release
            }
        }
    }
    

    这允许您仅在发布版本上使用存储的密钥和签名

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-17
      • 1970-01-01
      • 2014-12-16
      • 2014-12-11
      • 2017-07-04
      相关资源
      最近更新 更多