***********************************************
* 打包
* 多渠道打包
https://segmentfault.com/a/1190000002910311
http://tech.meituan.com/mt-apk-packaging.html
***********************************************
* signingConfigs 签名
** https://segmentfault.com/a/1190000002910311
1. 先创建个秘钥:菜单栏Build|Generate Signed APK|
a) Keystore path: F:\workspace_as\release.jks。Create New可以创建一个新的,或者Choose Existing选择从前创建好的。这里以release版本签名举例。
b) Keystore password: 123456(秘钥文件的密码?)
c) Keyalias: release(秘钥别名)
d) Keypassword: 123456(秘钥密码)
e) Rememberpasswords 记住密码
Next
f) APKDestination Folder: F:\workspace_as\ProjectName(选择工程目录)
g) BuildType: release (选择编译类型,是debug还是release)
h) SignatureVersions: 一般选V2(FullAPK Signature)
Finish
2. app右键| Signing(右侧第二个标签)| +绿色加号|
a) Name:release
b) KeyAlias: release(秘钥别名)
c) KeyPassword:123456 (秘钥密码)
d) StoreFile: F:\workspace_as\release.jks(秘钥文件)
e) StorePassword:123456(秘钥文件的密码?)
f) BuildType(右侧第四个标签):选择中间框里面的release,debuggable:false,是否可以调试;Signing Config:release,签名的版本。
以上2个步骤两个步骤写完后,代码里面会自动添加。也可以直接在代码里面写,例如:
signingConfigs {
debug {
}
release {
storeFilefile("../yourapp.keystore")
storePassword "yourpassword"
keyAlias "your alias"
keyPassword "yourpassword"
}
}
buildTypes {
debug {
minifyEnabled false
zipAlignEnabled false
shrinkResources false
signingConfig signingConfigs.debug
}
release {
minifyEnabled true//混淆编译
zipAlignEnabled true
//移除无用的资源文件
shrinkResources true
signingConfigsigningConfigs.release
proguardFilesgetDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
一般填上上面的代码即可执行签名,但是这种方式不太安全,建议不要在build.gradle文件中写上签名文件的密码,因为build.gradle文件一般都会集成到代码的版本控制中,这样所有人都会有签名文件的密码。
所以应该把签名文件的密码隔离起来,写到一个配置文件中,此配置文件不包含在代码版本控制中,这样其他开发者就不会知道签名文件的密码。
gradle配置文件一般以.properties结束,我们先新建一个signing.properties文件,内容如下:
STORE_FILE=yourapp.keystore
STORE_PASSWORD=yourpassword
KEY_ALIAS=your alias
KEY_PASSWORD=your password
注意没有字符串双引号""
接下在guild.gradle文件中读取signing.properties配置文件,读取的代码如下:
File propFile =file('signing.properties');
if (propFile.exists()) {
def Properties props = new Properties()
props.load(new FileInputStream(propFile))
if (props.containsKey('STORE_FILE')&& props.containsKey('STORE_PASSWORD') &&
props.containsKey('KEY_ALIAS')&& props.containsKey('KEY_PASSWORD')) {
android.signingConfigs.release.storeFile = file(props['STORE_FILE'])
android.signingConfigs.release.storePassword = props['STORE_PASSWORD']
android.signingConfigs.release.keyAlias= props['KEY_ALIAS']
android.signingConfigs.release.keyPassword= props['KEY_PASSWORD']
} else {
android.buildTypes.release.signingConfig = null
}
} else {
android.buildTypes.release.signingConfig =null
}
代码很简单,就是读取文件,然后拿到签名需要的四个变量值分别赋值即可。