Name: ARouter
Owner: Alibaba
Description: An android router middleware that help app navigating to activities and custom services.
Created: 2016-12-14 01:27:49.0
Updated: 2018-05-24 15:40:24.0
Pushed: 2018-05-21 08:21:01.0
Size: 22128
Language: Java
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Android????????????????????????? ?? ??????
??|arouter-api|arouter-compiler|arouter-annotation|arouter-register —|—|—|—|— ????||||
oid {
ultConfig {
CompileOptions {
annotationProcessorOptions {
arguments = [ moduleName : project.getName() ]
}
dependencies {
// ???????, ??????api
// ??compiler?????????????????
compile 'com.alibaba:arouter-api:x.x.x'
annotationProcessor 'com.alibaba:arouter-compiler:x.x.x'
...
} // ???gradle??(< 2.2)?????apt??????????'??#4' // Kotlin??????'??#5'
???
// ?????????????(??) // ???????????????????/xx/xx @Route(path = “/test/activity”) public class YourActivity extend Activity {
...
}
??SDK
if (isDebug()) { // ???????init??????????init??????
ARouter.openLog(); // ????
ARouter.openDebug(); // ??????(???InstantRun???????????????????????,???????)
} ARouter.init(mApplication); // ????????Application????
?????
// 1. ????????(??URL???'????'?) ARouter.getInstance().build(“/test/activity”).navigation();
// 2. ??????? ARouter.getInstance().build(“/test/1”)
.withLong("key1", 666L)
.withString("key3", "888")
.withObject("key4", new Test("Jack", "Rose"))
.navigation();
?????(?????Proguard)
-keep public class com.alibaba.android.arouter.routes.*{;} -keep class * implements com.alibaba.android.arouter.facade.template.ISyringe{*;}
-keep interface * implements com.alibaba.android.arouter.facade.template.IProvider
-keep class * implements com.alibaba.android.arouter.facade.template.IProvider
? Gradle ????????????
apply plugin: 'com.alibaba.arouter'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "com.alibaba:arouter-register:1.0.0"
}
}
??? ARouter ???????????????????????? dex ???
?? gradle ???????????????????????????????
?????????????????????????? api 1.3.0 ???
??????
?URL??
// ????Activity????Schame??,?????url???ARouter?? public class SchameFilterActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Uri uri = getIntent().getData();
ARouter.getInstance().build(uri).navigation();
finish();
}
}
oidManifest.xml
<!-- Schame -->
<intent-filter>
<data
android:host="m.aliyun.com"
android:scheme="arouter"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
?URL????
// ???????????????? @Autowired ?? // URL?????Parcelable???????ARouter api????Parcelable?? @Route(path = “/test/activity”) public class Test1Activity extends Activity {
@Autowired
public String name;
@Autowired
int age;
@Autowired(name = "girl") // ??name???URL??????
boolean boy;
@Autowired
TestObj obj; // ??????????URL???json??
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ARouter.getInstance().inject(this);
// ARouter?????????????????
Log.d("param", name + age + boy);
}
}
// ???????????????? SerializationService,???@Route????(?????????????)???? @Route(path = “/service/json”) public class JsonServiceImpl implements SerializationService {
@Override
public void init(Context context) {
}
@Override
public <T> T json2Object(String text, Class<T> clazz) {
return JSON.parseObject(text, clazz);
}
@Override
public String object2Json(Object instance) {
return JSON.toJSONString(instance);
}
}
????(?????????????)
// ??????????????????????????????????????? // ???????????????????????????? @Interceptor(priority = 8, name = “??????”) public class TestInterceptor implements IInterceptor {
@Override
public void process(Postcard postcard, InterceptorCallback callback) {
...
callback.onContinue(postcard); // ??????????
// callback.onInterrupt(new RuntimeException("???????")); // ????????????
// ???????????????????????
}
@Override
public void init(Context context) {
// ??????????sdk??????????????????
}
}
?????
// ???????navigation?????????????? ARouter.getInstance().build(“/test/1”).navigation(this, new NavigationCallback() {
@Override
public void onFound(Postcard postcard) {
...
}
@Override
public void onLost(Postcard postcard) {
...
}
});
????????
// ??DegradeService????????Path????????? @Route(path = “/xxx/xxx”) public class DegradeServiceImpl implements DegradeService { @Override public void onLost(Context context, Postcard postcard) {
// do something.
}
@Override public void init(Context context) {
} }
??????????
// ??????????????????????"??????"??? // ???? Route ???? extras ?????????????? int?????????int?4??????32??????32??? // ????????????????????32????????????????????????????????????????? @Route(path = “/test/activity”, extras = Consts.XXXX)
???????:????(?) ????
// ????,????????????? public interface HelloService extends IProvider {
String sayHello(String name);
}
// ???? @Route(path = “/service/hello”, name = “????”) public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "hello, " + name;
}
@Override
public void init(Context context) {
}
}
???????:????(?) ????
public class Test {
@Autowired
HelloService helloService;
@Autowired(name = "/service/hello")
HelloService helloService2;
HelloService helloService3;
HelloService helloService4;
public Test() {
ARouter.getInstance().inject(this);
}
public void testService() {
// 1. (??)?????????????,????????,???????????
// Autowired?????name???????byName??????????????name????????byType???????(??????????????????byName???????)
helloService.sayHello("Vergil");
helloService2.sayHello("Vergil");
// 2. ??????????????????????????????????byName?byType
helloService3 = ARouter.getInstance().navigation(HelloService.class);
helloService4 = (HelloService) ARouter.getInstance().build("/service/hello").navigation();
helloService3.sayHello("Vergil");
helloService4.sayHello("Vergil");
}
}
??????
????????
ARouter.openLog(); // ???? ARouter.openDebug(); // ??InstantRun?????????????????????????? ARouter.printStackTrace(); // ?????????????
??API??
// ????????? ARouter.getInstance().build(“/home/main”).navigation();
// ??????????????? ARouter.getInstance().build(“/home/main”, “ap”).navigation();
// ????????????Uri???? Uri uri; ARouter.getInstance().build(uri).navigation();
// ??????????startActivityForResult // navigation?????????Activity????????RequestCode ARouter.getInstance().build(“/home/main”, “ap”).navigation(this, 5);
// ????Bundle Bundle params = new Bundle(); ARouter.getInstance()
.build("/home/main")
.with(params)
.navigation();
// ??Flag ARouter.getInstance()
.build("/home/main")
.withFlags();
.navigation();
// ??Fragment Fragment fragment = (Fragment) ARouter.getInstance().build(“/test/fragment”).navigation();
// ???? ARouter.getInstance()
.withObject("key", new TestObj("Jack", "Rose"))
.navigation();
// ??????????????Bundle?? ARouter.getInstance()
.build("/home/main")
.getExtra();
// ????(????) ARouter.getInstance()
.build("/test/activity2")
.withTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom)
.navigation(this);
// ????(API16+) ActivityOptionsCompat compat = ActivityOptionsCompat.
makeScaleUpAnimation(v, v.getWidth() / 2, v.getHeight() / 2, 0, 0);
// ps. makeSceneTransitionAnimation ?????????????navigation???????Activity
ARouter.getInstance()
.build("/test/activity2")
.withOptionsCompat(compat)
.navigation();
// ??????(????????) ARouter.getInstance().build(“/home/main”).greenChannel().navigation();
// ????????????? ARouter.setLogger();
????URI
String uriStr = getIntent().getStringExtra(ARouter.RAW_URI);
???URL
// ??PathReplaceService????????Path????????? @Route(path = “/xxx/xxx”) // ?????? public class PathReplaceServiceImpl implements PathReplaceService {
/**
* For normal path.
*
* @param path raw path
*/
String forString(String path) {
return path; // ???????????????????
}
/**
* For uri type.
*
* @param uri raw uri
*/
Uri forUri(Uri uri) {
return url; // ???????????????????
} }
????
???????
- SDK????????(/test/1 /test/2)??????????????????????????????????????
- ???? @Route ??????????????????????(/*/)????
- ??????????????????????? ARouter.getInstance().build(path, group) ??????????????????
@Route(path = “/test/1”, group = “app”)
????????
- ??????????????????????????? init(Context context) ??????????????
- ???????????????????????????ARouter????????????????????????????????????????????????
- ???????????????App??????????????????????????????????
ack ??????
- ~~????????????????????????????????javac?api????Jack?????javac????????????~~
- ???????????build.gradle???moduleName?????
??gradle???????
apply plugin: 'com.neenbedankt.android-apt'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
}
apt {
arguments {
moduleName project.getName();
}
}
dependencies {
compile 'com.alibaba:arouter-api:x.x.x'
apt 'com.alibaba:arouter-compiler:x.x.x'
...
}
otlin????????
// ???? module-kotlin ?????? apply plugin: 'kotlin-kapt'
kapt {
arguments {
arg("moduleName", project.getName())
}
}
dependencies {
compile 'com.alibaba:arouter-api:x.x.x'
kapt 'com.alibaba:arouter-compiler:x.x.x'
...
}
??Q&A
W/ARouter::: ARouter::No postcard![ ]"
??Log????????????????????????DegradeService?PathReplaceService?????ARouter??????????
?????Service?????ARouter????????????????PathReplaceService??????????(????)????????
?????????????????
???app???DegradeService?PathReplaceService
W/ARouter::: ARouter::There is no route match the path [/xxx/xxx], in group [xxx][ ]"
- ???????????????????????
- ???????????????????????????
1. ?????????????????????????? (@Route(path="/test/test"), ????????????group???????)
2. ????????????gradle???????? arouter-compiler sdk (??????????apt??????compile?????)
3. ???????????????? ARouter::Compiler >>> xxxxx ??????????????????
4. ??App??????debug?log(openDebug/openLog), ????????????????? D/ARouter::: LogisticsCenter has already been loaded, GroupIndex[4]?GroupIndex > 0
?InstantRun??????(???Gradle???????)?
????InstantRun???????????????dex??????????ARouter?????????????????????????openDebug??
ARouter?????InstantRun???????????????????init**??**??openDebug
ransformException:java.util.zip.ZipException: duplicate entry ....
ARouter????????????????? 6-1 ???ARouter????module???????????????module??????????????????
otlin??????????????
???Kotlin?????????????????????????????????????????Kotlin?????set/get??????????private
??????Kotlin????????private??????????????? @JvmField
?URL??????intent???????????
??????????????????????? `ARouter.getInstance().inject(this)`???????????????? `@Autowired` ?????
?????????ARouter??????????????URL???????Intent????????intent?????????
???????????
ARouter??Dex???????????????ARouter???????????????(?????versionCode??)?????????(ARouter.openDebug())?
ARouter ???????????????????????? Debug ??
????
????
1. ???1 (?????2?)
![qq](https://raw.githubusercontent.com/alibaba/ARouter/master/demo/arouter-qq-addr.png)
2. ???2
![qq](https://raw.githubusercontent.com/alibaba/ARouter/master/demo/qq-qrcode-2.JPG)
?????? (???????????)
imknown](https://github.com/alibaba/ARouter/commits?author=imknown) : ??????
crazy1235](https://github.com/crazy1235) : ?????????????
luckybilly](https://github.com/luckybilly) : ?? Transform API ?????????
LinXiaoTao](https://github.com/LinXiaoTao) : postcard transition support 0
tanglie1993](https://github.com/tanglie1993) : ?????????