1. 前言
Android studio
采用的是gradle
来构建项目。
2. Project
下的build.gradle
文件
但由于gradle
这个项目构建工具却不是专门为了Android
开发而写的,故而可以支持其他的语言,如Java
、C++
等。故而在当前项目的build.gradle
文件中需要指定该项目所依赖的gradle
版本。
如:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
}
}
allprojects {
repositories {
jcenter()
}
}
而,上面所见的repositories
是指明仓库地址,因为我们会使用到jcenter
这个开源库中的一些开源项目。
当前的gradle
版本指定为3.5.2
3. app
下的build.gradle
文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 15
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
第一行指定是应用程序模块还是库模块,一般有两个值可以选择:
com.android.application
com.android.library
比较有意思的是plugin
是插件意思。
在defaultConfig
中我们对项目进行简单的配置。如:applicationId
用于指定项目的包名
在buildTypes
中,指定生成安装文件的相关配置。如:minifyEnabled
用于指定是否对项目的代码进行混淆,而proguardFiles
用于指定在混淆时候的规则文件。'proguard-android-optimize.txt
是在Andorid SDK
目录下,而proguard-rules.pro
是在当前项目根目录下,该文件的具体细节有一个官方的地址:here。
在dependencies
中,用于指定当前项目的依赖关系,一共有三种依赖方式:
- 本地依赖;
- 库依赖;
- 远程依赖;
本地依赖也就是对本地的Jar
或者目录添加依赖关系;
库依赖是指可以对项目中的库模块添加依赖;
远程依赖,则是指对jcenter
中的开源项目的依赖;
implementation fileTree
是本地依赖声明;
implementation
是远程依赖声明,如:implementation 'androidx.appcompat:appcompat:1.0.2'
implementation project(':helper')
库依赖;