web和原生app交互简单封装
{
"action" : “XXXX”,
"data" : {
"xxx" : "xxxx"
...
}
}
implementation project(':jsbridge-core')
annotationProcessor project(':jsbridge-compiler')
@Bridge("android") //window.andriod
@JsConfig(jsMethod = "call", actionName = "function", paramsName = "data") //window.android.call()
public class BridgeTest {
private static final String TAG = "BridgeTest";
@JsAction("test")
public void test(){
Log.i(TAG, "test: ");
}
@JsAction("testData")
public void testData(TestBean test){
Log.i(TAG, "testData: " + test.name +" " + test.data);
}
@UnHandle
public void UnHandle(String request){
Log.w(TAG, "UnHandle: " + request);
}
@JsError
public void error(String request, Exception e){
Log.e(TAG, "error: " +request, e);
}
@JsFunc("func")
public String func(String request){
return "func data";
}
}
JsBridge.bind(webView, new BridgeTest());
//无返回值
function call(){
var json = "{\"function\":\"test\", \"data\": {\"name\" : \"pxq\"}}"
window.android.call(json);
}
//同步调用
function callSync(){
var json = "{\"function\":\"func\", \"data\": {\"name\" : \"pxq\"}}"
var result = window.android.callSync(json);
}
#保持注解不被混淆
-keep class com.pxq.jsbridge.annotation.Bridge
#保持@Bridge注解的类不被混淆
-keep @com.pxq.jsbridge.annotation.Bridge class *
#保持生成的类不被混淆
-keep class **$$Bridge {
#保留构造方法
<init>(...);
#保留交互方法
@android.webkit.Javascriptinterface <method>;
}