使用组件化时moudle可以为单独运行,或者把单独的moudle变为libary
1. 添加控制变量属性 在gradle.properties文件末尾添加
#是否处于单独运行状态 true为单独运行状态,false为组件运行状态
isDebug=true
2.moudle变为libary
把需要变城libary的项目中build.gradle中首行
apply plugin: 'com.android.application'
改为
apply plugin: 'com.android.library'
if (isDebug.toBoolean()) {
//为true时,当前项目是moudle
apply plugin: 'com.android.application'
} else {
//为false时,当前项目是library
apply plugin: 'com.android.library'
}
3.配置AndroidManifest.xml
用与moudle与library之间切换
debug模式下 Manifest.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.home">
<application
android:allowBackup="true"
android:supportsRtl="true">
<activity android:name=".MainActivity">
</activity>
</application>
</manifest>
正常的moudle模式下Manifest.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.home">
<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/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
总体build.gradle的代码如下
if (isDebug.toBoolean()) {
//为true时,当前项目是moudle
apply plugin: 'com.android.application'
} else {
//为false时,当前项目是library
apply plugin: 'com.android.library'
}
//****************************************************************************************************
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
//*******************************用与moudle与library之间切换*********************************************************************
//为true时,当前项目是moudle
if (isDebug.toBoolean()) {
applicationId "com.example.home"
}
//****************************************************************************************************
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
//*****************************用与moudle与library之间切换***********************************************************************
sourceSets {//指定资源路径
main {
//为true时,当前项目是moudle,并加载对应的manifest
if (isDebug.toBoolean()) {
manifest.srcFile 'src/main/AndroidManifest.xml'
}else{
manifest.srcFile 'src/debug/AndroidManifest.xml'
}
}
}
//****************************************************************************************************
}
//此处不改变
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
4.通过改变 isDebug=true 或 false用与moudle与library之间切换
5.library依赖到app
6.app build.gradle中dependencies{}下添加判断即可
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
if (!isDebug.toBoolean()){
//引用后这一行会自动生成,添加判断条件即可
api project(path: ':home')
}
}