首页 > 代码库 > Gradle 编译学习笔录

Gradle 编译学习笔录

  • Gradle下载与配置

下载路径: http://www.gradle.org/downloads 

配置: 下载完gradle后, 解压缩到你的目录盘, 然后打开gradle的bin目录并将其路径复制, 打开环境变量, 将复制的内容加到Path中。

            配置完毕后,可以打开cmd命令工具, 运行gradle –version 如果出现正确的版本号 那么将配置成功 !

  • Gradle简单命令介绍 

build 编译命令: 使用cmd命令进入到项目目录下, 然后运行gradle build . 编译成功后可以在build/目下下查看文件

clean 命令: gradle clean 可以清除上一次build后的文件。

check 命令:gradle check 可以使编译用上类似checkstyle文件对项目进行检查。 

  • build.gradle 代码详细介绍

<style type="text/css">.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}.number_show{ padding-left:52px !important; list-style:decimal outside !important}.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}.number_hide{ padding-left:0px !important; list-style-type:none !important}.number_hide li{ list-style-type:none !important; border-left:0px}ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}ol.mainarea li pre.alt{ background-color:#f7f7ff !important}</style>

隐藏行号 复制代码 build.gradle
  1. buildscript {
  2.     ##### 大意就是支持java 依赖库管理(maven/ivy),用于项目的依赖。这也是gradle 强力的地方。。。
  3.     ## How does Gradle find the files for external dependencies? Gradle looks for them in a repository. 
  4.     ## A repository is really just a collection of files, organized by group, name and version.
  5.     ## Gradle understands several different repository formats, such as Maven and Ivy, 
  6.     ## and several different ways of accessing the repository, such as using the local file system or HTTP.
  7.     repositories {
  8.         #### 至少声明一个类型, 如果不用maven的话, 默认的就是这个了。 ####
  9.         mavenCentral()
  10.         
  11.         ## a remote Maven repository ##
  12.         maven{
  13.             url "http://10.10.20.10:8080/nexus/content/groups/public"
  14.         }
  15.         
  16.     }
  17.     ### 依赖什么版本的Gradle编译工具 ###
  18.     dependencies {
  19.         classpath ‘com.android.tools.build:gradle:0.9.2‘
  20.     }
  21. }
  22. #### 编译项目类型 ####
  23. apply plugin: ‘android‘
  24. ### 编译第三方包 ###
  25. dependencies {
  26.     # 编译所有libs下的第三方包 #
  27.     compile fileTree(dir: ‘libs‘, include: ‘*.jar‘)
  28.     # 编译一个Lib包 #
  29.     compile files("libs\android-support-v4.jar");
  30. }
  31. ### 配置项 ###
  32. android {
  33.     
  34.     compileSdkVersion 19
  35.     buildToolsVersion "19"
  36.     
  37.     defaultConfig {
  38.         minSdkVersion 14
  39.         targetSdkVersion 19
  40.     }
  41.     #### 配置release版本的签名项目以及代码混淆 ####
  42.     signingConfigs {
  43.         myConfig{
  44.             storeFile file("gradle.keystore")
  45.             storePassword "gradle"
  46.             keyAlias "gradle"
  47.             keyPassword "gradle"
  48.             ###  配置使用代码混淆  ###
  49.             runProguard true
  50.             proguardFile ‘proguard-android.txt‘
  51.         }
  52.     }
  53.     #### 编译版本 (release和debug版本) ####
  54.     buildTypes{
  55.         release {
  56.             signingConfig  signingConfigs.myConfig
  57.         }
  58.         
  59.         debug {
  60.             packageNameSuffix ".debug"
  61.         }
  62.     }
  63.     #### 配置多渠道  (playstore 与 hiapk) 改变不同渠道的包名 ####
  64.     productFlavors {
  65.         playstore {
  66.             packageName=‘com.youxiachai.androidgradle.playstore‘
  67.         }
  68.         hiapk {
  69.             packageName=‘com.youxiachai.androidgradle.amazonappstore‘
  70.         }
  71.     }
  72.     
  73.     sourceSets {
  74.         main {
  75.             manifest.srcFile ‘AndroidManifest.xml‘
  76.             java.srcDirs = [‘src‘]
  77.             resources.srcDirs = [‘src‘]
  78.             aidl.srcDirs = [‘src‘]
  79.             renderscript.srcDirs = [‘src‘]
  80.             res.srcDirs = [‘res‘]
  81.             assets.srcDirs = [‘assets‘]
  82.             jniLibs.srcDirs = [‘libs‘]
  83.         }
  84.         ### 不同渠道可以应用不同的文件 ###
  85.         hiapk {
  86.                manifest.srcFile ‘hiapk/AndroidManifest.xml‘
  87.            }
  88.            
  89.            playstore {
  90.                manifest.srcFile ‘hiapk/AndroidManifest.xml‘
  91.            }
  92.     
  93.         ### 配置android测试 ###
  94.         instrumentTest.setRoot(‘tests‘)
  95.     }
  96.     ### 使用lint检测 ###
  97.     lintOptions {
  98.         abortOnError false
  99.     }
  100. }
<style type="text/css">.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}.number_show{ padding-left:52px !important; list-style:decimal outside !important}.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}.number_hide{ padding-left:0px !important; list-style-type:none !important}.number_hide li{ list-style-type:none !important; border-left:0px}ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}ol.mainarea li pre.alt{ background-color:#f7f7ff !important}</style>

 

<style type="text/css">.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}.number_show{ padding-left:52px !important; list-style:decimal outside !important}.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}.number_hide{ padding-left:0px !important; list-style-type:none !important}.number_hide li{ list-style-type:none !important; border-left:0px}ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}ol.mainarea li pre.alt{ background-color:#f7f7ff !important}</style>
  • 参考资料来源声明        

    1. https://gist.github.com/youxiachai/5608223

2. http://www.cnblogs.com/youxilua/archive/2013/05/20/3087935.html 

3. https://github.com/Goddchen/Android-Gradle-Examples

 

Maven 使用:

  • 参考资料

地址: http://jingyan.baidu.com/article/60ccbceb01de4d64cbb19756.html

  •  

Nexus 搭建私服:

  • 参考资料:

地址: http://www.blogjava.net/ldwblog/archive/2013/11/19/406529.html

  • 版本选择:

JDK6 选择nexus 2.6一下版本, 否则启动不了。 nexus2.6以上版本需要JDK7