参考微信jsBridge的一套完整的native-bridge-web协议与实现,清晰规范的开发Hybrid App
Support API v19+
Support androidx
- 对web端提供js-sdk,形成sdk概念,统一app对外api,统一的api管理(权限、版本兼容)
- 让native端api具备组件化能力,不在需要将所有api写个一个modlue中
- 保障native-web js交互的数据一致性、安全
- 项目中有较多的web与native交互,需要native统一提供对外api
- native组件化,需要在不同模块中实现api逻辑
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.hcanyz:ZJsBridge:$version'
}
- Webview(android、x5...)
- WebViewClient(添加一些方法调用,协助zjs感知webview生命周期)
- activity|fragment
- 注册api实现类
- 添加一些方法,协助zjs感知容器生命周期
class WebView : WebView, IZWebView {
private val zWebHelper: ZWebHelper by lazy { ZWebHelper(this) }
override fun getCurUrl(): String {
return url
}
override fun getCurContext(): Context {
return context
}
override fun getCurZWebHelper(): ZWebHelper {
return zWebHelper
}
override fun execJs(methodName: String, params: String?, valueCallback: ValueCallback<String>?) {
val js: String = if (params.isNullOrBlank()) {
String.format("%s()", methodName)
} else {
String.format("%s('%s')", methodName, params)
}
execJs(js, valueCallback)
}
override fun execJs(sourceJs: String, valueCallback: ValueCallback<String>?) {
if (ZJsBridge.ZJS_DEBUG) ZJsBridge.log("evaluateJavascript:javascript:$sourceJs")
runOnMainThread(Runnable {
evaluateJavascript("javascript:$sourceJs") { valueCallback?.onReceiveValue(it) }
})
}
override fun runOnMainThread(runnable: Runnable) {
if (Looper.getMainLooper() == Looper.myLooper()) {
runnable.run()
return
}
post(runnable)
}
}
addJavascriptInterface(ZJavascriptInterface(this), ZJavascriptInterface.INTERFACE_NAME)
private inner class InnerCustomWebViewClient : WebViewClient() {
override fun onPageFinished(webView: WebView?, s: String?) {
super.onPageFinished(webView, s)
zWebHelper.injectCoreJs()
}
override fun doUpdateVisitedHistory(p0: WebView?, p1: String?, p2: Boolean) {
super.doUpdateVisitedHistory(p0, p1, p2)
zWebHelper.injectCoreJs()
}
}
web_test.getCurZWebHelper().registeredJsApiHandler(this, ZCommonJsHandler::class.java)
web_test.getCurZWebHelper().registeredJsApiHandler(this, ImageJsHandler::class.java)
override fun closeWindow() {
finish()
}
override fun updateTitle(title: String) {
tv_test_tile.text = title
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
web_test.getCurZWebHelper().dispatchContainerResult(requestCode, resultCode, data)
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
web_test.getCurZWebHelper().dispatchContainerDestroy()
}
override fun onBackPressed() {
if (web_test.canGoBack()) {
web_test.goBack()
} else {
super.onBackPressed()
}
}
这个库提供了一个h5的api测试页面
本项目中已集成一个打包后的产物,可以自行编译替换(ZJsBridge\app\src\main\assets)