Android 如何让Module单独运行调试

本文详细介绍了如何在项目中通过gradle.properties设置singleModule开关,以及根据不同模式配置module、AndroidManifest文件和build.gradle。重点讲解了独立模块模式下的清单文件和依赖管理。
该文章已生成可运行项目,

添加module开关

在项目根目录的 gradle.properties 添加变量 singleModule ,为 true 表示 各模块可以作为独立 app运行,为 false 表示 各个module 作为依赖库使用

singleModule = false

 module模块配置

配置模块作为独立 app 时的环境配置,如下图所示:

debug 模式下的清单文件详情:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.imooc.common">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true">
        <activity android:name=".ComponentListActivity"
            android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

独立module 模式下的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.imooc.common">
    <application>
        <activity android:name=".ComponentListActivity"></activity>
    </application>
</manifest>

 module  项目配置

根据开关对模块的 build.gradle 进行配置:

// 根据模块是否独立,将模块作为 apk 还是 module
if(singleModule.toBoolean())
{
    apply plugin: 'com.android.application'
}else{
    apply plugin: 'com.android.library'
}
apply from: '../dependencies.gradle'
android {
    //限制资源名称
    resourcePrefix  project.getName() +"_"
    defaultConfig {
        // 只有独立运行时才需要 applicationId
        if(singleModule.toBoolean()){
            applicationId "com.imooc.common"
        }
    }

    sourceSets {
        main {
            // 单独调试与集成调试时使用不同的 AndroidManifest.xml 文件
            if (singleModule.toBoolean()) {
                manifest.srcFile 'src/main/debug/AndroidManifest.xml'
                //多增加一个资源调试路径 src/main/debug/res
                res.srcDirs = ['src/main/res', 'src/main/debug/res']
            }else{
                manifest.srcFile 'src/main/AndroidManifest.xml'
                java {
                    //module 模式下要 排除src/test/文件夹下的所有文件
                    exclude 'src/debug/'
                }
            }
        }
    }
}
dependencies {
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

主工程依赖

根据开关进行模块依赖:

 if(!singleModule.toBoolean()) {
        runtimeOnly project(':common')
 }

 

本文章已经生成可运行项目
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值