第一步: 在主项目的根目录下新建cc-settings-2.gradle文件,键入以下内容:
project.apply plugin: 'cc-register'
//project.dependencies.add('api', "com.billy.android:cc:2.1.6") //用最新版
第二步:创建一个新的Model模块
1、modle的build.gradle 添加依赖
2、替换原来的apply plugin ‘XXX’为以下:
3、去掉applicationId
//第一步:替换原来的apply plugin 'XXX'
ext.alwaysLib = true
apply from: rootProject.file('cc-settings-2.gradle')
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
//第二步:去掉applicationId 或者如下:
//applicationId "com.heima.login"
//仅在以application方式编译时才添加applicationId属性
//if (project.ext.runAsApp) {
//applicationId 'com.billy.cc.demo.component.a'
//}
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//第三步:model添加依赖
implementation 'com.billy.android:cc:2.1.6'
}
第三步:在主项目的根目录下的(project)build.gradle依赖自动注册插件:
classpath "com.billy.android:cc-register:1.1.2" //使用最新版
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.1'
//第一步:添加插件
classpath "com.billy.android:cc-register:1.1.2" //使用最新版
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
第四步:在主项目的根目录下的(modle)build.gradle依赖自动注册插件,每个modle都要依赖:
1、添加依赖: implementation 'com.billy.android:cc:2.1.6'
2、添加其他模块的 modle 依赖
3、ext.mainApp = true //标记为主app module
apply from: rootProject.file('cc-settings-2.gradle')
//第一步将apply plugin ‘XXX’替换如下:
ext.mainApp = true //标记为主app module
apply from: rootProject.file('cc-settings-2.gradle')
android {
。。。。。。
}
dependencies {
。。。。。。
//第二步:添加依赖
implementation 'com.billy.android:cc:2.1.6'
//第三步:添加其它model 的依赖
//implementation project(path: ':login')
addComponent 'login'
}
第五步:创建ComponentA
package com.heima.login;
import android.util.Log;
import android.widget.Toast;
import com.billy.cc.core.component.CC;
import com.billy.cc.core.component.CCResult;
import com.billy.cc.core.component.CCUtil;
import com.billy.cc.core.component.IComponent;
/**
* author : yangjunjin
* date : 2020/3/31 0:02
*/
public class ComponentA implements IComponent {
@Override
public String getName() {
//指定组件的名称
return "ComponentA";
}
@Override
public boolean onCall(CC cc) {
String actionName = cc.getActionName();
switch (actionName) {
case "LoginActivity": //响应actionName为"showActivity"的组件调用
Log.e("ComponentA==","我过来了");
//跳转到页面:ActivityA
CCUtil.navigateTo(cc, LoginActivity.class);
//返回处理结果给调用方
CCResult result =CCResult.success().addData("yjj","我是最帅的");
CC.sendCCResult(cc.getCallId(),result);
//同步方式实现(在return之前听过CC.sendCCResult()返回组件调用结果),return false
return false;
default:
//其它actionName当前组件暂时不能响应,可以通过如下方式返回状态码为-12的CCResult给调用方
CC.sendCCResult(cc.getCallId(), CCResult.errorUnsupportedActionName());
return false;
}
}
}
第六步:在MainActivity实现跳转
//同步调用,直接返回结果
CCResult result = CC.obtainBuilder("ComponentA")
.setActionName("LoginActivity")
.build()
.call();
//或 异步调用,不需要回调结果
String callId1 = CC.obtainBuilder("ComponentA")
.setActionName("LoginActivity")
.build()
.callAsync();
//或 异步调用,在主线程执行回调
String callId2 = CC.obtainBuilder("ComponentA")
.setActionName("LoginActivity")
.build()
.callAsyncCallbackOnMainThread(new IComponentCallback() {
@Override
public void onResult(CC cc, CCResult result) {result.getDataItem("yjj");
//此onResult在主线程中运行
// String toast = "login " + (result.isSuccess() ? "success" : "failed");
String toast = "login " + result.getDataItem("yjj");
Toast.makeText(MainActivity.this, toast, Toast.LENGTH_SHORT).show();
}
});