一、统一配置config.gradle
project.ext {
// 是不是app
def isApp = project.name == 'app'
def isComm = project.name == 'lib-comm'
setAppConfig = {
if (isApp) {
apply plugin: 'com.android.application'
} else {
apply plugin: 'com.android.library'
}
apply plugin: 'org.jetbrains.kotlin.android'
// ARouter
apply plugin: 'com.alibaba.arouter'
apply plugin: 'kotlin-kapt'
// 2. android配置
setAndroidConfig(project.android)
// 3. 设置依赖
setDependencies(project.dependencies)
}
// 2. 设置公共配置
setAndroidConfig = {
android ->
android.compileSdk 32
android.defaultConfig {
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
if (isApp) {
applicationId "com.boardour.comproject"
}
// 阿里ARouter
javaCompileOptions {
annotationProcessorOptions {
arguments = [AROUTER_MODULE_NAME: project.getName()]
}
}
}
android.buildTypes {
release {
minifyEnabled false
proguardFiles android.getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
proguardFiles android.getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
android.compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
android.kotlinOptions {
jvmTarget = '1.8'
}
}
// 3. 设置依赖
setDependencies = {
dependencies ->
// 设置依赖代理
delegate = dependencies
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
// ARouter
implementation 'com.alibaba:arouter-api:1.5.2'
kapt 'com.alibaba:arouter-compiler:1.5.2'
if (!isComm) {
implementation project(path: ':lib-comm')
}
}
}
project.setAppConfig()
引入配置
// 引入配置
apply from: "${rootDir}/config.gradle"
二、项目使用、调试、清单配置
apply plugin: 'com.android.application'
android {
buildTypes {
// 公共常量配置,在BuildConfig类
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
// 清单里面用:${VERSION_NAME}
manifestPlaceholders = [
VERSION_NAME: this.app.versionName,
IS_DEBUG : "false"
]
// BuildConfig类下面的常量配置
buildConfigField("boolean", "IS_DEBUG", "false")
buildConfigField("String", "LOG_TAG", "\"Lven\"")
}
debug {
// 清单里面用:${VERSION_NAME}
manifestPlaceholders = [
VERSION_NAME: this.app.versionName,
IS_DEBUG : "false"
]
// BuildConfig类下面的常量配置
buildConfigField("boolean", "IS_DEBUG", "true")
buildConfigField("String", "LOG_TAG", "\"Lven\"")
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation this.depenLibs.appcompat
implementation this.depenLibs.constraintlayout
}
<!--清单引用配置测试-->
<meta-data
android:name="BUGLY_ENABLE_DEBUG"
android:value="${IS_DEBUG}" />
<meta-data
android:name="BUGLY_APP_VERSION"
android:value="${VERSION_NAME}" />
三、多渠道配置
清单配置
<application>
<meta-data
android:name="BO_CHANNEL"
android:value="${BO_CHANNEL}" />
</application>
app/build.gradle配置
android {
...
build {
defaultConfig {
// Gradle后如果出现报错,需要配置flavor dimension的维度是该版本号,这样维度就是都是统一的了
// 风味,也就是维度,一定要配置,可以配置多个
flavorDimensions "channel"
}
// 各个渠道
productFlavors {
huawei {}
xiaomi {}
}
productFlavors.all {
flavor -> flavor.manifestPlaceholders = [BO_CHANNEL: name]
}
// 设置输出APK名称
android.applicationVariants.all { variant ->
// 打包完成后输出路径
def name = "app"+
"_" + variant.flavorName +
"_" + variant.buildType.name +
"_" + variant.versionName +
"_" + new Date().format('yyyyMMdd')+ ".apk"
// 相当于路径 app/apk/
def path = "../../../../../apk/"
// 先删掉文件夹下的apk
File apkDir = file(path)
apkDir.deleteDir()
// 打包输出
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
//指定路径输出
output.outputFileName = new File(path, name)
}
}
}
}
}
获取渠道名称
/**
* 获取渠道名称
*/
public static String getChannel(Context context) {
try {
PackageManager pm = context.getPackageManager();
ApplicationInfo appInfo = pm.getApplicationInfo(context.getPackageName(),
PackageManager.GET_META_DATA);
// key为<meta-data>标签中的name
String channel = appInfo.metaData.getString("BO_CHANNEL");
return channel;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}