一美尸、統(tǒng)一配置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后如果出現(xiàn)報錯,需要配置flavor dimension的維度是該版本號蕾各,這樣維度就是都是統(tǒng)一的了
// 風味扒磁,也就是維度,一定要配置式曲,可以配置多個
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 "";
}