之前在项目中在线集成的X5内核,但是效果不好,只有手机设备这种装过微信QQ的机器加载成功的概率才比较理想,其它工程机器基本都是失败的,因此考虑静态集成到项目里边。
一、获取X5内核的方法
打开调试页,安转内核,logcat过滤:PLUGIN-TbsDownloadDebugPlugin ,可以看到下载链接
参考文章:https://blog.csdn.net/qq_34205629/article/details/122375262#comments_20014428
二、拷贝内核
拷贝到assets目录下,初始化的时候再拷贝到cache目录下,其它目录也可
1.拷贝方法
public static String getAssetsCacheFile(Context context, String fileName) {
File cacheFile = new File(context.getCacheDir(), fileName);
try {
InputStream inputStream = context.getAssets().open(fileName);
try {
FileOutputStream outputStream = new FileOutputStream(cacheFile);
try {
byte[] buf = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, len);
}
} finally {
outputStream.close();
}
} finally {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return cacheFile.getAbsolutePath();
}
三、安装本地内核
根据系统abi选择32位和64位的tbs内核,安装本地内核并初始化,
private fun initX5() {
if (QbSdk.getTbsVersion(applicationContext) == 0) {
Logger.i(TAG, "Install")
val phoneCpus = Build.SUPPORTED_ABIS
var fileName = ""
if (phoneCpus[0] == "arm64-v8a") {
fileName = "046007_x5.tbs.apk"
Log.i(TAG, "64bit $fileName")
} else if (phoneCpus[0] == "armeabi-v7a") {
fileName = "045912_x5.tbs.apk"
Log.i(TAG, "32bit $fileName")
}
if (fileName.isBlank()) {
Log.i(TAG, "no support")
initX5Setting()
}
val newPath = FileUtil.getAssetsCacheFile(applicationContext, fileName)
Log.i(TAG, "path $newPath")
QbSdk.reset(applicationContext)
QbSdk.installLocalTbsCore(applicationContext, fileName.substring(0, fileName.indexOf("_")).toInt(), newPath)
QbSdk.setTbsListener(object : TbsListener {
override fun onDownloadFinish(i: Int) {}
override fun onInstallFinish(i: Int) {
Logger.i(TAG, "onInstallFinish: $i")
initX5Setting()
}
override fun onDownloadProgress(i: Int) {}
})
} else {
Logger.i(TAG, "onlyInit")
initX5Setting()
}
}
private fun initX5Setting() {
//避免第一次打开需要安转X5,设置的的监听导致内存泄漏,重置为null
QbSdk.setTbsListener(null)
val map = HashMap<String, Any>(2)
map[TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER] = true
map[TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE] = true
QbSdk.initTbsSettings(map)
Logger.i(
TAG, "canLoadX5: ${QbSdk.canLoadX5(applicationContext)}|TbsVersion:" + QbSdk.getTbsVersion(
applicationContext
)
)
}
四、内核地址
32位(45912)
链接:https://pan.baidu.com/s/1pz_HSnzdzvEQ9RI5ohOVQg
提取码:ad9l
64位(46007)
链接:https://pan.baidu.com/s/1E1wgW1-ztHDg5xANDcePog
提取码:sil3