模块化入门
下面开始介绍大概的流程:
一、按照原来的模式进行创建工程,这没什么好说的,然后在项目下面新建三个module。
如图所示:
说明:
- app不用过多介绍了
base-res和router(可以统一的理解为common,也可以合并成一个module)。base-res存放的是整个工程都会用到的jar包、工具类、基类等资源文件。
注意:这个module会一直是一个library,R文件中的id都是 public static(ButterKnife不能在这里引用,因为黄油刀使用到的R.id 都必须要是public static final) - testone为一个新的module,引用router,可以再其中写一个新的业务模块。
二、增加 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'
}
- 1
- 2
- 3
- 4
- 5
并引入
sourceSets {
main {
if (isDebug.toBoolean()) {
manifest.srcFile 'src/main/debug/AndroidManifest.xml'
} else {
manifest.srcFile 'src/main/release/AndroidManifest.xml'
java {
exclude 'debug/**'
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
注意:如果贴进去代码,有可能会默认把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')
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
四、app里面的build.gradle修改
需要在引用的地方加入如下代码
if (!isDebug.toBoolean()) {
compile project(':testone')
} else {
compile project(':router')
}
- 1
- 2
- 3
- 4
- 5
这样最基本的一个模块化开发的框架就差不多搭建好了,剩下的就是填充base-res以及各模块中的代码了。
注意
1.跳转可以使用ActivityRouter里面的方法。
2.如果module太多,导致android studio很卡,可以再整个目录下的settings.gradle进行修改,注释掉里面暂时不用的模块。(只要电脑不是太好(:з」∠) 我相信会用到的)