Android
新增main.jsbundle文件
首先在瀏覽器打開http://localhost:8081/index.ios.bundle?platform=android&dev=false(根據自己項目及平臺來),然後將自己創建一個文件main.jsbundle來保存此網址返回的代碼,最後放到Android的assets目錄中
新增寫文件代碼
private static final String JSBUNDLE_FILE = "main.jsbundle";
private static void copyFile(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
private void prepareJSBundle() {
File targetFile = new File(getFilesDir(), JSBUNDLE_FILE);
if (!targetFile.exists()) {
try {
OutputStream out = new FileOutputStream(targetFile);
InputStream in = getAssets().open(JSBUNDLE_FILE);
copyFile(in, out);
out.close();
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
onCreate中setContentView之後調用prepareJSBundle()即可;
Ios
新增main.jsbundle文件
首先在瀏覽器打開http://localhost:8081/index.ios.bundle?platform=ios&dev=false(根據自己項目及平臺來),然後將自己創建一個文件main.jsbundle來保存此網址返回的代碼,最後放到Ios的項目根目錄中