标签:
百度百科说:Gradle 是以 Groovy 语言为基础,面向Java应用为主,基于DSL语法的自动化构建工具。
2、gradle的安装
gradle的官网下载:https://gradle.org/downloads
如果上面的下载慢或访问不了可以在这下载:http://www.androiddevtools.cn/
androidStudio的话,直接解压,然后把目录导进去就ok了。
eclipse的要,解压后要配置环境变量,具体的百度就好了,网上大把资料。
3、分析
下面举个例子:这个是gradle2.0之前的语法(2.0以后语法改了)
1 apply plugin: ‘android‘ 2 3 buildscript { 4 repositories { 5 mavenCentral() 6 } 7 dependencies { 8 classpath ‘com.android.tools.build:gradle:0.7.3‘ 9 classpath ‘net.sf.proguard:proguard-gradle:4.10‘ 10 } 11 } 12 13 dependencies { 14 compile fileTree(dir: ‘libs‘, include: [‘*.jar‘]) 15 } 16 17 android { 18 compileSdkVersion 21 19 buildToolsVersion "21.1.2" 20 21 defaultConfig { 22 minSdkVersion 9 23 } 24 } 25 26 //type:可以当做要使用什么工具, dependsOn:执行该动作的在compileReleaseJava执行后执行 27 task copyAssets(type: Copy, dependsOn: ‘compileReleaseJava‘) { 28 from ‘assets‘ 29 into ‘build/classes/release/assets‘ 30 } 31 32 task genJar(type: Jar, dependsOn: ‘copyAssets‘) { 33 from fileTree(dir: ‘build/classes/release‘) 34 archiveName = ‘gp_android_sdk_unproguard.jar‘ 35 } 36 37 task progJar(type: proguard.gradle.ProGuardTask, dependsOn: genJar) { 38 outjars ‘build/gp_android_sdk.jar‘ 39 libraryjars System.getenv("ANDROID_HOME")+"/platforms/android-19/android.jar" 40 41 // 具体需要keep住的类 42 configuration ‘sdk_proguard.pro‘ 43 }
在26行讲到的type:gradle的语法中有很多:什么Copy、Delete、Jar都是。具体的可看Gradle的开源文档:https://gradle.org/docs/current/javadoc/index.html
如Copy:从 src/main/doc 目录拷贝到 build/target/doc
task copyDocs(type: Copy) { from ‘src/main/doc‘ into ‘build/target/doc‘ }
是不是很简单。。其他的基础的百度看看别人的文档,这里推荐一篇:http://www.stormzhang.com/android/2014/02/28/android-gradle/
下面说说混淆:
buildTypes { release { runProguard false proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.txt‘ } }
官方把混淆写在上面代码的位置,但如果同时混淆并导出jar包,可用:(还有没有别的方法还没试过)
task genJar(type: Jar, dependsOn: ‘copyAssets‘) { from fileTree(dir: ‘build/classes/release‘) archiveName = ‘gp_android_sdk_unproguard.jar‘ } task progJar(type: proguard.gradle.ProGuardTask, dependsOn: genJar) { outjars ‘build/gp_android_sdk.jar‘ libraryjars System.getenv("ANDROID_HOME")+"/platforms/android-19/android.jar" // 具体需要keep住的类 configuration ‘sdk_proguard.pro‘ }
其实gradle编译的时候会生成jar包的:在build目录的exploded-aar下(不同版本的gradle会不一样的)回生成:classes.jar就是项目的jar包,这样你把module用gradle编译生成的classes.jar就是你要的jar包。
这里给个gradle2.0之后的例子:
1 //该脚本请使用gradle2.0以上编译 2 3 apply plugin: ‘com.android.application‘ 4 5 buildscript { 6 repositories { 7 jcenter() 8 } 9 dependencies { 10 classpath ‘com.android.tools.build:gradle:1.0.0‘ 11 classpath ‘net.sf.proguard:proguard-gradle:4.10‘ 12 } 13 } 14 15 dependencies { 16 compile fileTree(dir: ‘libs‘, include: [‘*.jar‘]) 17 } 18 19 android { 20 compileSdkVersion 21 21 buildToolsVersion "21.1.2" 22 23 defaultConfig { 24 minSdkVersion 9 25 } 26 } 27 28 task copyAssets(type: Copy, dependsOn: ‘compileReleaseJava‘) { 29 from ‘src/main/assets‘ 30 into ‘build/intermediates/classes/release/assets‘ 31 } 32 33 task genJar(type: Jar, dependsOn: ‘copyAssets‘) { 34 from fileTree(dir: ‘build/intermediates/classes/release‘) 35 archiveName = ‘gp_android_sdk_unproguard.jar‘ 36 } 37 38 task progJar(type: proguard.gradle.ProGuardTask, dependsOn: genJar) { 39 outjars ‘build/gp_android_sdk.jar‘ 40 libraryjars System.getenv("ANDROID_HOME")+"/platforms/android-19/android.jar" 41 42 // 具体需要keep住的类 43 configuration ‘sdk_proguard.pro‘ 44 } 45 46 task deleteOldJar(dependsOn: ‘progJar‘) { 47 delete ‘../app/libs/gp_android_sdk.jar‘ 48 } 49 50 task copyJar(type: Copy, dependsOn: ‘deleteOldJar‘) { 51 from ‘build/gp_android_sdk.jar‘ 52 into ‘../app/libs‘ 53 } 54 55 task sdk(dependsOn: ‘copyJar‘) { 56 }
标签:
原文地址:http://www.cnblogs.com/KennyXia/p/4331923.html