Skip to content

Commit 105c4ba

Browse files
committed
add support min API is 14
1 parent 19eef3f commit 105c4ba

31 files changed

Lines changed: 393 additions & 45 deletions

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ v1.0.2
1616

1717
1.h5调用原生和回调原生后切换线程到main线程,便于后续业务的处理
1818

19+
v1.0.3
1920

21+
1.完善JS桥API<17的实现,低版本采用onJsPrompt实现JS调用原生
22+
2.优化代码
2023

2124

2225

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
#### JSBridge-Android
2-
[ ![Download](https://api.bintray.com/packages/smallbuer/tools/jsbridge/images/download.svg?version=1.0.2) ](https://bintray.com/smallbuer/tools/jsbridge/1.0.2/link) [![Release Version](https://img.shields.io/badge/release-1.0.2-red.svg)](https://github.com/smallbuer/JSBridge-Android/releases) [![Platform](https://img.shields.io/badge/Platform-Android-brightgreen.svg)](https://github.com/smallbuer/JSBridge-Android)
2+
[ ![Download](https://api.bintray.com/packages/smallbuer/tools/jsbridge/images/download.svg?version=1.0.3) ](https://bintray.com/smallbuer/tools/jsbridge/1.0.3/link) [![Release Version](https://img.shields.io/badge/release-1.0.3-red.svg)](https://github.com/smallbuer/JSBridge-Android/releases) [![Platform](https://img.shields.io/badge/Platform-Android-brightgreen.svg)](https://github.com/smallbuer/JSBridge-Android)
33
### 简介
44

55
本项目来源于 lzyzsd的[JsBridge](https://github.com/lzyzsd/JsBridge),由于作者长时间未修复部分代码丢失问题,所以目前存在的调用丢失问题以及效率问题无法在原项目得到回应,所以重构了目前js文件以及java类;
66

77
主要修改如下:
88

99
不再使用URL SHEME拦截方式,直接采用webview的addJavaScriptInterface,此方法根据Android源码跟踪,是目前采用webview方案js与原生交互效率最高的一种系统实现;
10-
11-
10+
对于API小于17的使用者,1.0.3版本将支持版本降低到14,满足一些朋友的需要,为了安全主要实现方式利用onJsPrompt方法让H5可以和原生进行交互,并且移除了低版本的三个危险漏洞;
11+
```
12+
//4.2之前addJavascriptInterface有安全泄漏风险,进行移除
13+
webView.removeJavascriptInterface("searchBoxJavaBridge_");
14+
webView.removeJavascriptInterface("accessibility");
15+
webView.removeJavascriptInterface("accessibilityTraversal");
16+
```
1217
本项目保持和lzyzsd老用户的兼容性,内部也重构了BridgeWebView的实现,直接作为View使用即可;
1318
同时也扩展了外部webview的扩展方案,例如X5webview,UC内核(官方申请合作使用,此demo供学习使用)的示例;
1419

1520
欢迎提供好的实现意见与PR;
1621

17-
如果只想解决lzyzsd的[JsBridge]项目调用桥丢失问题,可以参考files目录下的README,将JS文件替换即可
22+
如果只想解决lzyzsd的[JsBridge]项目调用桥调用丢失问题,可以参考files目录下的README,将JS文件替换其项目即可
1823

19-
jscript文件下的js文件为新版本SDK内部字符串压缩前的原文件;
24+
jscript文件下的js文件为新版本SDK内部字符串压缩前的原文件,文件带Min的为API<17的低版本使用,不带的为>=17使用和之前保持一致;
25+
JS源文件在java代码中已经做了压缩处理,此文件只为了供大家查看使用,不需要添加到项目中;
2026

2127
### 导入SDK
2228

@@ -31,7 +37,7 @@ repositories {
3137
```
3238
3339
dependencies {
34-
implementation 'com.smallbuer:jsbridge:1.0.2'
40+
implementation 'com.smallbuer:jsbridge:1.0.3'
3541
}
3642
```
3743

app/build.gradle

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@ android {
99
buildToolsVersion "29.0.2"
1010
defaultConfig {
1111
applicationId "com.smallbuer.jsbridge.demo"
12-
minSdkVersion 21
12+
minSdkVersion 14
1313
targetSdkVersion 29
1414
versionCode 1
1515
versionName "1.0"
1616
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1717

18-
ndk{
19-
abiFilters "armeabi-v7a"
20-
}
2118
}
2219

2320
compileOptions {
@@ -39,7 +36,7 @@ dependencies {
3936

4037
//implementation project(':jsbridge')
4138

42-
implementation 'com.smallbuer:jsbridge:1.0.2'
39+
implementation 'com.smallbuer:jsbridge:1.0.3'
4340

4441
implementation 'com.github.tbruyelle:rxpermissions:0.10.2'
4542
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'

app/libs/uc_webview_sdk.jar

100755100644
File mode changed.

app/src/main/assets/jsbridge/demo.html

100755100644
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,10 @@
118118
});
119119

120120
bridge.registerHandler("functionInJs", function(data, responseCallback) {
121+
console.log(data);
121122
document.getElementById("show").innerHTML = ("data from Java: = " + data);
122123
if (responseCallback) {
123-
var responseData = "Javascript Says Right back aka!";
124+
var responseData = "I am from Javascript";
124125
responseCallback(responseData);
125126
}
126127
});

app/src/main/java/com/smallbuer/jsbridge/demo/MainActivity.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.os.Bundle
55
import android.util.Log
66
import android.view.View
77
import android.widget.RadioGroup
8+
import android.widget.Toast
89
import androidx.appcompat.app.AppCompatActivity
910
import com.tbruyelle.rxpermissions2.RxPermissions
1011
import kotlinx.android.synthetic.main.activity_main.*
@@ -51,6 +52,7 @@ class MainActivity : AppCompatActivity() , RadioGroup.OnCheckedChangeListener {
5152

5253
x5Webview.callHandler("functionInJs", "我是原生传递的参数") { data ->
5354
Log.i(TAG, "reponse data from js $data"+",Thread is "+Thread.currentThread().name)
55+
Toast.makeText(this@MainActivity,data,Toast.LENGTH_SHORT).show()
5456
}
5557
}
5658

@@ -69,8 +71,8 @@ class MainActivity : AppCompatActivity() , RadioGroup.OnCheckedChangeListener {
6971

7072
bridgeWebview.callHandler("functionInJs", "我是原生传递的参数") { data ->
7173

72-
7374
Log.i(TAG, "reponse data from js $data"+",Thread is "+Thread.currentThread().name)
75+
Toast.makeText(this@MainActivity,data,Toast.LENGTH_SHORT).show()
7476
}
7577
}
7678

@@ -91,6 +93,7 @@ class MainActivity : AppCompatActivity() , RadioGroup.OnCheckedChangeListener {
9193

9294
ucWebview.callHandler("functionInJs", "我是原生传递的参数") { data ->
9395
Log.i(TAG, "reponse data from js $data"+",Thread is "+Thread.currentThread().name)
96+
Toast.makeText(this@MainActivity,data,Toast.LENGTH_SHORT).show()
9497
}
9598
}
9699

app/src/main/java/com/smallbuer/jsbridge/demo/view/UCWebView.java

100755100644
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,26 @@
77

88
import androidx.annotation.Nullable;
99

10+
import com.smallbuer.jsbridge.core.BridgeLog;
1011
import com.smallbuer.jsbridge.core.BridgeTiny;
1112
import com.smallbuer.jsbridge.core.IWebView;
1213
import com.smallbuer.jsbridge.core.OnBridgeCallback;
14+
import com.uc.webview.export.JsPromptResult;
15+
import com.uc.webview.export.WebChromeClient;
1316
import com.uc.webview.export.WebSettings;
1417
import com.uc.webview.export.WebView;
1518
import com.uc.webview.export.WebViewClient;
1619

1720
public class UCWebView extends WebView implements IWebView {
1821

22+
private String TAG = "UCWebView";
1923
private BridgeTiny bridgeTiny;
2024

2125
@SuppressLint("SetJavaScriptEnabled")
2226
public UCWebView(Context arg0, AttributeSet arg1) {
2327
super(arg0, arg1);
2428
this.setWebViewClient(client);
29+
this.setWebChromeClient(chromeClient);
2530
initWebViewSettings();
2631
bridgeTiny = new BridgeTiny(this);
2732
}
@@ -68,6 +73,17 @@ public void onPageFinished(WebView webView, String s) {
6873

6974
};
7075

76+
private WebChromeClient chromeClient = new WebChromeClient(){
77+
@Override
78+
public boolean onJsPrompt(WebView webView, String url, String message, String defaultValue, JsPromptResult jsPromptResult) {
79+
BridgeLog.d(TAG,"message->"+message);
80+
bridgeTiny.onJsPrompt(UCWebView.this,message);
81+
//don't delete this line
82+
jsPromptResult.confirm("do");
83+
return true;
84+
}
85+
};
86+
7187
@Override
7288
public void destroy() {
7389
super.destroy();

app/src/main/java/com/smallbuer/jsbridge/demo/view/X5WebView.java

100755100644
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,30 @@
33
import android.annotation.SuppressLint;
44
import android.content.Context;
55
import android.util.AttributeSet;
6+
67
import androidx.annotation.Nullable;
8+
9+
import com.smallbuer.jsbridge.core.BridgeLog;
710
import com.smallbuer.jsbridge.core.BridgeTiny;
811
import com.smallbuer.jsbridge.core.IWebView;
912
import com.smallbuer.jsbridge.core.OnBridgeCallback;
13+
import com.tencent.smtt.export.external.interfaces.JsPromptResult;
1014
import com.tencent.smtt.sdk.ValueCallback;
15+
import com.tencent.smtt.sdk.WebChromeClient;
1116
import com.tencent.smtt.sdk.WebSettings;
1217
import com.tencent.smtt.sdk.WebSettings.LayoutAlgorithm;
1318
import com.tencent.smtt.sdk.WebView;
1419
import com.tencent.smtt.sdk.WebViewClient;
1520

1621
public class X5WebView extends WebView implements IWebView {
17-
22+
private String TAG = "X5WebView";
1823
private BridgeTiny bridgeTiny;
1924

2025
@SuppressLint("SetJavaScriptEnabled")
2126
public X5WebView(Context arg0, AttributeSet arg1) {
2227
super(arg0, arg1);
2328
this.setWebViewClient(client);
29+
this.setWebChromeClient(chromeClient);
2430
initWebViewSettings();
2531
this.getView().setClickable(true);
2632
bridgeTiny = new BridgeTiny(this);
@@ -68,6 +74,21 @@ public void onPageFinished(WebView webView, String s) {
6874

6975
};
7076

77+
private WebChromeClient chromeClient = new WebChromeClient(){
78+
@Override
79+
public boolean onJsPrompt(WebView webView, String url,String message, String defaultValue, JsPromptResult jsPromptResult) {
80+
BridgeLog.d(TAG,"message->"+message);
81+
bridgeTiny.onJsPrompt(X5WebView.this,message);
82+
//don't delete this line
83+
jsPromptResult.confirm("do");
84+
return true;
85+
}
86+
};
87+
88+
89+
90+
91+
7192
@Override
7293
public void destroy() {
7394
super.destroy();
-14.3 KB
Loading
-8.85 KB
Loading

0 commit comments

Comments
 (0)