Skip to content

Repository files navigation

Android-Router

English | 中文

libandroidrouterandroidrouter-compilerandroidrouter-annotations
Latest Version2.0.91.0.11.0.0

High-performance, flexible, easy-to-use lightweight Android component-based framework, Used to solve the interdependence of complex projects, A single module is conducive to independent development and maintenance.

Update Log

2.0.9: Optimize exception throw.
2.0.7: Feature: add runtime class case in the getValue.
2.0.6: Fix empty list pares exception.
2.0.5: Fix ANR issue when 'getValue' used but return are void type.
2.0.4: Optimizing Code.
2.0.3: Fix inherit class cast exception,support force typecast of method who named 'getValue'.
2.0.2: Fix context param override exception.
2.0.1: Support auto cast on return value, fix array cast exception. 2.0.0: Resolve callback support generic, Support reactive programming, Remove resolve.call 'type' param. -----2.0+ new feature dont support 1.0+
1.0.7: Fix system params passed exception.
1.0.6: Fix known issues.
1.0.5: Support array params and extend params.
1.0.4: Support await the result return.It will block thread.
1.0.3: Support auto promise.resolve when return type not eq void.
1.0.2: Support thread switching.
1.0.1: Optimizing performance.
1.0.0: First push.

Goal

  • Project decoupling
  • Stand-alone develop,Stand-alone maintenance.
  • Make life better

Characteristics

  • Uses annotation processing to generate boilerplate code for you.
  • Exception centralized processing.
  • Any parameter type return.
  • Runtime parameter parse,Support different types of delivery.
    • From jsonObject => To Object
    • From jsonArray => To List
    • From Object A => To Object A
    • From Object A => To Object B
    • From List< A> => To Object List< B>

Modular architecture diagram

Gradle

//Add dependencies inside application/library.//android plugin version >= 2.2+dependencies {
compile 'com.library.tangxiaolv:androidrouter:x.x.x'
annotationProcessor 'com.library.tangxiaolv:androidrouter-compiler:x.x.x'
}
//android plugin version < 2.2
apply plugin: 'com.neenbedankt.android-apt'buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
}
dependencies {
compile 'com.library.tangxiaolv:androidrouter:x.x.x'
apt 'com.library.tangxiaolv:androidrouter-compiler:x.x.x'
}

Getting Started

Note:Android-Router Protocol Format: scheme://host/path?params=json

scheme[1] host[1] path[2] params[2] 1:required 2:option

Step 1:Setup router module

