Android Studio中Gradle使用详解

原文出处:http://www.jianshu.com/p/02cb9a0eb2a0  还在更新中。

一)基本配置

  1. build配置

1
2
3
4
5
6
7
8
buildscript {
  repositories {
      jcenter() 
  }
  dependencies {
      classpath  'com.android.tools.build:gradle:1.2.3'
 
}

Android脚本

1
apply plugin:  'com.android.application'

Android配置

1
2
3
4
android {
  compileSdkVersion 22
  buildToolsVersion  "22.0.1"
}

项目结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
MyApp
├── build.gradle
├── settings.gradle
└── app
     ├── build.gradle
     ├── build
     ├── libs
     └── src
         └── main
             ├── java
             │   └── com.package.myapp
             └── res
                 ├── drawable
                 ├── layout
                 └── etc.

Gradle Wrapper结构(这些新建项目时都添加给了用户,不需要重新添加)

1
2
3
4
5
6
myapp/
  ├── gradlew 
  ├── gradlew.bat
  └── gradle/wrapper/
      ├── gradle-wrapper.jar
      └── gradle-wrapper.properties

运行build任务 - 列出所有可用任务

1
$ ./gradlew tasks

生成App-debug.apk任务

1
2
3
$ ./gradlew assembleDebug
 
# Apk路径: MyApp/app/build/ outputs/apk

手动导入Eclipse-Android项目(自动导入请连续点“下一步”)
在项目路径下创建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
buildscript {
   repositories {
       jcenter() 
   }
   dependencies {
       classpath  'com.android.tools.build:gradle:1.2.3'
   }
}
apply plugin:  'com.android.application'
android {
   compileSdkVersion 22
   buildToolsVersion  "22.0.1"
   sourceSets {
       main {
           manifest.srcFile  'AndroidManifest.xml'
           java.srcDirs = [ 'src' ]
           resources.srcDirs = [ 'src' ]
           aidl.srcDirs = [ 'src' ]
           renderscript.srcDirs = [ 'src' ]
           res.srcDirs = [ 'res' ]
           assets.srcDirs = [ 'assets' ]
       }
       androidTest.setRoot( 'tests' )
  
}
dependencies {
   compile fileTree(dir:  'libs' , include: [ '*.jar' ])
}
  1. PS 也可以复制粘贴Eclipse-Android项目的源代码到Android Studio的项目里

二)自定义配置

  1. Gradle所有文件结构

1
2
3
4
5
MyApp
├── build.gradle
├── settings.gradle
└── app
     └── build.gradle

settings.gradle

1
include  ':app'

MyApp/build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
buildscript {
  repositories {
      jcenter()
  }
  dependencies {
      classpath  'com.android.tools.build:gradle:1.2.3'
 
}
allprojects {
  repositories {
      jcenter() 
  }
}

MyApp/app/build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
apply plugin:  'com.android.application'
android {
  compileSdkVersion 22
  buildToolsVersion  "22.0.1"
  defaultConfig {
      applicationId  "com.gradleforandroid.gettingstarted"
      minSdkVersion 14
      targetSdkVersion 22
      versionCode 1
      versionName  "1.0"
  }
  buildTypes {
      release {
          minifyEnabled  false
          proguardFiles getDefaultProguardFile( 'proguard-android.txt' ),  'proguard-rules.pro'
      }
 
}
dependencies {
  compile fileTree(dir:  'libs' , include: [ '*.jar' ])
  compile  'com.android.support:appcompat-v7:22.2.0'
}

基础任务

1
2
3
4
5
6
7
8
9
$ ./gradlew assemble -为所有构建类型创建apk
$ ./gradlew check 运行所有的检查,比如说Android Lint,如果发现问题可终止任务
$ ./gradlew build 运行以上两个任务
$ ./gradlew clean -清除生成的apk
++++
$ ./gradlew connectedCheck - 在设备上运行测试
$ ./gradlew deviceCheck - 远程设备运行测试
$ ./gradlew installDebug/installRelease - 在设备商安装指定版本
$ ./gradlew uninstall - 卸载

blob.png

Build Types不同版本的参数设置 - BuildConfig/Resource Value

1
2
3
4
5
6
7
8
9
10
11
12
13
14
android {
   buildTypes {
       debug {
           buildConfigField  "String" "API_URL" , "\"http://test.example.com/api\""
           buildConfigField  "boolean" "LOG_HTTP_CALLS" "true"
           resValue  "string" "app_name" "Example DEBUG"
       }
       release {
           buildConfigField  "String" "API_URL" "\"http://example.com/api\""
           buildConfigField  "boolean" "LOG_HTTP_CALLS" "false"
           resValue  "string" "app_name" "Example"
       }
   }
}

全局设置(项目根目录的build.gradle)

1
2
3
4
5
6
7
allprojects {
   apply plugin:  'com.android.application'
   android {
       compileSdkVersion 22
       buildToolsVersion  "22.0.1"
  
}

设置全局参数

1
2
3
4
ext {
   compileSdkVersion = 22
   buildToolsVersion =  "22.0.1"
}

在MyApp/app/build.gradle里面使用参数

1
2
3
4
android {
   compileSdkVersion rootProject.ext.compileSdkVersion
   buildToolsVersion rootProject.ext.buildToolsVersion
}

默认任务(MyApp/build.gradle)

1
defaultTasks  'clean' 'assembleDebug'

三) 依赖管理

  1. 仓库
    预设配置仓库

1
2
3
4
5
repositories {
  mavenCentral()
  jcenter()
  mavenLocal()
}

远程仓库

1
2
3
4
5
6
7
8
9
10
11
12
repositories {
  maven {
      url  "http://repo.acmecorp.com/maven2"
      credentials {
          username  'user'
         password  'secretpassword'
      }
  }
  ivy {
      url  "http://repo.acmecorp.com/repo"
  }
}

本地仓库

1
2
3
4
5
repositories {
  maven {
      url  "../repo"
  }
}

本地依赖
项目文件依赖

1
2
3
dependencies {
   compile fileTree(dir:  'libs' , include: [ '*.jar' ])
}

原生库结构与配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 结构:
app
  ├── AndroidManifest.xml
  └── jniLibs
      ├── armeabi
      │   └── nativelib.so
      ├── armeabi-v7a
      │   └── nativelib.so
      ├── mips
      │   └── nativelib.so
      └── x86
          └── nativelib.so
# 配置:
android {
   sourceSets.main {
       jniLibs.srcDir  'src/main/libs'
   }
}

Libray项目

1
2
3
4
5
6
7
8
# 修改Android插件:
apply plugin:  'com.android.library'
# settings.gradle新增libray项目:
include  ':app' ':library'
# app内引用library项目:
dependencies {
   compile project( ':library' )
}

依赖概念

1
<待续>

Android Studio内添加依赖

1440250527134852.png

四)构建变体

1
<待续>

五)多模块构建管理

  1. 加速构建

1
2
在gradle.properties里面添加:
org.gradle.parallel= true

六) 测试

  1. 单元测试
    使用JUnit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 结构:
