配置
1、根目录build.gradle添加
buildscript {
dependencies {
classpath 'com.billy.android:autoregister:1.3.0'
}
}
2、根目录新建cc-settings.gradle文件,并拷贝https://raw.githubusercontent.com/luckybilly/CC/master/cc-settings.gradle到新建文件中;
除基础模块外,所有模块的build.gradle
apply plugin: 'com.android.library'
//或
apply plugin: 'com.android.application'
//改为
apply from: rootProject.file('cc-settings.gradle')
//主模块需添加如下代码
ext.mainApp = true //设置为true,表示此module为主app module,一直以application方式编译
其余模块若需独立运行,需要在根目录的local.properties中添加
模块名=true
使用
3、对外提供功能(同App同进程提供Fragment界面),实现IComponent接口,并重写其方法。
public class ComponentMap implements IComponent {
@Override
public String getName() {
return "component_map";//CC组件名
}
@Override
public boolean onCall(CC cc) {
Context context = cc.getContext();
Intent intent;
switch (cc.getActionName()) {
case "get_map_fragment"://CC动作指令
CC.sendCCResult(cc.getCallId(), CCResult.success("map_fragment", new MapFragment()));
break;
case "trail_back":
intent = new Intent(context, TrailBackActivity.class);
if (!(context instanceof Activity)) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
intent.putExtra("callId", cc.getCallId());
context.startActivity(intent);
break;
case "realtime_trajectory":
intent = new Intent(context, RealTimeTrajectoryActivity.class);
if (!(context instanceof Activity)) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
intent.putExtra("callId", cc.getCallId());
context.startActivity(intent);
break;
}
return false;
}
}
4、调用定义的组件
//同步调用,直接返回结果
CCResult result = CC.obtainBuilder("ComponentA").build().call();
CC.obtainBuilder(Const.COMPONENT_MAP).setActionName(Const.MAP_REALTIME_TRAJECTORY).build().call();
//或 异步调用,不需要回调结果
String callId = CC.obtainBuilder("ComponentA").build().callAsync();
//或 异步调用,在子线程执行回调
String callId = CC.obtainBuilder("ComponentA").build().callAsync(new IComponentCallback(){...});
//或 异步调用,在主线程执行回调
String callId = CC.obtainBuilder("ComponentA").build().callAsyncCallbackOnMainThread(new IComponentCallback(){...});
5、模块依赖,主模块依赖其他模块,其他模块不能相互依赖。
依赖本地组件
addComponent 'component_map',project(':component_map')//如果module名称跟dependencyName相同,project(':component_map')可省略
依赖maven远程组件
addComponent 'component_user', 'cn.vkel.electrocar:compnent_user:1.0.0'