Skip to content

IsaiahKing/JsBridge-1

 
 

Repository files navigation

JsBridge

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());

web调用方式

//无返回值
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>;
}

About

web和原生app交互简单封装

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 97.5%
  • HTML 2.5%