Gradle 常用配置
签名
当程序中使用了第三方的功能,比如分享、地图时,要求程序必须是已签名的
在Gradle中配置签名后,可以使我们的Debug也带上签名,而不需要使用Build/Generate Signer Apk去编译带签名的APK
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| signingConfigs { signConfig { storeFile file ('../key.jks') storePassword '123456' keyAlias 'xdj' keyPassword '123456' } } buildTypes { release { signingConfig signingConfigs.signConfig } debug { signingConfig signingConfigs.signConfig } }
|
1 2
| // 终端命令 keytool -list -v -keystore ./key.jks
|
输出结果

Tips
还可通过在AndroidStudio自带终端中运行 ./gradlew signingReport
命令获取
1 2
| jarsigner -verify -certs -verbose ./app/build/outputs/apk/app-googleplay-debug.apk
|
输出结果

Tips:
${keyAlias}为CN=Android Debug 说明APK未签名
多渠道打包
以友盟为例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| defaultConfig { manifestPlaceHolders=[UMENG_CHANNEL_VALUE:'Umeng'] } productFlavors { googleplay { manifestPlaceHolders=[UMENG_CANNEL_VALUE:'Google Play'] } wandoujia{ manifestPlaceholders = [UMENG_CHANNEL_VALUE: "Wandoujia"]
} xiaomi{ } tencent{ } }
|
1 2
| <meta-data android:name="UMENG_CHANNEL" android:value="${UMEMG_CHANNEL_VALUE}"/>
|
- 编译命令
./gradlew assemble
: 编译所有渠道的Debug和Release
./gradlew assembleWandoujia
: 编译豌豆荚的Debug和Release
./gradlew assembleWandoujiaDebug
: 编译豌豆荚的Debug
./gradlew build
:
Tips
貌似Android Studio
上有多渠道打包的插件,但最终还是需要使用命令行
###自定义APK名称
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| def releaseTime() { return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC")) }
buildTypes { applicationVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk') && outputFile.name.indexOf('debug') == -1) { def fileName = outputFile.name.replace(".apk", "-v${defaultConfig.versionName}-build${defaultConfig.versionCode}-" + "${releaseTime()}.apk") output.outputFile = new File(outputFile.parent, fileName) } } } }
|
导入包
导入aar、jar、modle
1 2 3 4 5 6 7 8 9 10
| dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'group:name:version' compile 'group:name:1.1.+' compile(group:name:version) { exclude module: 'support-v4' } compile(name:'xxx', ext:'aar') compile project(:modle) }
|
Tips
+
表示最新的版本号,曾经遇到过在Windows下导入包是输入的时准确的版本号1.0.0
没有问题,到了Mac
上的就提示找不到包,后来改成 1.0.+
就好了
导入 *.so
- Android Studio 默认路径:
Project
视图模式下./app/src/main/jniLibs
这是我目前使用的方法,直接把so包放入文件夹即可,不需要其他操作

- 自定义路径
在Eclipse中的习惯是讲so包放入libs文件夹中,这在Android Studio中也可以通过配置Gradle实现
build.gradle1 2 3 4 5 6 7
| sourceSets { main {
jniLibs.srcDirs=['./libs/jniLibs'] } }
|
其他配置
1 2 3 4 5 6 7 8 9 10 11 12 13
| idea { module { downloadSources = true downloadJavadoc = false } }
eclipse { classpath { downloadSources = true downloadJavadoc = false } }
|
完整build.gradle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| apply plugin: 'com.android.application'
def releaseTime() { return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC")) }
android { compileSdkVersion 22 buildToolsVersion "23.0.1"
signingConfigs { signConfig { storeFile file ("../key.jks") storePassword "123456" keyAlias "xdj" keyPassword "123456" } }
defaultConfig { applicationId "com.xdj.rxjavademo" minSdkVersion 15 targetSdkVersion 22 versionCode 1 versionName "1.0.0"
manifestPlaceholders=[UMENG_CHANNEL_VALUE: 'Umeng'] }
buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' signingConfig signingConfigs.signConfig zipAlignEnabled true
} debug { buildConfigField "boolean", "LOG_DEBUG", "false" signingConfig signingConfigs.signConfig } applicationVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk') && outputFile.name.indexOf('debug') == -1) { def fileName = outputFile.name.replace(".apk", "-${releaseTime()}" + "-v${defaultConfig.versionName}-build${defaultConfig.versionCode}" + ".apk") output.outputFile = new File(outputFile.parent, fileName) } } } }
productFlavors { googleplay { manifestPlaceholders = [UMENG_CHANNEL_VALUE: "Google Play"] } xiaomi {
} _360 {
} tencent {
} baidu {
} }
sourceSets { main { jniLibs.srcDirs=['./libs/jniLibs'] } } }
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:22.2.1' compile 'com.android.support:design:22.2.1' compile 'io.reactivex:rxjava:1.1.0' compile 'io.reactivex:rxandroid:1.1.0' compile 'com.android.support:recyclerview-v7:22.2.1' }
|
gradle bin镜像地址
https://mirrors.aliyun.com/macports/distfiles/gradle/
来做第一个留言的人吧!