赞
踩
由于发展需要,以后的项目有可能会越来越多,重复的模块也会越来越多,我们不想每次都进行重复开发,所以最近开始进行模块化开发的改造。
下面开始介绍大概的流程:
一、按照原来的模式进行创建工程,这没什么好说的,然后在项目下面新建三个module。
如图所示:
说明:
二、增加 isDebug=false 到 gradle.properties中
如图:
如果你想要做到一个module在开发的时候是一个app,在发布的时候是一个Library这个属性必不可少。
三、修改testone这个module的文件。
首先在main文件夹下新建两个文件夹,debug和release,用来存放不同的AndroidManifest.xml配置文件。如图所示:
这两个配置文件不同的地方就是,debug文件夹下的配置文件,注册activity需要有一个入口,包括一些权限。
而release文件夹下的只需要用户对需要注册的组件进行注册即可。
然后,需要在testone下面的build.gradle修改头部的apply
if (isDebug.toBoolean()) {
apply plugin: 'com.android.application'
} else {
apply plugin: 'com.android.library'
}
并引入
sourceSets {
main {
if (isDebug.toBoolean()) {
manifest.srcFile 'src/main/debug/AndroidManifest.xml'
} else {
manifest.srcFile 'src/main/release/AndroidManifest.xml'
java {
exclude 'debug/**'
}
}
}
}
注意:如果贴进去代码,有可能会默认把mainfest.srcFile,第一个字母变成大写,这里要注意
最后的全部gradle如下:
if (isDebug.toBoolean()) {
apply plugin: 'com.android.application'
} else {
apply plugin: 'com.android.library'
}
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
if (isDebug.toBoolean()) {
manifest.srcFile 'src/main/debug/AndroidManifest.xml'
} else {
manifest.srcFile 'src/main/release/AndroidManifest.xml'
java {
exclude 'debug/**'
}
}
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile project(':router')
}
四、app里面的build.gradle修改
需要在引用的地方加入如下代码
if (!isDebug.toBoolean()) {
compile project(':testone')
} else {
compile project(':router')
}
这样最基本的一个模块化开发的框架就差不多搭建好了,剩下的就是填充base-res以及各模块中的代码了。
注意
1.跳转可以使用ActivityRouter里面的方法。
2.如果module太多,导致android studio很卡,可以再整个目录下的settings.gradle进行修改,注释掉里面暂时不用的模块。(只要电脑不是太好(:з」∠) 我相信会用到的)
项目地址:https://github.com/marvinzzzz/ModelTest
在下还是个小白,如果上述内容有问题,请指出 - - 共同进步。谢谢
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。