美文网首页移动开发Android技术知识ITBOX
Android Studio 在Gradle中隐藏Keystor

Android Studio 在Gradle中隐藏Keystor

作者: GingerBot | 来源:发表于2015-12-22 11:27 被阅读3548次

转载请注明原作者,如果你觉得这篇文章对你有帮助或启发,不用请我喝咖啡:D

1.添加releaseConfig

signingConfigs{
        releaseConfig{
            storeFile file("mykey.keystore.jks")
            storePassword "storepwd"
            keyAlias "MyApp"
            keyPassword "mypwd"

        }
    }

2.指向releaseConfig

在buildTypes中,release部分,添加sighingConfig 指向releaseConfig

buildTypes {
        release {
            minifyEnabled false
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.releaseConfig
        }
    }

3. 隐藏密码

不想暴露密码,修改步骤1中的storePassword和keyPassword为

signingConfigs{
        releaseConfig{
            storeFile file("hnt.keystore.jks")
            storePassword System.console().readLine("\nKeyStore password: ")
            keyAlias "HaniuTang"
            keyPassword System.console().readLine("\nKey password: ")

        }
    }

这样使用命令行进行打包(assembleRelease命令)会让你输入密码。

4.遇到的问题及解决

设置apk签名时从console中读取keyAlias以及密码,使用"system.console().readLine"方法,在编译时总是出现问题:

Cannot invoke method readLine() on null object"

使用命令行的时候没有问题,但是当使用android studio的时候,还是报这个错,可能是android studio默认设置gradle为deamon方式。

  • 方法1:去除优化gradle设置的./gradle/gradle.properties中的damon为true。

Reference:
http://forums.gradle.org/gradle/topics/standard_in_when_using_the_daemon

  • 方法2:加上
if(system.console() != null)

这之后再从命令行读取密码以及别名

Reference:
https://www.timroes.de/2013/09/22/handling-signing-configs-with-gradle/

5.最终脚本

最终,gradle脚本中的相关部分长这样:

signingConfigs{
        releaseConfig{
            storeFile file("mykey.keystore.jks")
            storePassword
            if(System.console() != null)
                System.console().readLine("\nKeystore password: ")
            keyAlias "MyApp"
            keyPassword
            if(System.console() != null)
                System.console().readLine("\nKey password: ")
        }
    }

相关文章

网友评论

  • 北漂的青年:我的并没有提示要我输入是什么情况,加了if的判断
  • 02a39153e40b:按楼主你的方法,并没有出现要输入密码提示,而是直接报错,不知道怎么解决
    * What went wrong:
    A problem was found with the configuration of task ':app:packageRelease'.
    > No value has been specified for property 'signingConfig.storePassword'.

    * Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug
    option to get more log output.
    GingerBot:@Blacole_1f62 我对比了一下,没有看出问题,是否signingConfigs 和 buildTypes位置放得不对导致没有引用到?如果不行,就按照他说的Run with --stacktrace option to get the stack trace. Run with --info or --debug
    option to get more log output. 用详细的日志再看看
    02a39153e40b:@GingerBot ,楼主你好,下面是我的signingConfig和buildTypes,麻烦指点一下:
    signingConfigs {
    config {
    storeFile file(RELEASE_STORE_FILE)
    storePassword
    if(System.console() != null)
    System.console().readLine("\nKeystore password: ")

    keyAlias RELEASE_KEY_ALIAS
    keyPassword
    if(System.console() != null)
    System.console().readLine("\nKey password: ")

    }
    }

    buildTypes {
    release {


    signingConfig signingConfigs.config
    }
    }
    GingerBot:从字面意思猜测,可能是signingConfig变量名印错了。方便把你的signingConfigs那一段贴上来看看吗?
  • DevWang:最终脚本用命令行之行没问题,但是用gradle命令执行不出现让我输入密码的提示,题主遇到这个问题没?
    DevWang:@GingerBot 目前来看只能用命令行来执行
    GingerBot:@我兜里有颗糖 我用gradle时就是报错。你的报错了吗?还是说可以不输入密码就成功打包了?
  • 陈晨XX:apk is not signed. Please configure the signing information for the selected
    怎么弄的?
    GingerBot:@陈晨XX 是在哪一步出的问题?字面意思看是说你的APK没有签名。是不是还没有为APK制作相应的密钥文件?

本文标题:Android Studio 在Gradle中隐藏Keystor

本文链接:https://www.haomeiwen.com/subject/hwomhttx.html