版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/good123day/article/details/78850211
Android开发之gradle理解
1. gradle插件和gradle构建库
二者是不同的,但有对应关系,请参考:gradle插件与gradle构建库对应关系
2. project最外层build.gradle理解,见下代码:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()//代码托管库:设置之后可以在项目中轻松引用jcenter上的开源项目
}
dependencies {
//声明gradle插件版本,
// 插件目录%install dir%\Android Studio\gradle\m2repository\com\android\tools\build\gradle
//注意:gradle插件和gradle不是一回事,且二者版本需要匹配,详见:https://developer.android.google.cn/studio/releases/gradle-plugin.html#updating-plugin
classpath 'com.android.tools.build:gradle:2.3.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
3.app层build.gradle理解,见下代码:
apply plugin: 'com.android.application' //默认的应用程序模块(插件)
android {
compileSdkVersion 26 //编译版本,目录:android-sdk\platforms
buildToolsVersion '26.0.2' //构建工具版本,目录:E:\Android\android-sdk\build-tools
defaultConfig {
applicationId "com.test.pack" //包名
minSdkVersion 19 //最低兼容版本
targetSdkVersion 26 //充分测试过版本(建议版本)
versionCode 1 //版本号
versionName "1.0" //版本名称
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
buildConfigField("boolean", "enableLog", "false")
}
buildTypes {
release {
minifyEnabled false //是否混淆(true为混淆)
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField("boolean", "enableLog", "false")
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs') //本地依赖声明
compile 'com.leon:lfilepickerlibrary:1.0'
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { //测试用例库
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12' //声明测试用例库
compile files('libs/test.jar') //引入库编译
}