07,release模式配置keystore

release模式配置keystore

我们点击Project Structure


image.png

点击我们项目的model,选择Signing选项


image.png

输入已创建的keystore信息
image.png

使得签名生效需配置Build Types


image.png

点击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值不设置


image.png

到此,配置完成
我们在工程进行编译打包


+

打包成功
image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,981评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,970评论 6 342
  • 转载注明出处:http://www.jianshu.com/p/5255b100930e 0. 前言 完全由个人翻...
    王三的猫阿德阅读 2,575评论 0 4
  • 由于您的应用签名密钥用于验证您作为开发者的身份,并确保为您的用户进行无缝而安全的更新,因此,管理和保护您的密钥对于...
    贝贝beibei96阅读 2,543评论 0 0
  • 只对着那片湖 只望着那片山 意外 在高粱铺满的原野 在红绿灯右转的夜总会 在晃晃悠悠的酒杯里 但那不是意外 那也不...
    小小仲马阅读 177评论 0 4