Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add the dependency
dependencies {
implementation 'com.github.lwugang.android-js-bridge-v2:library:v2.0.3'
implementation 'com.github.lwugang.android-js-bridge-v2:js-bridge-anno:v2.0.3'
annotationProcessor 'com.github.lwugang.android-js-bridge-v2:js-bridge-compiler:v2.0.3'
}
<com.wugang.jsbridge.library.BridgeWebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/web_view"/>
- 所有需要注入的对象推荐继承JsPlugin这个类并且 加上 @JsInject 注解标记
- 被 @JsInject 标记的类会被自动注入,并调用空参的构造创建对象,如果有自定义构造 可以使用 webView.getJsBridge().register()
- 如果该类中的方法不希望被注入可以 使用 @JsInject 注解上的 filter参数过滤掉
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BridgeWebView webView = (BridgeWebView) findViewById(R.id.web_view);
// 如果需要自定义构造函数,需要手动注册
webView.getJsBridge().register("AB",new AB(this));
webView.loadUrl("file:///android_asset/test.html");
WebView.setWebContentsDebuggingEnabled(true);
}
@JsInject public static class AB extends JsPlugin {
private Context context;
public AB() {
}
public AB(Context context) {
this.context = context;
}
public void test(String s, JSFunction jsFunction) throws JSONException {
Toast.makeText(context, "js调用我", 1).show();
JSONObject jsonObject = new JSONObject();
jsonObject.put("aa",12);
jsFunction.execute(jsonObject.toString());
}
public String getResult(){
return "nativeResult";
}
public void test1() {
Log.e("-------", "test1: ");
}
public void test2() {
Log.e("-------", "test2: ");
}
}
}
HTML&JS代码
<html>
<script>
window.JSBridgeReady=function(){
console.log("---window EasyJSReady---")
}
function test(){
AB.test({"aa":123},function(ret){
alert("native 回调我"+JSON.stringify(ret))
})
}
</script>
<body>
<button onclick="javascript:location.reload()">refresh</button>
<button onclick="test()">Test</button>
<!--获取android返回的结果-->
<button onclick="javascript:alert(AB.getResult())">getNativeResult</button>
</body>
</html>
//预加载,推荐在Application中调用
PreLoadManager.get(this).preload("http://www.baidu.com", "http://www.youku.com");
public class PreLoadActivity extends AppCompatActivity {
@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BridgeWebView webView = PreLoadManager.get(this).getWebView();
setContentView(webView);
webView.setWebViewClient(new WebViewClient(){
@Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if(request.hasGesture()){
Intent intent = new Intent(PreLoadActivity.this,PreLoadActivity.class);
intent.putExtra("url",request.getUrl());
startActivity(intent);
return true;
}
return super.shouldOverrideUrlLoading(view, request);
}
});
webView.loadUrl(getIntent().getStringExtra("url"));
}
}
-keep com.liwg.jsbridge.library.**{*;}