Android手動(dòng)和自動(dòng)簽名

一. 手動(dòng)簽名

  • 就是普通的簽名方式,大家經(jīng)常用的,GenerateSignedApk

二. 通過配置build文件,在build的時(shí)候自動(dòng)簽名并生成apk(module/build/outputs/apk下)

  1. 在模塊的build文件下的android塊里面配置signingConfigs
    android {
        signingConfigs {
            release {
                /*下面屬性都對(duì)應(yīng)手動(dòng)簽名時(shí)候的那些屬性*/
                keyAlias 'nickelfox'//秘鑰別名
                keyPassword 'pass'//秘鑰密碼
                storeFile file('path')//秘鑰庫位置
                storePassword 'pass'//秘鑰庫密碼
            }
        }
    }
    
  2. 為每個(gè)buildTypes指定對(duì)應(yīng)的簽名配置,如下面是給release的build指定上面配置的release簽名配置
    android {
        buildTypes {
            release {
                signingConfig signingConfigs.release
            }
        }
    }
    
  3. 進(jìn)行如上配置之后,每次build都會(huì)在module/build/outputs/apk下面生成release版本簽名之后的apk

三. 注意!注意!注意!

上面配置過程中,將秘鑰的密碼等信息直接以明文形式配置在了build文件中,是極其不安全的。所以我們應(yīng)該換種方式:利用配置文件,消除明文配置的重要信息

  1. 在工程根目錄下新建keystore.properties文件(文件名可以任?。锩媾渲萌缦滦畔ⅲ?strong>注意這里不需要引號(hào))
    storePassword=password
    keyPassword=password
    keyAlias=nickelfox
    storeFile=/Users/huanglingyu/Learning/AndroidMaterial/nickelfox.jks
    
  2. build文件讀取properties配置(一般在build文件頭部?)
    //獲取簽名配置
    def signProperties = rootProject.file("sign/keystore.properties")//配置文件路徑
    def props = new Properties()
    props.load(new FileInputStream(signProperties))
    def file = file(props['storeFile'])//jks秘鑰庫文件
    
  3. 更改signingConfigs為從配置文件里面讀取
    android {
        signingConfigs {
            release {
                //為了保護(hù)秘鑰信息的安全,這些信息不該以明文放到build文件中,下面是利用配置文件進(jìn)行配置
                if (file.exists() && signProperties.exists()) {//如果秘鑰庫文件和配置文件存在就簽名,否則不簽名
                    keyAlias props['keyAlias']//Properties['key']方法用來獲取key對(duì)應(yīng)的屬性值,就是properties文件下配置的屬性
                    keyPassword props['keyPassword']
                    storeFile file//注意這里要的是file
                    storePassword props['storePassword']
                }
            }
        }
    }
    
  4. 配置完成,最后記得在開源的時(shí)候?qū)⑴渲梦募蚸ks文件等信息放在gitignore即可

四. 生成帶有簽名的apk方法

  1. 執(zhí)行一次完整build,或者rebuild
  2. 命令行執(zhí)行g(shù)radlew assembleRelease
  3. 去module下的build/outputs/apk下找?guī)в泻灻腶pk即可

五. 完整build文件

apply plugin: 'com.android.application'

//獲取簽名配置
def signProperties = rootProject.file("sign/keystore.properties")//配置文件路徑
def props = new Properties()
props.load(new FileInputStream(signProperties))
def file = file(props['storeFile'])//jks秘鑰庫文件

android {
    signingConfigs {
        release {
            //為了保護(hù)秘鑰信息的安全,這些信息不該以明文放到build文件中,下面是利用配置文件進(jìn)行配置
            if (file.exists() && signProperties.exists()) {//如果秘鑰庫文件和配置文件存在就簽名,否則不簽名
                keyAlias props['keyAlias']//Properties['key']方法用來獲取key對(duì)應(yīng)的屬性值,就是properties文件下配置的屬性
                keyPassword props['keyPassword']
                storeFile file//注意這里要的是file
                storePassword props['storePassword']
            }
            /*下面屬性都對(duì)應(yīng)手動(dòng)簽名時(shí)候的那些屬性*/
            /*keyAlias 'nickelfox'//秘鑰別名
            keyPassword 'pass'//秘鑰密碼
            storeFile file('path')//秘鑰庫位置
            storePassword 'pass'//秘鑰庫密碼*/
        }
    }
    compileSdkVersion 26
    defaultConfig {
        applicationId "cn.foxnickel.autosigndemo"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    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:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    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'
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 這一章主要針對(duì)項(xiàng)目中可以用到的一些實(shí)用功能來介紹Android Gradle,比如如何隱藏我們的證書文件,降低風(fēng)險(xiǎn)...
    acc8226閱讀 7,969評(píng)論 3 25
  • 關(guān)于作者: 李濤,騰訊Android工程師,14年加入騰訊SNG增值產(chǎn)品部,期間主要負(fù)責(zé)手Q動(dòng)漫、企鵝電競(jìng)等項(xiàng)目的...
    稻草人_3e17閱讀 3,906評(píng)論 0 10
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,568評(píng)論 19 139
  • 1.拉伸相關(guān)肌肉 大腿,前股四頭肌,外,闊筋膜張肌,逢匠肌。后,國伸肌,內(nèi),內(nèi)收肌 小腿,拉后側(cè), 2、拉伸方法,...
    高科baby閱讀 398評(píng)論 0 0
  • 午后,我在開滿雛菊的田野上散步,左手拉著爸爸,右手拉著媽媽。 忽然,在我面前出現(xiàn)了一片霧氣迷蒙的湖水。 爸爸向左走...
    蔥花兒老師閱讀 430評(píng)論 0 1

友情鏈接更多精彩內(nèi)容