Android build.gradle详解

1.build.gradle位置

通常Android项目都至少会有两个build.gradle,一个是在app目录下得,另一个则是在整个项目的目录下。如下图所示


build.gradle目录位置

2.整个目录下的build.gradle

buildscript {
    ext.kotlin_version = "1.4.32"
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.3"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

ext.kotlin_version 表示是当前使用kotlin插件的版本号
repositories 可以声明google(),jcenter()的依赖库,可以自行添加别的依赖库的maven仓地址
dependencies 声明了gradle和kotlin两个插件

3.app目录下build.gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.plusplus.learnkotlin"
        minSdkVersion 22
        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'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include:['*.jar','*.aar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

3.1 plugins

com.android.application表示当前是一个应用程序的模块,可以独立运行;另外一种就是com.android.library,表示当前是一个库模块,只能依赖别的应用程序模块运行。
kotlin-android使用kotlin语言开发Android项目必须用到的
kotlin-android-extensions对kotlin语言的一些功能扩展,比如activity中直接引入布局文件中控件的id

3.2 android

compileSdkVersion 指项目编译的SDK版本
buildToolsVersion 项目构建工具的版本

3.2.1 defaultConfig

applicationId:应用的唯一标识附
minSdkVersion:项目运行时最低兼容的Android版本
targetSdkVersion:项目在当前版本做了充分测试,可以兼容当前版本的新的特性,比如版本号改成23就需要做Android6.0以上的权限的动态申请,改成22则不需要。
versionCode:版本号
versionName:版本名
testInstrumentationRunner:JUnit测试使用

3.2.2 buildTypes

debug:生成测试版安装文件的配置
release:生成正式版安装文件的配置,minifyEnabled项目代码是否混淆,true表示混淆,false表示不混淆。proguardFiles中有两个文件,第一个表示所有项目通用的混淆规则,第二个文件表示当前项目特有的混淆规则

3.2.3 compileOptions

sourceCompatibility:指定编译 .java文件的jdk版本
targetCompatibility:指的是运行时jdk的环境

3.2.4 kotlinOptions

jvmTarget: kotlin语音编译的时候使用jdk1.8的版本特性

3.3 dependencies

指定当前项目得依赖关系,可以分为:本地依赖,库依赖和远程依赖。举例如下:
implementation fileTree(dir: 'libs', include:['.jar','.aar']):本地依赖,libs依赖的目录'.jar,.aar 依赖包的格式
implementation 'androidx.core:core-ktx:1.2.0' :远程仓库依赖
implementation project(':common') :库依赖
testImplementation 'junit:junit:4.+':自动化测试依赖

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容