release模式配置keystore
我们点击Project Structure
点击我们项目的model,选择Signing选项
输入已创建的keystore信息
使得签名生效需配置Build Types
点击ok后,此时发现我们model下的build.gradle文件有如下的变化:
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.zxn.gaode"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
signingConfigs {
release {
keyAlias 'mgddkeystore'
keyPassword 'mgdd888'
storeFile file('D:/MyGitHubDemos/MyGaoDe-asp/MyGaoDeDemo/mgdd.jks')
storePassword 'mgdd888'
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
签名文件的密码信息配置在local.properties
上述的配置简单实用,但是不安全,假如你的项目是开源的,你把签名文件的配置密码之类的信息用明文写在build.gradle里面,不够安全
可以将签名文件的配置密码之类的信息写在local.properties下,因为在Git版本控制的项目中,我们可以看到我们项目project模式根目录下有一个.gitignore的文件,里面的配置大概如下所示
*.iml
.gradle
/local.properties
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
.DS_Store
/build
/captures
.externalNativeBuild
其中我们可以看到/local.properties,意思就是说local.properties默认是不添加到版本控制里面的,因为local.properties存储的是我们环境资源的一些相关信息,如sdk的路径。故我们可以在local.properties下配置签名信息而不用担心密钥外泄。对于开源项目来说,是非常好的。
在local.properties下直接添加相关信息
## This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Sat Apr 21 12:17:45 CST 2018
ndk.dir=C\:\\MyPrograms\\Android\\sdkAS\\ndk-bundle
sdk.dir=C\:\\MyPrograms\\Android\\sdkAS
#keystore.path= D\:/MyGitHubDemos/MyGaoDe-asp/MyGaoDeDemo/mgdd.jks
KEY_ALIAS = mgddkeystore
STORE_PASSWORD = mgdd888
KEY_PASSWORD = mgdd888
为了不用明文显示,app的build.gradle里配置
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.zxn.gaode"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
signingConfigs {
release {
Properties properties = new Properties();
properties
.load(project.rootProject.file("local.properties")
.newDataInputStream())
storePassword properties.get("STORE_PASSWORD")
keyAlias properties.get("KEY_ALIAS")
keyPassword properties.get("KEY_PASSWORD")
storeFile file('D:/MyGitHubDemos/MyGaoDe-asp/MyGaoDeDemo/mgdd.jks')
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
Signing中keystore值不设置
到此,配置完成
我们在工程进行编译打包
打包成功