动态配置模块库为Application和Library

参考:https://blog.youkuaiyun.com/zsjlovesm521/article/details/91884574

Application作为应用程序启动:apply plugin: ‘com.android.application’
Library作为库工程被引用:apply plugin: ‘com.android.library’

怎么动态切换库为application和library呢?步骤如下:

一、gradle.properties中添加:

二、Module的build.gradle中添加:(我的是UserCenter)

if (isUserModule.toBoolean()) {
    apply plugin: 'com.android.library'
} else {
    apply plugin: 'com.android.application'
}

加了后:

三、使用两套Manifest用于application和library的切换

      把工程切换到project目录下,在UserCenter的main文件夹下新建两个文件夹debug和relese,在debug和relese中添加清单文件,

   1、 debug中的清单文件添加启动代码:(application时使用)

<application
    android:allowBackup="true"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".ui.RegisterActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

2、relese文件夹下的清单文件不添加启动代码:(library时使用)

<application
    android:theme="@style/AppTheme"
    android:allowBackup="true"
    android:label="@string/app_name"
    android:supportsRtl="true"
    >
    <activity android:name=".ui.RegisterActivity">
    </activity>
</application>

3、在UserCenter的build.gradle中的android目录中添加两套Manifest切换的代码:

 sourceSets {
        main {
            if (isUserModule.toBoolean()) {
                manifest.srcFile 'src/main/release/AndroidManifest.xml'
//              release模式下排除debug文件中所有的java文件
                java {
                    exclude 'debug/**'
                }
            } else {
                manifest.srcFile 'src/main/debug/AndroidManifest.xml'
            }

        }
    }

四、在我们的主模块(如:App的build.gradle)中dependencies目录下添加:

if (isUserModule.toBoolean()){
    api project(path: ':UserCenter')
}

配置完成后我们通过通过修改isUserModule的值并sync后Android stuido就可以帮助我们动态编译当前模块的状态(Application或者Library)

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_26554909

有您的支持,我们将做的更好

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值