五、涉及到的 Gradle 构建脚本
顶层 Gradle 代码示例 :
// Top-level build file where you can add configuration options common to all sub-projects/modules. // 将 component.gradle 配置文件中的内容导入到该位置 // 相当于引入头文件 apply from: "component.gradle" buildscript { ext.kotlin_version = "1.4.10" repositories { google() jcenter() } dependencies { classpath "com.android.tools.build:gradle:4.1.0" 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() } } task clean(type: Delete) { delete rootProject.buildDir }
引入的 Gradle 代码 :
// ext 是 extension 扩展的含义 // ext 后的 {} 花括号 , 是闭包 , ext{ // 定义 android 变量 , 类型是字典 Map 集合 // 其中定义了若干键值对集合 androidConfig = [ compileSdkVersion : 30, minSdkVersion : 18, targetSdkVersion : 30, versionCode : 1, versionName : "1.0" ] applicationId = [ "app" : "kim.hsl.component", "mylibrary" : "", "mylibrary2" : "", ] }
Module 层级的 Gradle 代码示例 : Component\app\build.gradle 构建脚本 ;
plugins { id 'com.android.application' id 'kotlin-android' } println("Print Variable : rootProject.ext.androidConfig : ${rootProject.ext.androidConfig}") // def 相当于 Java 中的 Object // 声明 config 和 appId 变量 , 并为其赋值 def androidConfig = rootProject.ext.androidConfig def appId = rootProject.ext.applicationId android { compileSdkVersion androidConfig.compileSdkVersion buildToolsVersion "30.0.3" defaultConfig { applicationId appId["app"] minSdkVersion androidConfig.minSdkVersion targetSdkVersion androidConfig.targetSdkVersion versionCode androidConfig.versionCode versionName androidConfig.versionName 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 "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation 'androidx.core:core-ktx:1.3.2' implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.google.android.material:material:1.3.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' }
六、博客资源
博客源码 :
GitHub : https://github.com/han1202012/Component
CSDN 下载 :