Android组件化——1,build.gradle
Posted 夜辉疾风
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android组件化——1,build.gradle相关的知识,希望对你有一定的参考价值。
文章目录
conf.gradle
在项目根目录下新建conf.gradle文件,在其中写项目的通用配置
ext
packageType = 0 //马甲包配置;0,马甲包1;1,马甲包2;2,马甲包3
isMainType = 0;//默认主程序入口;1,马甲包1入口;模块模式true;发布模式false
//通用配置
android = [
compileSdkVersion : 29,//编译使用的sdk版本
buildToolsVersion : "29.0.1",//使用的buildTools版本
minSdkVersion : 16,//最小支持sdk版本
targetSdkVersion : 29,//使用sdk版本
versionCode : 100,//apk版本
versionName : "1.0.0",//apk展示版本
sourceCompatibility : JavaVersion.VERSION_1_8,//java源代码版本
targetCompatibility : JavaVersion.VERSION_1_8,//java使用版本
encoding : "UTF-8",//编码格式
flavorDimensions : "xxxx",//模块统一维度名称
multiDexEnabled : true,//敏捷开发
]
//马甲包配置……
majiabao1 = [
app_name : "appName",//App名称
app_name_huawei : "appNameHuawei",//App华为渠道名称
app_name_xiaomi : "appNameXiaomi",//App小米渠道名称
versionNamePrefix : "appName",//版本展示名称前缀,此配置将在打包时自动生成文件名。例如:appName_V1.0.0_[huawei].apk
applicationId : "com.xxxx.xxxx",//applicationId,模块化开发时每个马甲包或者emodule都需要该值
icon : "@mipmap/ic_launcher_appname",//app的icon相对路径
roundIcon : "@mipmap/ic_launcher_round_appname",//app的圆形icon的相对路径
xxx_key : "1A2D7E1D5E2CD8D5D7",//第三方key
]
majiabao2 = [
//同majiabao1的key一致,但value根据马甲包信息配置
]
//确定马甲包使用资源
dependencies = [
//共用资源包
junit : "junit:junit:4.12",
appcompat : "androidx.appcompat:appcompat:1.1.0",
runner : "androidx.test:runner:1.2.0",
espresso_core : "androidx.test.espresso:espresso-core:3.2.0",
design : "com.android.support:design:28.0.0",
kotlin_stdlib : "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version",
core_ktx : "androidx.core:core-ktx:1.3.1",
app_name : packageType == 0 ? "$majiabao1.app_name" : "$majiabao2.app_name",
app_name_xiaomi : packageType == 0 ? "$majiabao1.app_name_xiaomi" : "$majiabao2.app_name_xiaomi",
app_name_huawei : packageType == 0 ? "$majiabao1.app_name_huawei" : "$majiabao2.app_name_huawei",
applicationId : packageType == 0 ? "$majiabao1.applicationId" : "$majiabao2.applicationId",
icon : packageType == 0 ? "$majiabao1.icon" : "$majiabao2.icon",
roundIcon : packageType == 0 ? "$majiabao1.roundIcon" : "$majiabao2.roundIcon",
versionName : (packageType == 0 ? "$majiabao1.versionNamePrefix" : "$majiabao2.versionNamePrefix")+"_V$android.versionName",
xxx_key : packageType == 0 ? "$majiabao1.xxx_key" : "$majiabao2.xxx_key",
]
project build.gradle
在项目的build.gradle中引用配置文件
apply from: "conf.gradle"
buildscript
ext.kotlin_version = "1.3.72"
……
dependencies
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
module build.gradle
在模块的build.gradle中使用配置文件
def conf = rootProject.ext //指向conf.gradle的根标签ext
//判断当前模式。如果当前模式为模块化,则引入library;如果当前是测试该module,则将该module作为程序入口,引入application
if (conf.isMainType == 1)
apply plugin: 'com.android.application'
else
apply plugin: 'com.android.library'
android
//编译sdk版本
compileSdkVersion conf.android.compileSdkVersion
//buildTools版本
buildToolsVersion conf.android.buildToolsVersion
//任务栏编辑编码格式
tasks.withType(JavaCompile)
options.encoding = conf.android.encoding
//兼容java8
compileOptions
sourceCompatibility conf.android.sourceCompatibility
targetCompatibility conf.android.targetCompatibility
//默认配置
defaultConfig
//统一资源维度
flavorDimensions conf.android.flavorDimensions
//敏捷开发开关,突破java限制方法数
multiDexEnabled conf.android.multiDexEnabled
//最小支持版本
minSdkVersion conf.android.minSdkVersion
//最大支持版本
targetSdkVersion conf.android.targetSdkVersion
//appid
if (conf.isMainType == 1)
applicationId conf.dependencies.applicationId
//版本号
versionCode conf.android.versionCode
versionName conf.dependencies.versionName
//ndk编译配置
ndk
moduleName 'xxxx'//需要编译so库的module的名字
//设置支持的SO库架构
abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'//, 'x86', 'x86_64'
//公共资源
manifestPlaceholders = [
//icon
icon : conf.dependencies.icon,
roundIcon: conf.dependencies.roundIcon,
]
//app名
resValue "string", "app_name", conf.dependencies.app_name
//conf.gradle中的key
buildConfigField "String", "xxx_key", conf.dependencies.xxx_key
//执行lint检查,有任何的错误或者警告提示,都会终止构建,我们可以将其关掉。
lintOptions
abortOnError false
//签名
signingConfigs
config
if(conf.packageType == 0)
storeFile file("../xxxx1.jks")
storePassword "xxxxx"
keyAlias "xxxxx"
keyPassword "xxxxx"
else
storeFile file("../xxxx2.jks")
storePassword "xxxxx"
keyAlias "xxxxx"
keyPassword "xxxxx"
//构建配置
buildTypes
//调试
debug
//签名配置
signingConfig signingConfigs.config
//发布
release
//签名配置
signingConfig signingConfigs.config
//拼接生成的包名
android.applicationVariants.all variant ->
variant.outputs.all
outputFileName = "$defaultConfig.versionName_$productFlavors.name.apk"
//渠道包
productFlavors
official
huawei resValue "string", "app_name", conf.dependencies.app_name_huawei
xiaomi resValue "string", "app_name", conf.dependencies.app_name_xiaomi
tencent
oppo
vivo
rests
//渠道包通用配置
productFlavors.all
flavor ->
flavor.manifestPlaceholders = [
channel_name: name,
]
//指引马甲包引入资源路径
sourceSets
main
def packageType = conf.packageType
def path = "src/main/"//默认资源路径为main
//与main文件夹同级建立majiabao1,下面包含需要区别引入的文件
if (packageType == 0)
path = "src/majiabao1/"
else if (packageType == 1)
path = "src/majiabao2/"
else if (packageType == 2)
path = "src/majiabao3/"
manifest.srcFile path + 'AndroidManifest.xml'
java.srcDirs = [path]
res.srcDirs = [path]
assets.srcDirs = [path]
//......
//第三方库
dependencies
testImplementation conf.dependencies.junit
implementation conf.dependencies.appcompat
implementation conf.dependencies.design
implementation conf.dependencies.runner
implementation conf.dependencies.espresso_core
implementation conf.dependencies.core_ktx
implementation conf.dependencies.kotlin_stdlib
//......
//编译编码
tasks.withType(JavaCompile)
options.encoding = conf.android.encoding
AndroidManifest.xml
在AndroidManifest.xml中配置build.gradle中的参数
<application
android:icon="$icon"
android:label="@string/app_name"
android:roundIcon="$roundIcon"
tools:replace="name,icon,roundIcon">
<!--渠道名称-->
<meta-data
android:name="channel_name"
android:value="$channel_name" />
<!--配置的key-->
<meta-data
android:name="xxx_key"
android:value="$xxx_key" />
</application>
Java.class
在java类中使用build.gradle中的配置参数
//调用在AndroidManifest.xml中的meta-data标签
PackageManager packageManager = application.getPackageManager();
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(application.getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = applicationInfo.metaData;
String xxx_key = bundle.getString("xxx_key");
//java预编译
public final class BuildConfig
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String LIBRARY_PACKAGE_NAME = "yoobingo.yiji.main";
public static final String BUILD_TYPE = "debug";
public static final int VERSION_CODE = 120;
public static final String VERSION_NAME = "qyqx_V1.2.0";
//调用
String versionName = BuildConfig.VERSION_NAME;
以上是关于Android组件化——1,build.gradle的主要内容,如果未能解决你的问题,请参考以下文章