作者:手机用户2602917083 | 来源:互联网 | 2024-11-23 15:07
Gradle是AndroidStudio中默认的构建工具,了解其基本配置对于开发效率的提升至关重要。本文将详细介绍如何在Gradle中定义和使用共享变量,以确保项目的一致性和可维护性。
Gradle 是 Android Studio 中默认的构建工具,掌握其基本配置对于提高开发效率和项目管理至关重要。
共享变量的定义与使用
在 Gradle 开发过程中,经常会遇到多个模块需要使用相同的配置,如 compileSdkVersion
和 buildToolsVersion
等。这些公共配置被称为共享变量,通常需要统一管理以确保一致性。
为了方便管理和引用这些共享变量,可以在项目的根目录下创建一个名为 common_config.gradle
的配置文件:
ext { android = [ versionName: "1.0.0", versionCode: 1, compileSdkVersion: 30, buildToolsVersion: "30.0.3", minSdkVersion: 16, targetSdkVersion: 30 ] dependencies = [ appcompat: 'androidx.appcompat:appcompat:1.2.0', material: 'com.google.android.material:material:1.2.0', constraintlayout: 'androidx.constraintlayout:constraintlayout:2.1.3', stdlib: "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version", ktx: 'androidx.core:core-ktx:1.6.0' ] testDependencies = [ androidTestJunit: 'androidx.test.ext:junit:1.1.3', testJunit: 'junit:junit:4.+', testEspresso: 'androidx.test.espresso:espresso-core:3.4.0', testng: 'org.testng:testng:6.9.6' ] minifyEnable = true shrinkResEnable = minifyEnable javaVersion = 8 javaMaxHeapSize = '4G' sourceCompatibility = this.getJavaVersion() targetCompatibility = this.getJavaVersion() jvmTarget = '1.8' } def getJavaVersion() { switch (project.ext.javaVersion) { case 6: return JavaVersion.VERSION_1_6 case 7: return JavaVersion.VERSION_1_7 case 8: return JavaVersion.VERSION_1_8 case 9: return JavaVersion.VERSION_1_9 default: return JavaVersion.VERSION_1_8 } }
为了使项目中的所有模块都能引用这些共享变量,可以在每个模块的 build.gradle
文件中应用这个配置文件:
apply from: "common_config.gradle"
以下是应用这些共享变量的具体示例:
plugins { id 'com.android.application' id 'kotlin-android' } android { compileSdkVersion rootProject.android.compileSdkVersion buildToolsVersion rootProject.android.buildToolsVersion defaultConfig { applicationId "com.***.sdkdemoopt" minSdkVersion rootProject.android.minSdkVersion targetSdkVersion rootProject.android.targetSdkVersion versionCode rootProject.android.versionCode versionName rootProject.android.versionName testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { signingConfig signingConfigs.release minifyEnabled rootProject.minifyEnable proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility rootProject.sourceCompatibility targetCompatibility rootProject.targetCompatibility } productFlavors { _360 {} tencent {} baidu {} oppo {} vivo {} huawei {} xiaomi {} googleplay {} } productFlavors.all { flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name] } signingConfigs { release { storeFile file("***.keystore") storePassword "***" keyAlias "***" keyPassword "***" } debug { storeFile file("***.keystore") storePassword "***" keyAlias "***" keyPassword "***" } } variant.outputs.all { output -> def buildName = "com.***" def type = variant.buildType.name if (type == "debug") { def apkName = 'app-debug' outputFileName = new File("../.././../../../build/outputs/apk/debug", apkName + '_' + type + '.apk') } else { def releaseApkName = buildName + '_' + variant.productFlavors.get(0).name + '_' + type + '_' + versionName + '_' + releaseTime() + '.apk' outputFileName = releaseApkName } } repositories { flatDir { dirs 'libs' } } android.libraryVariants.all { variant -> variant.outputs.all { def fileName = "baseModel" + '_' + releaseTime() + ".aar" outputFileName = fileName } } kotlinOptions { jvmTarget = rootProject.jvmTarget } } static def releaseTime() { return new Date().format("yyyy-MM-dd--HH-mm-ss", TimeZone.getTimeZone("GMT+8")) } dependencies { implementation fileTree(includes: ['*.jar'], dir: 'libs') implementation rootProject.ext.dependencies.constraintlayout implementation rootProject.ext.dependencies.appcompat implementation rootProject.ext.dependencies.material implementation project(":libBase") implementation(name: 'libEngine', ext: 'aar') implementation rootProject.ext.dependencies.stdlib implementation rootProject.ext.dependencies.ktx testImplementation rootProject.testDependencies.testJunit androidTestImplementation rootProject.testDependencies.androidTestJunit androidTestImplementation rootProject.testDependencies.testEspresso }
上述配置涵盖了常见的 Gradle 设置,包括但不限于:
- 签名密钥配置
- 混淆开关设置
- 自定义 APK 安装包名称
- 自定义生成 AAR 包名称
- 产品维度配置及友盟支持
- 依赖 AAR 包
- 依赖子模块
这些配置经过实际测试,确保在项目中稳定运行。