因为项目用到了模块化,所以不同module之间的跳转是我们需要考虑的地方,这里用到一个阿里开源的框架ARouter.
简单介绍一下。
java
配置:
在我们的app gradle中
defaultConfig {
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
/**
* 配置 arouter
*/
javaCompileOptions {
annotationProcessorOptions {
arguments = [AROUTER_MODULE_NAME: project.getName()]
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
/**
* 配置 arouter 添加的依赖包 版本一定要是根据你项目的gradle版本匹配最新。
*/
implementation 'com.alibaba:arouter-api:1.4.1'
annotationProcessor 'com.alibaba:arouter-compiler:1.2.2'
}
ok配置就这么简单。
使用:
首先初始化在application中,别忘了在清淡文件中添加application name。
/**
* author: pw on 2018/12/28 15:03
*
*/
public class MyApplication extends Application {
private boolean isDebug = true;
@Override
public void onCreate() {
super.onCreate();
if (isDebug == true){
ARouter.openLog();
ARouter.openDebug();
}
ARouter.init(this);
}
}
// 我是 A module 中的界面
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/**
* 跳转到 B module的页面中
*/
ARouter.getInstance().build("/one/appone").navigation();
}
});
}
}
//我是B module 中的界面
@Route(path = "/one/appone")
public class appone extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_appone);
}
}
是不是感觉很简单啊,通过指定路径(至少俩级)就可以完成跳转了。(前提是必须是module才可以,如果是俩个独立的application那是不可能完成跳转的,至少arouter是完成不了)那么就需要有一个是宿主才可以哦。这里就是我们使用模块化的时候用是最好的呢。
模块化:
首先在我们的项目中的 gradle.properties 中写一个变量标记当前module是独立的application还是依赖的library。
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
isModule = true
添加一个isModule = true 默认模块是独立的application。
我这里是app模块和one模块。最终需要整合到一个模块中运行整个项目。那么就需要进行一个配置。
首先在我的app模块的app gradle中
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.alibaba:arouter-api:1.4.1'
annotationProcessor 'com.alibaba:arouter-compiler:1.2.2'
//main模块只跟app一起进行运行
if(!isModule.toBoolean()) {
implementation project(':one')
}
}
如果整合就要依赖one这个module,让app这个module变成宿主appliaction。
然后需要新建一个module文件夹里面新建一个AndroidManifest.xml,有什么用呢,因为整合的时候是不能有多个application的配置的。宿主里有一个就ok了。
所以在我们的one module的app gradle中添加
sourceSets{
main{
if (isModule.toBoolean()){
manifest.srcFile 'src/main/module/AndroidManifest.xml'
}else {
manifest.srcFile 'src/main/AndroidManifest.xml'
//在集成的时候 排除掉debug文件夹内的所有内容
java{
exclude'**/debug/**'
}
}
}
}
这里有一个debug文件夹,说一下。因为我们的不同的模块都需要有自己的一些配置。所以他们有他们自己的application。当然我们整合的时候,需要将模块中的application文件删掉。我的是在java文件夹下建了一个 debug 文件夹,用来存放 myapplication 整合的时候删除。
ok这样我们就可以每次通过 gradle.properties 文件中的 isModule 为 true 或 false 来切换我们的项目是moudle形式还是application形式喽,有问题请留言,如果想转载一定要标明出处哦,欢迎讨论。