• Stars
    star
    159
  • Rank 235,916 (Top 5 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created almost 8 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple android framework used to route activity or action with url.

EasyRouter

欢迎加入Android技术交流群,群号码:577953847

EasyRouter是一个简易的使用字符串进行Activity,Browser跳转的路由框架,并支持组件化开发。

Features

  • EasyRouter实现了通过字符串进行Activity之间跳转路由,通过APT在编译器实现路由表的构建,劫持了startActivity()进行动态路由
  • EasyRouter实现了Activity之间跳转,返回时的数据自动注入,完全屏蔽了原生的一套繁琐API
  • EasyRouter实现了通过字符串进程Browser的路由跳转
  • EasyRouter支持更换路由跳转时数据序列化的解析器,默认为Gson,可以通过EasyRouterSet进行更换
  • EasyRouter劫持了onActivityResult(),并将其改为接口回调
  • EasyRouter通过transform+ASM实现了一个gradle插件来支持组件化开发

Usage

1.在Activity中自定义URL标识符,目前只支持单一的URL标识,URL的Scheme均为activity://

@Route("activity://main")
public class MainActivity extends AppCompatActivity {
}

2.在Application中进行初始化

EasyRouter.init(this);

3.跳转

  • Activity之间无参数传递跳转
Message message = new MessageBuilder().setAddress("activity://two").build();
EasyRouter.route(MainActivity.this, message);
  • Activity之间有参数传递跳转
Message message = new MessageBuilder()
                        .setAddress("activity://two")
                        .addParam("data", "haha", String.class)
                        .addParam("person", new Person(21, "Zane"), Person.class)
                        .build();
EasyRouter.route(MainActivity.this, message);

被启动Activity需要用@Param标记参数变量

@Route("activity://two")
public class ActivityTwo extends AppCompatActivity{

    @Param("data")
    public String data;
    @Param("person")
    public Person person;
}
  • Activity需要回调的启动(startActivityForResult())
Message message = new MessageBuilder()
                               .setAddress("activity://two")
                               .addParam("data", "haha", String.class)
                               .addParam("person", new Person(21, "Zane"), Person.class)
                               .build();
EasyRouter.routeForResult(MainActivity.this, message, new OnActivityResultListener() {
          @Override
          public void onActivityResult(int resultCode, Intent data) {
                //dosomething
          }
});

如果有返回的数据,需要通过@Result标记返回参数变量

@Route("activity://main")
public class MainActivity extends AppCompatActivity {

    @Result("result_three")
    public String resultThree;
    @Result("result_two")
    public String resultTwo;
}
  • Activity的setResult()
EasyRouter.setResult(ActivityTwo.this, 0, new MessageBuilder()
                                                   .addParam("result_two", "data from two", String.class)
                                                   .build());
finish();
  • Web页面跳转
EasyRouter.route(MainActivity.this, new MessageBuilder()
                                             .setAddress("http://xzane.cc")
                                             .build());
  • 更改数据序列化的工具

首先需要实现一个序列化工具的工厂类,可以参考框架中的GsonConvertFactory

EasyRouterSet.setConverterFactory(GsonConventerFactory.creat());

Principle

  • 使用动态代理生成Instrumentation代理类并通过反射替换ActivityThread中的Instrumentation变量,劫持execStartActivity()方法达到动态查找路由表并进行跳转的效果
  • 所有的参数注入代码以及路由表的代码生成均通过APT在编译期完成
  • onActivityResult方法的劫持是通过生成无View的Fragment达到的,借鉴了RxPermission
  • 通过registerActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback)去劫持所有Activity的onCreat()方法
  • 编译期通过transform获取编译产物,使用ASM进行AOP操作融合application和library的路由表

Dependency

  • 在Project build.gradle中
allprojects {
 repositories {
    maven { url "https://jitpack.io" }
 }
}
  • 在Application build.gradle中
dependencies {
    compile 'com.github.Zane96.EasyRouter:router:v1.1.1'
    annotationProcessor 'com.github.Zane96.EasyRouter:easyrouter-compiler:v1.1.1'
}
  • 如果您希望支持Android组件化开发,那么还需要在Project的build.gradle中添加如下依赖:
buildscript {
    dependencies {
        classpath 'com.zane:easyrouterMerge:1.0.0'
    }
}

并在Application和Library的build.gradle中均添加如下插件:

apply plugin: 'me.zane.easyrouter-merge'

TODO

  • 返回数据,自动注入
  • 请求报文实体类封装,Builder类生成URL,头部,数据body
  • 支持组件化开发
  • 跳转时候的参数应该不依赖key的值,要自动或者手动注入
  • 地址做成多个

License

Copyright 2017 Zane

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

More Repositories

1

Fairy

Android system log viewer on Android phone without root.
Java
530
star
2

GithubQuery

由rxjava+retrofit2+dagger2+mvp架构完成的查询github用户信息的app by android。
Java
43
star
3

P2P_UDP

使用Android+Node.js实现基于UDP协议的P2P打洞测试,移动端
Java
41
star
4

EasyMVP

在安卓上运用的MVP模式开发框架。A framework for MVP pattern in android.
Java
38
star
5

ICY-ClaTable

i重邮项目课表查询功能。by android
Java
19
star
6

BookManager

个人练手项目:图书管理,阅读计划类型app by android。(Rxjava, Retrofit2, dagger2, EasyMVP, Datamanager, MD设计)
Java
15
star
7

EasyImage

运用在安卓开发上提供本地,缓存,网络三方图片的快速开发库
Java
14
star
8

P2P_UDP_SERVER

使用Android+Node.js实现基于UDP协议的P2P打洞测试,服务端
JavaScript
12
star
9

EasyLineCounter

a gradle plugin to count the number of lines of code in android project
Groovy
7
star
10

BestBitmapDownload

使用双向弱引用防止recycleview图片异步加载错位。
Java
5
star
11

Zane96.github.io

纪录程序人生
HTML
4
star
12

EasyHomeWork

数据库比赛app
Java
4
star
13

EventBus_Annotation

EventBus源码的中文注释
Java
2
star
14

SharedElements_ViewDragHelper

一个简单的练习页面跳转组件共享以及仿Youtube拖拉控件的效果demo
Java
2
star
15

Cheese

一个符合material design的app by android,练习了design包的各种控件。
Java
2
star
16

mvp-test-login

一个用MVP模式实现的登入页面。A realization of the MVP pattern login page.
Java
1
star
17

NetListener

A simple app to make a cell phone access through IP addresses to listen for other phones by android(Zane.)
Java
1
star
18

custom-view-touch-test

一些自定义的view来加强对Touch的认识
Java
1
star
19

Script_Set

脚本练习集合
Python
1
star