不一样于Eclipse,Android Studio是采用Gradle来构建项目的。Gradle是一个很是先进的项目构建工具,它使用了一种基于Groovy的领域特定语言(DSL)来声明项目设置。
首先看项目最外层目录下的build.gradle文件,代码以下所示:
~~~
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
~~~
这些代码是自动生成的,
首先两处,repositories的闭包中都声明了jcenter()这个配置。jcenter是一个代码托管仓库。
接下来,dependencies闭包中使用classpath声明一个Gradle插件。由于Gradle并非专门为构建Android项目而开发的,所以若是咱们要想使用它来构建Android项目,则须要声明com.android.tools.build:gradle:3.1.3
这个插件。其中最后面的部分是版本号。android
下面咱们来看app目录下的build.gradle文件,代码以下所示:
~~~
apply plugin: 'com.android.application'缓存
android {
compileSdkVersion 28
buildToolsVersion '28.0.0'
defaultConfig {
applicationId "com.lowthink.helloworld"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}闭包
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test🏃1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}app
第一行应用了一个插件,通常有两种值可选: * com.android.application 表示这是一个应用程序模块, * com.android.library 表示这是一个库模块。 应用程序模块和库模块的最大区别在于,一个是能够直接运行的,一个只能做为代码库依附于别的应用程序模块来运行。 * * * * * **android闭包**:在这个闭包中咱们能够配置项目构建的各类属性。 * **compileSdkVersion**:用于指定项目构建的版本,这里指定成28标示使用Android 9.0系统的SDK编译。 * **buildToolsVersion**:用于指定项目构建工具的版本,目前最新的28.0.0,若是有更新的版本时,Android Studio会进行提示。修改buildToolsVersion有两种方法, 第一种直接在app中的build.gradle里添加
android {
compileSdkVersion 28
buildToolsVersion '28.0.0'
defaultConfig {}
buildTypes {}
}
~~~工具
第二种方法是选中项目,而后点鼠标右键,Open Module Settings,进入里面直接更改 buildTools的版本,如图测试
也能够点右上角的这个图片,如图gradle
buildTypes闭包:用于指定生成安装文件的相关配置,一般只会有两个自闭包,一个是debug,一个是release。ui
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
声明,为了ConstraintLayout的使用(了解更多请移步这里)第四行代码testImplementation是用于声明测试用例库的;google
本文由博客一文多发平台 OpenWrite 发布!spa