app
└─── src
├─── main
│ ├─── java
      │    │    └─── com.example.app
      │    └───res
      └─── test
           └─── java
                └─── com.example.app
# 依赖:
dependencies {
  testCompile  'junit:junit:4.12'
}

使用Robolectric

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 依赖:
apply plugin:  'org.robolectric'
  dependencies {
     compile fileTree(dir:  'libs' , include: [ '*.jar' ])
     compile  'com.android.support:appcompat-v7:22.2.0'
     testCompile  'junit:junit:4.12'
     testCompile 'org.robolectric:robolectric:3.0'
     testCompile 'org.robolectric:shadows-support:3.0'
}
# Demo:
@RunWith(RobolectricTestRunner.class)
@Config(manifest =  "app/src/main/AndroidManifest.xml" , sdk = 18)
public class MainActivityTest {
  @Test
  public void clickingButtonShouldChangeText() {
      AppCompatActivity activity = Robolectric.buildActivity(MainActivity.class).create().get();
      Button button = (Button) activity.findViewById(R.id.button);
      TextView textView = (TextView) activity.findViewById(R.id.label);
      button.performClick();
      assertThat(textView.getText().toString(), equalTo(activity.getString(R.string.hello_robolectric)));
 
}

功能测试
使用Espresso

1
<待续>

测试覆盖度
使用Jacoco

1
<待续>

七)创建任务与插件

1
<待续>

八)配置CI

1
<待续>

九)自定义配置 - 进阶

  1. 缩减apk文件大小
    使用ProGuard

1
2
3
4
5
6
7
8
android {
  buildTypes {
      release {
          minifyEnabled  true
          proguardFiles getDefaultProguardFile( 'proguard-android.txt' ),  'proguard-rules.pro'
      }
 
}

收缩资源文件 - 自动 (<手动待续>)

1
2
3
4
5
6
7
8
android {
  buildTypes {
      release {
          minifyEnabled  true
          shrinkResources  true
      }
 
}

加速构建

1
2
3
org.gradle.parallel= true  # 并行构建
org.gradle.daemon= true  # 开启Gradle守护进程
org.gradle.jvmargs=-Xms256m -Xmx1024m  # 配置JVM<参照下图>

1440257241123716.png

使用Profiling

1
<待续>

使用Jack(Java Android Compiler Kit) and Jill(Jack Intermediate Library Linker)

1
<待续>

忽略Lint

1
2
3
4
5
android {
   lintOptions {
       abortOnError  false
   }
}

使用Ant

1
<待续>

app打包 - 进阶
分割apk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
android {
   splits {
       density {
           enable  true
           exclude  'ldpi' 'mdpi'
           compatibleScreens  'normal' 'large' 'xlarge'
       }
  
}
生成结果:
app-hdpi-release.apk
app-universal-release.apk
app-xhdpi-release.apk
app-xxhdpi-release.apk
app-xxxhdpi-release.apk
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值