阿里路由框架--ARouter最简单例子

时间:2020-12-27 20:48:05

这是最简单的案例

项目结果:

一个是项目的app,然后我新建了一个module:libraryone

阿里路由框架--ARouter最简单例子

首先处理app

阿里路由框架--ARouter最简单例子



MainActivity.java


package com.example.cuishizhou.arouter4;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.alibaba.android.arouter.launcher.ARouter;

public class MainActivity extends AppCompatActivity {
Button bt1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1= (Button) findViewById(R.id.bt1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ARouter.getInstance().build("/ui/主页2").navigation();
Toast.makeText(MainActivity.this, "hello", Toast.LENGTH_SHORT).show();

}
});
}
}

布局文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context="com.example.cuishizhou.arouter4.MainActivity">

<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我跳"/>
</LinearLayout>


MyApplication.java

package com.example.cuishizhou.arouter4;

import android.app.Application;

import com.alibaba.android.arouter.launcher.ARouter;

/**
* @author: xiewenliang
* @Filename:
* @Description:
* @Copyright: Copyright (c) 2017 Tuandai Inc. All rights reserved.
* @date: 2017/4/19 10:56
*/

public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
if (BuildConfig.DEBUG) {
ARouter.openLog(); // 打印日志
ARouter.openDebug(); // 开启调试模式(如果在InstantRun模式下运行,必须开启调试模式!线上版本需要关闭,否则有安全风险)
}
ARouter.init(this);
}
}


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cuishizhou.arouter4">

<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>

build.gradle
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "27.0.3"
defaultConfig {
applicationId "com.example.cuishizhou.arouter4"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
javaCompileOptions {
annotationProcessorOptions {
arguments = [moduleName: project.getName()]
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

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'
testCompile 'junit:junit:4.12'
compile 'com.alibaba:arouter-api:1.2.1'
annotationProcessor 'com.alibaba:arouter-compiler:1.1.2'
compile project(':libraryone')
}

接着就是libraryone了
阿里路由框架--ARouter最简单例子

Test2.java
package com.example.libraryone;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.alibaba.android.arouter.facade.annotation.Route;

/**
* Created by cuishizhou on 2018/1/9.
*/

@Route(path = "/ui/主页2")
public class Test2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);

}

}

布局文件:test2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="崔*宙"/>

</LinearLayout>

AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.libraryone">

<application android:allowBackup="true" android:label="@string/app_name"
android:supportsRtl="true">
<activity android:name=".Test2" />
</application>

</manifest>

build.gradle
apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion "27.0.3"

defaultConfig {
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
javaCompileOptions {
annotationProcessorOptions {
arguments = [moduleName: project.getName()]
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
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'
testCompile 'junit:junit:4.12'

compile 'com.alibaba:arouter-api:1.2.1'
annotationProcessor 'com.alibaba:arouter-compiler:1.1.2'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'io.reactivex:rxjava:1.1.0'
compile 'io.reactivex:rxandroid:1.1.0'
}