解决Android Studio配置签名出现的问题

来源:互联网 发布:淘宝客服用语 物流慢 编辑:程序博客网 时间:2024/06/05 09:53

对于Android Studio中的gradle,相信都知道的他的强大与便捷了,但是由于这个东西是groovy语言(其实就是Java的变种版)写的,很多使用上不是很明白,再加上文档都不是很清除甚至官方文档都有误,坑爹啊。最近打算使用签名的功能,于是在模块的build.gradle文件中增加如下的配置:

def keystorePropertiesFile = rootProject.file("keystore.properties")def keystoreProperties = new Properties()keystoreProperties.load(new FileInputStream(keystorePropertiesFile))android {    signingConfigs {        config {            keyAlias keystoreProperties["keyAlias"]            keyPassword keystoreProperties["keyPassword"]            storeFile keystoreProperties['storeFile']//关键地方            storePassword keystoreProperties["storePassword"]        }    }...
注意“keystore.properties”文件是在项目的根目录,其内容如下:

storePassword=123456keyPassword=123456keyAlias=myKeyAliasstoreFile=D\:\\Xanthuim\\APP\\arshowbaby.jks
原始漫天欢喜的同步一下,结果提示这样的错误:

Error:Could not find method storeFile() for arguments [D:\Xanthuim\APP\arshowbaby.jks] on SigningConfig_Decorated{name=config, storeFile=null, storePassword=null, keyAlias=myKeyAlias, keyPassword=123456, storeType=null, v1SigningEnabled=true, v2SigningEnabled=true} of type com.android.build.gradle.internal.dsl.SigningConfig.

说什么找不到storeFile()方法,其实这个就是上面的“storeFile”。那为什么会提示这个错误呢,而且谷歌文档就是这么写的啊!最后觉得这个应该是一个文件路径,那我添加一个file(...)行不行呢?

storeFile file(keystoreProperties['storeFile'])

同步一下,搞定!至于官方文档大家可以去看下,如下:

https://developer.android.com/studio/publish/app-signing.html?hl=zh-cn#release-mode

至此,大家可能觉得这个没什么,殊不知你不知道gradle的强大,其实你在上面的signingConfigs中是可以配置多个签名的,也就是你可以对于不同情形使用不同的签名。什么意思?比如说你使用第三方的SDK的时候,是需要一个签名,而调试的时候需要一个签名,那么你可以配置多个签名项,然后在“buildTypes”中来进行配置的。最后贴一下我的配置清单:

apply plugin: 'com.android.application'//keystore.properties是在根目录,其实也可以配置在系统环境变量里面,然后下面去读取def keystorePropertiesFile = rootProject.file("keystore.properties")def keystoreProperties = new Properties()keystoreProperties.load(new FileInputStream(keystorePropertiesFile))android {    signingConfigs {        release {            keyAlias keystoreProperties["keyAlias"]            keyPassword keystoreProperties["keyPassword"]            //注意下面的file(),否则无法使用            storeFile file(keystoreProperties['storeFile'])            storePassword keystoreProperties["storePassword"]        }        debug {        }    }    compileSdkVersion 25    buildToolsVersion "25.0.2"    defaultConfig {        applicationId "com.tencent.mobileqq"        minSdkVersion 18        targetSdkVersion 25        versionCode 1        versionName "1.0"        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"        vectorDrawables.useSupportLibrary = true    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'            signingConfig signingConfigs.release        }        debug {            //...这里可以配置debug模式            signingConfig signingConfigs.debug        }    }}dependencies {    compile fileTree(include: ['*.jar'], dir: 'libs')    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {        exclude group: 'com.android.support', module: 'support-annotations'    })    compile 'com.android.support:appcompat-v7:25.2.0'    compile 'com.android.support:design:25.2.0'    compile 'com.android.support:support-vector-drawable:25.2.0'    testCompile 'junit:junit:4.12'}

更多的配置说请参考官方文档:https://developer.android.com/studio/build/build-variants.html?hl=zh-cn#product-flavors

补充:

今天打开CSDN就看到有人针对多渠道打包写了一遍更详细的例子,算是我这篇文字的补充吧,大家可以去看下。http://blog.csdn.net/u012551350/article/details/62041833

0 0