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

@ -65,7 +65,7 @@ public class DoricSSRActivity extends AppCompatActivity {
RootNode rootNode = new RootNode(doricContext); RootNode rootNode = new RootNode(doricContext);
rootNode.setRootView(doricPanel); rootNode.setRootView(doricPanel);
String filePath = getIntent().getStringExtra("file"); String filePath = getIntent().getStringExtra("file");
String json = DoricUtils.readAssetFile("src/" + filePath); String json = DoricUtils.readAssetBinFile("src/" + filePath);
try { try {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
JSONObject jsonObject = new JSONObject(json); JSONObject jsonObject = new JSONObject(json);

View File

@ -63,11 +63,12 @@ import pub.doric.devkit.R;
import pub.doric.devkit.qrcode.DisplayUtil; import pub.doric.devkit.qrcode.DisplayUtil;
import pub.doric.devkit.qrcode.activity.CaptureActivity; import pub.doric.devkit.qrcode.activity.CaptureActivity;
import pub.doric.devkit.qrcode.activity.CodeUtils; import pub.doric.devkit.qrcode.activity.CodeUtils;
import pub.doric.utils.DoricUtils;
import static pub.doric.devkit.ui.DoricShowNodeTreeActivity.DORIC_CONTEXT_ID_KEY; import static pub.doric.devkit.ui.DoricShowNodeTreeActivity.DORIC_CONTEXT_ID_KEY;
public class DoricDevActivity extends AppCompatActivity implements DoricDev.StatusCallback { public class DoricDevActivity extends AppCompatActivity implements DoricDev.StatusCallback {
private int REQUEST_CODE = 100; private final int REQUEST_CODE = 100;
private ContextCellAdapter cellAdapter; private ContextCellAdapter cellAdapter;
@Override @Override
@ -282,26 +283,6 @@ public class DoricDevActivity extends AppCompatActivity implements DoricDev.Stat
} }
} }
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);
}
private 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 "";
}
}
private static class ContextCellAdapter extends RecyclerView.Adapter<ContextCellHolder> { private static class ContextCellAdapter extends RecyclerView.Adapter<ContextCellHolder> {
private final ArrayList<DoricContext> contexts = new ArrayList<>(); private final ArrayList<DoricContext> contexts = new ArrayList<>();
@ -360,11 +341,11 @@ public class DoricDevActivity extends AppCompatActivity implements DoricDev.Stat
context.getSource())); context.getSource()));
String btnTitle = holder.itemView.getContext().getString(android.R.string.ok); String btnTitle = holder.itemView.getContext().getString(android.R.string.ok);
builder.setMessage( builder.setMessage(
String.format(Locale.getDefault(), String.format(Locale.getDefault(),
"Size:%d\nMD5:%s\nScript:\n%s", "Size:%d\nMD5:%s\nScript:\n%s",
context.getScript().length(), context.getScript().length(),
md5(context.getScript()), DoricUtils.md5(context.getScript()),
context.getScript())) context.getScript()))
.setPositiveButton(btnTitle, new DialogInterface.OnClickListener() { .setPositiveButton(btnTitle, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {

View File

@ -20,11 +20,6 @@ android {
consumerProguardFiles 'proguard-rules.pro' consumerProguardFiles 'proguard-rules.pro'
} }
} }
sourceSets {
main {
assets.srcDirs = ["../../doric-js/bundle"]
}
}
} }
dependencies { dependencies {

View File

@ -0,0 +1 @@
蹬趕憤旬箕<EFBFBD><EFBFBD><EFBFBD><EFBFBD>㫠癪葃<EFBFBD><EFBFBD>隸葔葔<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>𡯁搌姘痀楸釓癪葔葃<EFBFBD><EFBFBD>𡚺<EFBFBD><EFBFBD><EFBFBD>𣞼<EFBFBD><EFBFBD><EFBFBD>𧟌<EFBFBD><EFBFBD><EFBFBD>隸葔譜<EFBFBD><EFBFBD>隸葔<EFBFBD><EFBFBD><EFBFBD>癪葔葃袜<EFBFBD><EFBFBD>孊楞眾岋<EFBFBD>隸葔譜<EFBFBD><EFBFBD>隸葔<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>湗謄<EFBFBD><EFBFBD><EFBFBD>夂癪葃<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>暕癪葔<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>𨪚<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>䬬劏<EFBFBD><EFBFBD><EFBFBD>𠵯<EFBFBD>癪葔葔<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>秺癪葔葔<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>𠾐軀<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>暊癪葔葔<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>㑳軀<EFBFBD><EFBFBD><EFBFBD><EFBFBD>蘭葔葔葔<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>𨌘軀<EFBFBD><EFBFBD><EFBFBD>蘭葔葔葔<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>𥐙<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>𤣩蘭葔葔<EFBFBD>癪葔<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>葔葔葔<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>耂軀<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>𥐙<EFBFBD><EFBFBD><EFBFBD><EFBFBD>秺癪葔葔<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>秺癪葔<EFBFBD>蘣葔葔<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>趼蘭葔譜<EFBFBD><EFBFBD><EFBFBD>隸譜<EFBFBD><EFBFBD>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

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

View File

@ -279,7 +279,7 @@ public class DoricWebShellJSExecutor implements IDoricJSE {
@Override @Override
public WebResourceResponse onIntercept(String url) { 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()); InputStream inputStream = new ByteArrayInputStream(content.getBytes());
return new WebResourceResponse("text/html", "utf-8", inputStream); return new WebResourceResponse("text/html", "utf-8", inputStream);
} }
@ -292,7 +292,7 @@ public class DoricWebShellJSExecutor implements IDoricJSE {
@Override @Override
public WebResourceResponse onIntercept(String url) { 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()); InputStream inputStream = new ByteArrayInputStream(content.getBytes());
return new WebResourceResponse("text/javascript", "utf-8", inputStream); return new WebResourceResponse("text/javascript", "utf-8", inputStream);
} }

View File

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

View File

@ -54,6 +54,8 @@ import java.io.InputStream;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import pub.doric.Doric; import pub.doric.Doric;
@ -63,6 +65,55 @@ import pub.doric.Doric;
* @CreateDate: 2019-07-18 * @CreateDate: 2019-07-18
*/ */
public class DoricUtils { 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) { public static String readAssetFile(String assetFile) {
InputStream inputStream = null; InputStream inputStream = null;
try { try {

View File

@ -1,4 +1,6 @@
const fs = require("fs"); const fs = require("fs");
const path = require("path");
const crypto = require("crypto");
async function work() { async function work() {
const imageDts = await fs.promises.readFile("src/image.d.ts", "utf-8"); const imageDts = await fs.promises.readFile("src/image.d.ts", "utf-8");
@ -13,6 +15,24 @@ ${indexDts
${imageDts} ${imageDts}
`; `;
await fs.promises.writeFile("index.d.ts", content); await fs.promises.writeFile("index.d.ts", content);
const files = [
"bundle/doric-lib.js",
"bundle/doric-sandbox.js",
"bundle/doric-web.js",
"bundle/doric-web.html",
];
const androidAssets = "../doric-android/doric/src/main/assets";
for (let file of files) {
const md5 = crypto.createHash("md5");
md5.update(path.basename(file));
const name = md5.digest("hex").toLowerCase();
const data = await fs.promises.readFile(file);
const temp = new Uint8Array(data.buffer);
for (let i = 0; i < temp.length; i++) {
temp[i] = 0xff - temp[i];
}
await fs.promises.writeFile(path.resolve(androidAssets, name), data);
}
} }
work(); work();