Android Studio中Gradle使用详解
一)基本配置
build配置
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}Android脚本
apply plugin: 'com.android.application'Android配置
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
}项目结构
MyApp
├── build.gradle
├── settings.gradle
└── app
├── build.gradle
├── build
├── libs
└── src
└── main
├── java
│ └── com.package.myapp
└── res
├── drawable
├── layout
└── etc.Gradle Wrapper结构(这些新建项目时都添加给了用户,不需要重新添加)
myapp/
├── gradlew
├── gradlew.bat
└── gradle/wrapper/
├── gradle-wrapper.jar
└── gradle-wrapper.properties运行build任务 - 列出所有可用任务
$ ./gradlew tasks生成App-debug.apk任务
$ ./gradlew assembleDebug # Apk路径: MyApp/app/build/ outputs/apk手动导入Eclipse-Android项目(自动导入请连续点“下一步”)
在项目路径下创建build.gradle文件: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'])
}PS 也可以复制粘贴Eclipse-Android项目的源代码到Android Studio的项目里
二)自定义配置
Gradle所有文件结构
MyApp
├── build.gradle
├── settings.gradle
└── app
└── build.gradlesettings.gradle
include ':app'MyApp/build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
allprojects {
repositories {
jcenter()
}
}MyApp/app/build.gradle
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'
}基础任务
$ ./gradlew assemble -为所有构建类型创建apk
$ ./gradlew check 运行所有的检查,比如说Android Lint,如果发现问题可终止任务
$ ./gradlew build 运行以上两个任务
$ ./gradlew clean -清除生成的apk
++++
$ ./gradlew connectedCheck - 在设备上运行测试
$ ./gradlew deviceCheck - 远程设备运行测试
$ ./gradlew installDebug/installRelease - 在设备商安装指定版本
$ ./gradlew uninstall - 卸载
Build Types不同版本的参数设置 - BuildConfig/Resource Value
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)
allprojects {
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
}
}设置全局参数
ext {
compileSdkVersion = 22
buildToolsVersion = "22.0.1"
}在MyApp/app/build.gradle里面使用参数
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
}默认任务(MyApp/build.gradle)
defaultTasks 'clean', 'assembleDebug'
三) 依赖管理
仓库
预设配置仓库repositories {
mavenCentral()
jcenter()
mavenLocal()
}远程仓库
repositories {
maven {
url "http://repo.acmecorp.com/maven2"
credentials {
username 'user'
password 'secretpassword'
}
}
ivy {
url "http://repo.acmecorp.com/repo"
}
}本地仓库
repositories {
maven {
url "../repo"
}
}本地依赖
项目文件依赖dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}原生库结构与配置
# 结构:
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项目
# 修改Android插件:
apply plugin: 'com.android.library'
# settings.gradle新增libray项目:
include ':app', ':library'
# app内引用library项目:
dependencies {
compile project(':library')
}依赖概念
<待续>Android Studio内添加依赖
四)构建变体
<待续>
五)多模块构建管理
- 加速构建
在gradle.properties里面添加:
org.gradle.parallel=true
六) 测试
单元测试
使用JUnit# 结构:
app
└─── src
├─── main
│ ├─── java
│ │ └─── com.example.app
│ └───res
└─── test
└─── java
└─── com.example.app
# 依赖:
dependencies {
testCompile 'junit:junit:4.12'
}使用Robolectric
# 依赖:
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<待续>测试覆盖度
使用Jacoco<待续>
七)创建任务与插件
<待续>
八)配置CI
<待续>
九)自定义配置 - 进阶
- 缩减apk文件大小
使用ProGuardandroid {
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}收缩资源文件 - 自动 (<手动待续>)
android {
buildTypes {
release {
minifyEnabled true
shrinkResources true
}
}
} 加速构建
org.gradle.parallel=true # 并行构建
org.gradle.daemon=true # 开启Gradle守护进程
org.gradle.jvmargs=-Xms256m -Xmx1024m # 配置JVM<参照下图>
使用Profiling
<待续>使用Jack(Java Android Compiler Kit) and Jill(Jack Intermediate Library Linker)
<待续>忽略Lint
android {
lintOptions {
abortOnError false
}
}使用Ant
<待续>app打包 - 进阶
分割apkandroid {
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
原文链接:http://www.jianshu.com/p/02cb9a0eb2a0
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
Android Studio中Gradle使用详解的更多相关文章
- android studio学习----gradle命令详解
首先来给大家介绍一种简便并且个人最喜欢的一种办法.很多时候我们在GitHub上看到一个不错的开源项目,一般有两种需求,阅读源码和查看运行效果,如果是单纯的查看源码我更喜欢用一些轻量级编辑器,如vim, ...
- 【Android 应用开发】Ubuntu 下 Android Studio 开发工具使用详解 (旧版本 | 仅作参考)
. 基本上可以导入项目开始使用了 ... . 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/21035637 ...
- 【Android 应用开发】Ubuntu 下 Android Studio 开发工具使用详解
. 基本上可以导入项目开始使用了 ... . 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/21035637 ...
- 快速掌握 Android Studio 中 Gradle 的使用方法
快速掌握 Android Studio 中 Gradle 的使用方法 Gradle是可以用于Android开发的新一代的 Build System, 也是 Android Studio默认的build ...
- Android Studio 中 Gradle 依赖的统一管理(rootProjectt)
最近遇到Android Studio 中 Gradle 依赖的统一管理的不懂得地方,看大神的也没看懂,百度了一下,使用起来还挺方便 下面是链接,在这里我就不详细说明了, http://www.jian ...
- 【Android应用开发】Android Studio - MAC 版 - 快捷键详解
博客地址 : http://blog.csdn.net/shulianghan/article/details/47321177 作者 : 韩曙亮 要点总结 : -- 熟练使用快捷键 : 在任何编程环 ...
- Android Studio使用教程图文详解
谷歌表示Android Studio 1.0 能让开发者“更快更有生产力”,并认为它可以代替 Eclipse,同时为Eclipse 用户提供迁移步骤.代码自动提示.运行响应速度.都比Eclipse来的 ...
- 【Android Studio安装部署系列】二十四、Android studio中Gradle插件版本和Gradle版本关系
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 在从Android Studio3.0.0版本升级到Android Studio3.0.1版本的时候,出现了一个问题,需要升级Gra ...
- Android Studio中Gradle sync failed
问题:Android Studio中更新同步Gradle 失败 Gradle sync failed: Could not find com.android.tools.build:gradle:3. ...
随机推荐
- MyBatis 学习入门
mybatis 第一天 mybatis的基础知识 持久层的框架,对jdbc的封装 课程安排 第一天:基础知识(重点,内容量多) 最简单的jdbc程序 public class JdbcTest{ pu ...
- Cannot Create Supplier Site (Address) (文档 ID 1069032.1)
Error Address and Site Creation - Unable to create address and sites because of the following error ...
- bzoj1324
经典例题 在<最小割模型在信息学竞赛中的应用>有详细的解答就不赘述了 主要想说,其实这题的几个结论其实是很好猜出来的: 当摸不清题目本质的时候,不妨多找几种情况,猜测一下 顺便推广一下几个 ...
- bzoj2763
首先是稀疏图,不难想到dij+heap 观察题目可以知道,0<=k<=10; 所以比较裸的想法就是,d[i,j]表示已经免费了i条线路后到达j的最短路 容易得到 d[i,j]:=min(d ...
- WordPress 3.5.1远程代码执行漏洞
漏洞版本: WordPress 3.5.1 漏洞描述: WordPress是一种使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL 数据库的服务器上架设自己的网志.也可以把 WordPre ...
- POJ2104 区间第k小
题意就是区间第k大…… 题解: 前段时间用主席树搞掉了…… 如今看到划分树,是在想来写一遍,结果18号对着学长的代码调了一上午连样例都没过,好桑心…… 今天在做NOI2010超级钢琴,忽然发现用划分树 ...
- 【转】 如何查看linux版本 如何查看LINUX是多少位
原文网址:http://blog.csdn.net/hongweigg/article/details/7192471 一.如何得知自己正在使用的linux是什么版本呢,下面的几种方法将给你带来答案! ...
- SQL Server查询性能优化——覆盖索引(二)
在SQL Server 查询性能优化——覆盖索引(一)中讲了覆盖索引的一些理论. 本文将具体讲一下使用不同索引对查询性能的影响. 下面通过实例,来查看不同的索引结构,如聚集索引.非聚集索引.组合索引等 ...
- 【转】Compile FFmpeg on CentOS 6.x
This guide is based on a minimal CentOS installation and will install FFmpeg with several external e ...
- as3+java+mysql(mybatis) 数据自动工具(一)
在页游中,大部分的开发模式都是:客户端(as3)+ 服务端(java)+ 数据库(mysql). 在这3个部分会有一个相同的部分就是数据结构.比如一个用户数据,在客户端使用类 UserVO(as3) ...