2011-03-17 52 views
24

我在一个文件夹中有十个Android项目。对于每个项目,我可以使用ant debug来构建它。因此编写一个简单的脚本来编译所有这些项目是没有问题的。我使用哈德森每天建立这些项目,并且它工作正常。Android自动化发布版本

但是现在我们的项目需要进入发布阶段。所以编译命令变成ant release。为编译发布项目,我必须在编译期间每次输入证书的密码。所以我不能做发布的自动化。

这个编译工作杀了我,因为我有10个项目都需要与输入密码进行交互。

如何让发布版本仍然是自动的?

+0

这是一个自动输入密码的教程:http://helpmeco.de/2012/2/compiling-and-signing-android-release-apk-from-the-command-line。 – browep 2012-02-29 23:00:23

回答

31

假设您使用的是最新的Android工具,比如v9或v10。

如果您在Android SDK目录看tools/ant/main_rules.xml

<!-- called through target 'release'. Only executed if the keystore and 
    key alias are known but not their password. --> 
<target name="-release-prompt-for-password" if="has.keystore" unless="has.password"> 
    <!-- Gets passwords --> 
    <input 
      message="Please enter keystore password (store:${key.store}):" 
      addproperty="key.store.password" /> 
    <input 
      message="Please enter password for alias '${key.alias}':" 
      addproperty="key.alias.password" /> 
</target> 

<!-- called through target 'release'. Only executed if there's no 
    keystore/key alias set --> 
<target name="-release-nosign" unless="has.keystore"> 
    <echo>No key.store and key.alias properties found in build.properties.</echo> 
    <echo>Please sign ${out.unsigned.file} manually</echo> 
    <echo>and run zipalign from the Android SDK tools.</echo> 
</target> 

搜索在XML文件中has.keystore显示:

<!-- properties for signing in release mode --> 
<condition property="has.keystore"> 
    <and> 
     <isset property="key.store" /> 
     <length string="${key.store}" when="greater" length="0" /> 
     <isset property="key.alias" /> 
    </and> 
</condition> 
<condition property="has.password"> 
    <and> 
     <isset property="has.keystore" /> 
     <isset property="key.store.password" /> 
     <isset property="key.alias.password" /> 
    </and> 
</condition> 

所以我假设你有四个定义传递给build.xml:key.store,key.alias,key.store.password, key.alias.password

为了安全起见,切记不要在命令行上传递这些定义。 :)

1

只是一个说明......我不想在道具文件中设置密码,默认情况下,您的密码将在命令行上回显,这也是一个问题。将SecureInputHandler的使用添加到您的main_rules.xml中,以便在命令行上不公开您的密码。

<target name="-release-prompt-for-password" if="has.keystore" unless="has.password"> 
    <!-- Gets passwords --> 
    <input 
      message="Please enter keystore password (store:${key.store}):" 
      addproperty="key.store.password" > 
     <handler classname="org.apache.tools.ant.input.SecureInputHandler" /> 
    </input> 
    <input 
      message="Please enter password for alias '${key.alias}':" 
      addproperty="key.alias.password" > 
     <handler classname="org.apache.tools.ant.input.SecureInputHandler" /> 
    </input> 
</target> 
1

看看this的文章,特别是当它开始提key.store.password。我用它没有麻烦。

基本上你应该在你的(构建)机器上创建一些本地的secure.properties文件,它必须保持相对安全,例如,每个人都无法访问或者没有存储在源代码管理中的每个人。该文件将密码存储为具有正确名称的属性,并将其导入项目ANT构建文件中。

8

您可以在项目文件夹的project.properties中定义您的密钥库配置。 就这样

key.store = /路径/到/你/密钥库

key.alias = yourkeyalias

key.store.password = yourkeystorepassword

key.alias。密码= yourkeyaliaspassword

+0

我可以问你一些关于你的答案的东西,只有当我有密钥时,它才适用于我,但是我怎样才能自动生成它。 – Reham 2013-03-19 06:49:10

+0

您可以使用'$ keytool -genkey -v -keystore my-release-key.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000'来生成密钥库。细节是[here](http://developer.android.com/tools/publishing/app-signing.html) – lishali 2013-03-22 05:29:20

22

为基础的摇篮建立

1)创建secure.properties文件包含你的密码:

key.store.password=<your keystore password> 
key.alias.password=<your alias password> 

你可能不希望它的版本控制之下,这就是为什么我们把密码放在一个单独的*.properties文件中。如果你不介意让你的密码在版本控制下,你可以直接输入你的密码到build.gradle,但这不是建议的,所以我没有直接显示。

2)设置你的build.gradle如下:

Properties secureProperties = new Properties() 
secureProperties.load(new FileInputStream("secure.properties")) 

android { 
    signingConfigs { 
     release { 
      storeFile file("<path to your keystore>") 
      storePassword secureProperties['key.store.password'] 
      keyAlias "<alias name>" 
      keyPassword secureProperties['key.alias.password'] 
     } 
    } 

    buildTypes { 
     release { 
      signingConfig signingConfigs.release 
     } 
    } 
} 

就是这样。 ./gradlew assembleRelease现在构建并签署我的APK,而不会提示输入我的密码。

基于Ant构建

1)创建secure.properties文件包含你的密码:

key.store.password=<your keystore password> 
key.alias.password=<your alias password> 

你可能不希望它的版本控制之下,这就是为什么我们”不要将密码放入现有的*.properties文件之一中。如果您不介意在版本控制下使用您的密码,请将这两行写入ant.properties即可。

2)创建一个custom_rules.xml文件以告知构建系统有关您的secure.properties文件。

<?xml version="1.0" encoding="UTF-8"?> 
<project name="custom_rules" default="help"> 
    <property file="secure.properties" /> 
</project> 

我不熟悉这个构建系统,所以我不知道的project元素的namedefault性质,但我相信我的选择应该为大家工作。

图2b)任何近期的Android SDK工具版本应该是好走,但如果由于某种原因,您build.xml文件不包含以下内容,你应该添加:

<import file="custom_rules.xml" optional="true" /> 

而且应该是这样。 ant release现在构建并签署我的APK,而不会提示输入我的密码。

+0

你在哪里创建custom_rules.xml?这是否被检入您的版本控制? – brianestey 2013-05-14 03:15:52

+1

@brianestey'custom_rules.xml'位于项目的根目录下,与'build.xml'一起。我将它检入版本控制。 – 2013-05-14 03:28:27

+0

感谢您的快速回复!我得到了它的工作。 =) – brianestey 2013-05-14 03:41:07