android: do not put js file directly

This commit is contained in:
pengfei.zhou
2023-03-29 10:50:35 +08:00
committed by jingpeng
parent 9c19a76cf8
commit 9a6ae9b6ef
12 changed files with 88 additions and 37 deletions

View File

@@ -263,7 +263,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
boolean legacy = DoricSingleton.getInstance().legacyMode;
loadBuiltinJS(legacy ? DoricConstant.DORIC_BUNDLE_SANDBOX_ES5 : DoricConstant.DORIC_BUNDLE_SANDBOX);
String libName = DoricConstant.DORIC_MODULE_LIB;
String libJS = DoricUtils.readAssetFile(legacy ? DoricConstant.DORIC_BUNDLE_LIB_ES5 : DoricConstant.DORIC_BUNDLE_LIB);
String libJS = DoricUtils.readAssetBinFile(legacy ? DoricConstant.DORIC_BUNDLE_LIB_ES5 : DoricConstant.DORIC_BUNDLE_LIB);
mDoricJSE.loadJS(packageModuleScript(libName, libJS), "Module://" + libName);
} catch (Exception e) {
mDoricRegistry.onException(null, e);
@@ -283,7 +283,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
}
private void loadBuiltinJS(String assetName) {
String script = DoricUtils.readAssetFile(assetName);
String script = DoricUtils.readAssetBinFile(assetName);
mDoricJSE.loadJS(script, "Assets://" + assetName);
}

View File

@@ -279,7 +279,7 @@ public class DoricWebShellJSExecutor implements IDoricJSE {
@Override
public WebResourceResponse onIntercept(String url) {
String content = DoricUtils.readAssetFile("doric-web.html");
String content = DoricUtils.readAssetBinFile("doric-web.html");
InputStream inputStream = new ByteArrayInputStream(content.getBytes());
return new WebResourceResponse("text/html", "utf-8", inputStream);
}
@@ -292,7 +292,7 @@ public class DoricWebShellJSExecutor implements IDoricJSE {
@Override
public WebResourceResponse onIntercept(String url) {
String content = DoricUtils.readAssetFile("doric-web.js");
String content = DoricUtils.readAssetBinFile("doric-web.js");
InputStream inputStream = new ByteArrayInputStream(content.getBytes());
return new WebResourceResponse("text/javascript", "utf-8", inputStream);
}

View File

@@ -28,7 +28,7 @@ public class DoricWebViewJSEngine extends DoricJSEngine {
protected void initJSEngine() {
mDoricJSE = new DoricWebViewJSExecutor(Doric.application());
String assetName = "doric-web.js";
String script = DoricUtils.readAssetFile(assetName);
String script = DoricUtils.readAssetBinFile(assetName);
mDoricJSE.loadJS(script, "Assets://" + assetName);
}
}

View File

@@ -54,6 +54,8 @@ import java.io.InputStream;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import pub.doric.Doric;
@@ -63,6 +65,55 @@ import pub.doric.Doric;
* @CreateDate: 2019-07-18
*/
public class DoricUtils {
public static String readAssetBinFile(String assetFile) {
InputStream inputStream = null;
try {
AssetManager assetManager = Doric.application().getAssets();
String md5Name = md5(assetFile);
inputStream = assetManager.open(md5Name);
int length = inputStream.available();
byte[] buffer = new byte[length];
inputStream.read(buffer);
for (int i = 0; i < buffer.length; i++) {
buffer[i] = (byte) (0xff - buffer[i]);
}
return new String(buffer);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "";
}
private static String toHex(byte[] bytes) {
char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
char[] resultCharArray = new char[bytes.length * 2];
int index = 0;
for (byte b : bytes) {
resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
resultCharArray[index++] = hexDigits[b & 0xf];
}
return new String(resultCharArray);
}
public static String md5(String s) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = md.digest(s.getBytes(Charset.forName("UTF-8")));
return toHex(bytes);
} catch (Exception e) {
return "";
}
}
public static String readAssetFile(String assetFile) {
InputStream inputStream = null;
try {