赞
踩
系列博客第二篇,本篇主要实现 组件的基本配置(同时支持单独调试和集成测试)
首先我们新建一个项目,之后我们切换到Project视图,右键项目——>New——>Module为这个项目添加一个组件,假设这个组件是一个基础组件,命名为basecomponents. (这里选择Android Library和Phone &Tablet都可以,创建之后可以在build.gradle中更改,两者区别见下方
接下来我们需要对组件进行配置实现组件既能在开发过程中以APP形式单独调试,也能在集成测试阶段作为APP整体的一个lib进行调试。Android下控制module是否可以独立运行是通过plugin 类型进行的,plugin值为com.android.application
表示此Module为可单独运行的APP,当plugin值为com.android.library
表示此Module为主APP的一个组件库。
两者区别其实也比较简单, App 插件来配置一个 Android App 工程,项目构建后输出一个 APK 安装包,Library 插件来配置一个 Android Library 工程,构建后输出 aar 包。
OK,到这里我们已经知道应该怎么做了,只需要设置一个是否单独调试的标志位,开启时设定plugin类型为com.android.application
,关闭时设定plugin类型为com.android.library
即可。
在刚才新建的basecomponents
组件邮件右键新建Resource Bundle
文件,命名为gradle(命名随意,会自动加上.properties格式后缀)
gradle.properties定义一个标志位即可
# 是否独立运行,用于单独测试此组件
isRunningAlone = false
接下来我们修改basecomponents
的build.gradle
if (isRunningAlone.toBoolean()) {
apply plugin: 'com.android.application'
} else {
apply plugin: 'com.android.library'
}
apply plugin: 'kotlin-android'
通过刚才定义的isRunningAlone字段值设定插件类型,同时在defaultConfig中新增applicationId字段,因为单独调试时需要appId
这里注意,如果是新版gradle会自动添加plugins闭包,如下所示
plugins {
id 'com.android.library'
id 'kotlin-android'
}
由于plugins闭包内部只支持String,不支持进行if判断,所以直接删除整个plugins闭包,改为上面的apply plugin:
格式即可
修改后的build.gradle完整文件如下:
if (isRunningAlone.toBoolean()) { apply plugin: 'com.android.application' } else { apply plugin: 'com.android.library' } apply plugin: 'kotlin-android' android { compileSdkVersion 31 buildToolsVersion '30.0.3' defaultConfig { // 组件单独调试时作为application if (isRunningAlone.toBoolean()) { applicationId "com.xiaok.componentlearning" } minSdkVersion 16 targetSdkVersion 31 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles "consumer-rules.pro" } 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.1' implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.google.android.material:material:1.2.1' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' }
组件在单独调试时,想正常运行,显然还需要一个AndroidManifest.xml文件,作为基础组件库时,需要使用整个APP的AndroidManifest,具体配置如下:
① 下图中2处是新建组件时默认生成的,作为基础组件库时使用2。我们需要新建1处的AndroidManifest.xml用于单独调试时使用
AndroidManifest.xml中主要就是新增了一个Launcher Activity作为单独调试此组件的入口
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xiaok.basecomponents"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.ComponentLearning"> <activity android:name=".TestBaseActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
② 针对不同模式在build.gradle中配置AndroidManifest.xml
在sourceSets节点下根据isRunningAlone的值来配置不同的manifest
sourceSets {
main {
if (isRunningAlone.toBoolean()) {
manifest.srcFile 'src/main/manifest/AndroidManifest.xml'
} else {
manifest.srcFile 'src/main/AndroidManifest.xml'
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。