应用程序application和库工程library之间的切换
知识点:
Application作为应用程序启动:apply plugin: 'com.android.application'
Library作为库工程被引用: apply plugin: 'com.android.library'
两者之间的切换:
if(xxxx.toBoolean()) {
//库工程时
apply plugin : 'com.android.library'
}else{
//应用程序时
apply plugin : 'com.android.application'
}
需要两套AndroidManifest文件
一套用于Application时使用,配置主题及默认启动,位于debug目录。
一套用于Library时使用,注册组件及权限,位于release目录
两者之间的切换:
sourceSets {
main{
if(xxxx.toBoolean()){
//库工程时
manifest.srcFile 'src/main/release/AndroidManifest.xml'
//release模式下排除debug文件夹中的所有Java文件
java{
exclude 'debug/**'
}
}else {
//应用程序时
manifest.srcFile 'src/main/debug/AndroidManifest.xml'
}
}
}
实战:
目的:实现UserCenter模块的application和library间的转换。
步骤:
(为Android模式下)
①在gradle.properties(Project Properties)中添加转换条件。
isUserModule=false时作为应用程序启动。
isUserModule=true时作为库工程时启动。
②在UserCenter模块中添加转换条件
![]()
(切换到Project模式)
③在UserCenter模块下的main目录下添加debug和release目录
debug目录下的AndroidManifest文件作为引用程序时启动,需要在该AndroidManifest文件中添加<intent-filter>自启动。
release目录下的作为库工程时启动,则不需要自启动。
④添加对AndroidManifest文件的判断