Andorid Studio 模块化开发相关配置

Andorid Studio 模块化开发相关配置

下面以宿主APP模块和Uer_Module模块为例:

第一步:在项目根目录gradle.properties配置文件添加如下代码

isNeedUserModule=true
#isNeedUserModule=false

 

第二步 在user_module中的build.gradle文件中加入了如下代码来控制此库是library还是APP:

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

 

第三步 在user_module中的build.gradle文件中加入了如下代码来指定AndroidManifest.xml文件路径:

android{
 sourceSets {
    main {
        if (!isNeedUserModule.toBoolean()) {
            manifest.srcFile 'src/main/app/AndroidManifest.xml'
        } else {
            manifest.srcFile 'src/main/module/AndroidManifest.xml'
        }
    }

}
}

 

其中 app中的AndroidManifest 内容如下:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.finddreams.module_user">

<application>
    <activity
        android:name=".LoginActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

 

 module目录下的AndroidManifest 如下:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.finddreams.module_user" >
<application>
    <activity android:name=".LoginActivity"></activity>
</application>

</manifest>

 

第四步 在user_module中的build.gradle文件中加入了如下代码来为该模块的下的资源自动添加前缀 避免模块之间资源命名重复:

resourcePrefix "module_user"

 

第五步 在宿主app中的build.gradle文件中加入了如下代码来动态引入user_module模块

  if (isNeedUserModule.toBoolean()) {
compile project(':user_module')
}

 

第六步 配置页面跳转路由

目前成熟的开源路由框架有:

美团WMRouter

阿里的ARouter

可参考:《阿里路由框架ARouter的使用步骤》

 

关于我

私人博客

技术微信公众号:infree6 或者直接扫码

相关文章

安装环境都很简单,就是下载工具需要在华为开发者联盟里注册...
上传的方式有两种,第一种是通过bintray官方出的插件bintray...
转载自: 完美解决Error:SSLpeershutdownincorrectly打开gr...
https://www.jianshu.com/p/9220227cdfb3buildscript{ext.ko...
Markdown版本笔记我的GitHub首页我的博客我的微信我的邮箱My...
 跟着教程做的,已经有了JDK,直接进行后面的步骤,下载安装...