@RouterModule(scheme = "android", host = "main")
publicclassMainModuleimplementsIRouter {
//Route => android://main//default parameter: Application context, String scheme, VPromise promise@RouterPathpublicvoiddef(Applicationcontext, Stringscheme, VPromisepromise) {
promise.resolve("","from scheme: [" + scheme + "] " + "path: []");
}
//if return type not eq void, The promise will be auto called.@RouterPath("/autoReturn")
publicStringautoReturn(Stringscheme) {
return"I'm auto return!!!!! ";
}
//Route => android://main/activity/localActivity@RouterPath("/activity/localActivity")
publicvoidopenLocalActivityAndReturnResult(Applicationcontext, VPromisepromise) {
Stringtag = promise.getTag();
Intentintent = newIntent(context, LocalActivity.class);
intent.putExtra("tag", tag);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
//Take out the value from json object//Route => android://main/params/basis?params={'f':1,'i':2,'l':3,'d':4,'b':true}@RouterPath("/params/basis")
publicvoidparamsBasis(floatf, inti, longl, doubled, booleanb,
Stringscheme, VPromisepromise) {
promise.resolve("","from scheme: [" + scheme + "] " + "path: [/params/basis]");
}
//Take out the complex value from json object//Route => android://main/params/complex?params={'b':{},'listC':[]}@RouterPath("/params/complex")
publicvoidparamsComplex(Bb, List<C> listC, Stringscheme, VPromisepromise) {
promise.resolve("","from scheme: [" + scheme + "] " + "path: [/params/complex]");
}
//From json object => to object//The key must be "_params_"@RouterPath("/jsonObject")
publicvoidparamsPakege(Package_params_, Stringscheme, VPromisepromise) {
promise.resolve("","from scheme: [" + scheme + "] " + "path: [/jsonObject]");
}
//From json array => to List//The key must be "_params_"@RouterPath("/jsonArray")
publicvoidjsonArray(List<A> _params_, Stringscheme, VPromisepromise) {
promise.resolve("","from scheme: [" + scheme + "] " + "path: [/jsonArray]");
}
//eg: from A => to B//Note:The primitive params name and type must be the same.//Note:Both need implements IRouter and have empty constructor.@RouterPath("/differentTypes")
publicvoiddifferentTypes(Aa, List<A> listA, Stringscheme, VPromisepromise) {
promise.resolve("","from scheme: [" + scheme + "] " + "path: [/differentTypes]");
}
//If you want to return an error. Recommend use RouterRemoteException@RouterPath("/throwError")
publicvoidthrowError(VPromisepromise) {
promise.reject(newRouterRemoteException("I'm error................."));
}
@RouterPath("/reactive")
publicvoidreactive(VPromisepromise) {
promise.resolve("I'm from reactive!!!!!");
}
}
//Support multiple scheme.@RouterModule(scheme = "android|remote", host = "lib")
publicclassRemoteModuleimplementsIRouter {
@RouterPath("/openRemoteActivity")
publicvoidopenRemoteActivity(Applicationcontext, Stringscheme, VPromisepromise) {
Intentintent = newIntent(context, RemoteActivity.class);
intent.putExtra("tag", promise.getTag());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}

Supported parameter

from-todesc
floatfloat1.0.0+
intint1.0.0+
longlong1.0.0+
doubledouble1.0.0+
booleanboolean1.0.0+
StringString1.0.0+
Object AObject A1.0.0+
Object AObject B1.0.0+ A and B object must implement IRouter and keep empty constructor
A[]A[]2.0.1+
A[]B[]2.0.1+ A and B object must implement IRouter and keep empty constructor
A[]Varargs A2.0.1+ [1,2,3] → add(int... i)
List< A>List< A>1.0.0+ Receiver must be defined as List<?> interface
List< A>List< B>1.0.0+ A and B object must implement IRouter and keep empty constructor
Json ObjectObject1.0.0+
Json ObjectMap< String,String>2.0.1+
Json ArrayList< ?>1.0.0+

Step 2:Invoke

//Take the value with async.AndroidRouter
.open("android://main/activity/localActivity")
.callOnSubThread()
.returnOnMainThread()
.call(newResolve<String>() {
@Overridepublicvoidcall(Stringresult) {
//Receive result
}
}, newReject() {
@Overridepublicvoidcall(Exceptione) {
//All routing errors are callback here.
}
});
//orAndroidRouter
.open("android", "main", "/differentTypes")
.showTime()//Show time
.call();//Igone result and error.//or//Take the value with synchronized.booleanvalue = AndroidRouter.open("android://main/getValue").getValue();
//orAndroidRouter.open("android://main/reactive")
.call(newFunc<String, Integer>() {
@OverridepublicIntegercall(Strings) {
return1;
}
})
.then(newFunc<Integer, Double>() {
@OverridepublicDoublecall(Integerinteger) {
return2.0;
}
})
.then(newFunc<Double, String>() {
@OverridepublicStringcall(Doubleddouble) {
return"I'm reactive!";
}
})
.done(newResolve<String>() {
@Overridepublicvoidcall(Stringresult) {
title.setText(result);
}
}, newReject() {
@Overridepublicvoidcall(Exceptione) {
title.setText(e.toString());
}
});

Proguard

//Add proguard-rules
-keep class * implements com.tangxiaolv.router.interfaces.IMirror{*;}
-keep class * implements com.tangxiaolv.router.interfaces.IRouter{*;}

License

Copyright 2017 TangXiaoLv
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.

About

An android componentization protocol framework, used for decoupling complex project. Android高性能轻量级路由框架

Topics

Resources

Stars

216 stars

Watchers

8 watching

Forks

Releases

Packages

Used by

Contributors

Languages