gradle 编译android项目 Eclipse

gradle下载地址

gradle环境配置


1.工程目录结构


说明:如果项目有多个library工程并且有可能重复引用了相同的jar包,如support-4等,需要将这些jar单独拎出来作为一个BaseLibray,其他library引用这个BaseLibrary,如果不这样做很有可能在编译的时候遇到下面这样的错误:

  1. UNEXPECTED TOP-LEVEL EXCEPTION:
  2. java.lang.IllegalArgumentException: already added: Landroid/support/v4


2.Eclipse下自动生成相关文件

File -> Export -> Android -> Generate Gradle build files


如果没有Generate这个选项,可以尝试升级到高版本的ADT 笔者使用的ADT-23

选中主工程



然后finish即可。完成之后会自动在工程目录中生成一些build文件


说明:在目录下添加local.properties文件,内容如:

sdk.dir=D:\\dev\\adt-bundle-windows-x86_64-20140702\\sdk


 
即指定Android SDK的目录,gradle在编译工程的时候会调用该目录下的打包工具,由于我已经把SDK目录配置在了环境变量中,这里可以不需要local.properties文件了。 

打开build.gradle我们可以看到:

  1. // Top-level build file where you can add configuration options common to all sub-projects/modules.
  2. buildscript {
  3. repositories {
  4. jcenter()
  5. }
  6. dependencies {
  7. classpath 'com.android.tools.build:gradle:0.12.+'
  8. }
  9. }

修改这个文件:

  1. // Top-level build file where you can add configuration options common to all sub-projects/modules.
  2. buildscript {
  3. repositories{
  4. mavenCentral()
  5. }
  6. dependencies{
  7. //笔者使用的gradle版本为2.4
  8. classpath 'com.android.tools.build:gradle:1.1.3'
  9. }
  10. /***
  11. tasks.withType(Compile){
  12. options.encoding = "UTF-8"
  13. }
  14. **/
  15. tasks.withType(JavaCompile) { options.encoding = "UTF-8" }
  16. }

由于使用了ADT自动生成build files的功能,库工程和主工程下都会有build.gradle文件

主工程下build.gradle

  1. apply plugin: 'com.android.application'
  2. dependencies {
  3. compile fileTree(dir: 'libs', include: '*.jar')
  4. compile project(':appcompat_v7')
  5. compile project(':AndroidSupportLibrary')
  6. compile project(':AndroidSupportLibrary2')
  7. }
  8. android {
  9. compileSdkVersion 22
  10. buildToolsVersion "22.0.1"
  11. sourceSets {
  12. main {
  13. manifest.srcFile 'AndroidManifest.xml'
  14. java.srcDirs = ['src']
  15. resources.srcDirs = ['src']
  16. aidl.srcDirs = ['src']
  17. renderscript.srcDirs = ['src']
  18. res.srcDirs = ['res']
  19. assets.srcDirs = ['assets']
  20. }
  21. // Move the tests to tests/java, tests/res, etc...
  22. instrumentTest.setRoot('tests')
  23. // Move the build types to build-types/<type>
  24. // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
  25. // This moves them out of them default location under src/<type>/... which would
  26. // conflict with src/ being used by the main source set.
  27. // Adding new build types or product flavors should be accompanied
  28. // by a similar customization.
  29. debug.setRoot('build-types/debug')
  30. release.setRoot('build-types/release')
  31. }
  32. }


直接命令行切换到主工程目录:执行gradle clean

执行gradle build


至此这个基本流程就算完成了。

3.打多个不同包名,使用不同资源不同渠道定制包。

 以umeng多渠道为例,修改android:value

  1. <meta-data
  2. android:name="UMENG_CHANNEL"
  3. android:value="${CHANNEL_NAME}" > </meta-data>
build.gradle中添加

  1. productFlavors {
  2. aa {
  3. applicationId 'com.example.testgradle.aa'
  4. manifestPlaceholders = [ CHANNEL_NAME:"aa"]
  5. }
  6. bb {
  7. applicationId 'com.example.testgradle.bb'
  8. manifestPlaceholders = [ CHANNEL_NAME:"bb"]
  9. }
  10. }

applicationId为包名,manifestPlaceholders会在打包时替换AndroidManifest.xml中<meta-data/>节点下的CHANNEL_NAME,

在src目录下新增目录 src/aa/res  src/bb/res


如想重新命名应用名称,只需修改对应路径下的string.xml就行了,换应用的icon或其他资源也同理,保证资源名和主工程下的一致,同时放到对应路径,gradle在打包的时候就会替换为新的资源文件。【这儿有个问题需要注意,就是如果主工程下有自定义的attr,在使用自定义属性的xml中的schemas中使用xmlns:app="http://schemas.android.com/apk/res-auto" 否则在打其他包名的时候提示找不到attribute】


贴上一份还算完整的build.gradle

  1. apply plugin: 'com.android.application'
  2. dependencies {
  3. compile fileTree(dir: 'libs', include: '*.jar')
  4. compile project(':appcompat_v7')
  5. compile project(':AndroidSupportLibrary')
  6. compile project(':AndroidSupportLibrary2')
  7. }
  8. android {
  9. compileSdkVersion 22
  10. buildToolsVersion "22.0.1"
  11. defaultConfig {
  12. applicationId 'com.example.testgradle'
  13. minSdkVersion 9
  14. targetSdkVersion 20
  15. versionCode 1
  16. versionName '1.0'
  17. }
  18. productFlavors {
  19. aa {
  20. applicationId 'com.example.testgradle.aa'
  21. manifestPlaceholders = [ CHANNEL_NAME:"aa"]
  22. }
  23. bb {
  24. applicationId 'com.example.testgradle.bb'
  25. manifestPlaceholders = [ CHANNEL_NAME:"bb"]
  26. }
  27. signingConfigs{
  28. myConfig{
  29. storeFile file("test.keystore")//指定keystore的路径 这儿为当前工程目录下
  30. storePassword "123321"
  31. keyAlias "alias_name"
  32. keyPassword "123321"
  33. }
  34. }
  35. buildTypes{
  36. release{
  37. //runProguard true //打开混淆开关
  38. //proguardFile 'proguard.txt.txt' //配置单个文件这样
  39. signingConfig signingConfigs.myConfig
  40. }
  41. }
  42. sourceSets {
  43. main {
  44. manifest.srcFile 'AndroidManifest.xml'
  45. java.srcDirs = ['src']
  46. resources.srcDirs = ['src']
  47. aidl.srcDirs = ['src']
  48. renderscript.srcDirs = ['src']
  49. res.srcDirs = ['res']
  50. assets.srcDirs = ['assets']
  51. }
  52. }
  53. // Move the tests to tests/java, tests/res, etc...
  54. instrumentTest.setRoot('tests')
  55. // Move the build types to build-types/<type>
  56. // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
  57. // This moves them out of them default location under src/<type>/... which would
  58. // conflict with src/ being used by the main source set.
  59. // Adding new build types or product flavors should be accompanied
  60. // by a similar customization.
  61. debug.setRoot('build-types/debug')
  62. release.setRoot('build-types/release')
  63. }
  64. }

相关技术文章

http://tech.meituan.com/mt-apk-adaptation.html

http://stackoverflow.com/questions/17571427/gradle-no-resource-identifier-found-for-attribute-when-using-flavors-and-packag