api 'com.tencent.tbs.tbssdk:sdk:43939'
为了保障内核的动态下发和正常使用,您需要在您的AndroidManifest.xml增加如下权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
为了保障X5功能的正常使用,您需要在您的proguard.cfg文件中添加如下配置:
-dontwarn dalvik.**
-dontwarn com.tencent.smtt.**
-keep class com.tencent.smtt.** {
*;
}
-keep class com.tencent.tbs.** {
*;
}
为了提高合作方的webview场景稳定性,及时发现并解决x5相关问题,当客户端发生crash等异常情况并上报给服务器时请务必带上x5内核相关信息。x5内核异常信息获取接口为:com.tencent.smtt.sdk.WebView.getCrashExtraMessage(context)。以bugly日志上报为例:
UserStrategy strategy = new UserStrategy(appContext);
strategy.setCrashHandleCallback(new CrashReport.CrashHandleCallback() {
public Map<String, String> onCrashHandleStart(
int crashType,
String errorType,
String errorMessage,
String errorStack) {
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
String x5CrashInfo = com.tencent.smtt.sdk.WebView.getCrashExtraMessage(appContext);
map.put("x5crashInfo", x5CrashInfo);
return map;
}
@Override
public byte[] onCrashHandleStart2GetExtraDatas(
int crashType,
String errorType,
String errorMessage,
String errorStack) {
try {
return "Extra data.".getBytes("UTF-8");
} catch (Exception e) {
return null;
}
}
});
CrashReport.initCrashReport(appContext, APPID, true, strategy);
TBS内核首次使用和加载时,ART虚拟机会将Dex文件转为Oat,该过程由系统底层触发且耗时较长,很容易引起anr问题,解决方法是使用TBS的 ”dex2oat优化方案“。
(1). 设置开启优化方案
// 在调用TBS初始化、创建WebView之前进行如下配置
HashMap map = new HashMap();
map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);
QbSdk.initTbsSettings(map);
(2). 增加Service声明
在AndroidManifest.xml中增加内核首次加载时优化Service声明。
该Service仅在TBS内核首次Dex加载时触发并执行dex2oat任务,任务完成后自动结束。
<service
android:name="com.tencent.smtt.export.external.DexClassLoaderProviderService"
android:label="dexopt"
android:process=":dexopt" >
</service>
下载 SDK jar 包放到工程的libs目录下,将源码和XML里的系统包和类替换为SDK里的包和类,具体对应如下:
系统内核 | SDK内核 |
---|---|
android.webkit.ConsoleMessage | com.tencent.smtt.export.external.interfaces.ConsoleMessage |
android.webkit.CacheManager | com.tencent.smtt.sdk.CacheManager(deprecated) |
android.webkit.CookieManager | com.tencent.smtt.sdk.CookieManager |
android.webkit.CookieSyncManager | com.tencent.smtt.sdk.CookieSyncManager |
android.webkit.CustomViewCallback | com.tencent.smtt.export.external.interfaces.IX5WebChromeClient.CustomViewCallback |
android.webkit.DownloadListener | com.tencent.smtt.sdk.DownloadListener |
android.webkit.GeolocationPermissions | com.tencent.smtt.export.external.interfaces.GeolocationPermissionsCallback |
android.webkit.HttpAuthHandler | com.tencent.smtt.export.external.interfaces.HttpAuthHandler |
android.webkit.JsPromptResult | com.tencent.smtt.export.external.interfaces.JsPromptResult |
android.webkit.JsResult | com.tencent.smtt.export.external.interfaces.JsResult |
android.webkit.SslErrorHandler | com.tencent.smtt.export.external.interfaces.SslErrorHandler |
android.webkit.ValueCallback | com.tencent.smtt.sdk.ValueCallback |
android.webkit.WebBackForwardList | com.tencent.smtt.sdk.WebBackForwardList |
android.webkit.WebChromeClient | com.tencent.smtt.sdk.WebChromeClient |
android.webkit.WebHistoryItem | com.tencent.smtt.sdk.WebHistoryItem |
android.webkit.WebIconDatabase | com.tencent.smtt.sdk.WebIconDatabase |
android.webkit.WebResourceResponse | com.tencent.smtt.export.external.interfaces.WebResourceResponse |
android.webkit.WebSettings | com.tencent.smtt.sdk.WebSettings |
android.webkit.WebSettings.LayoutAlgorithm | com.tencent.smtt.sdk.WebSettings.LayoutAlgorithm |
android.webkit.WebStorage | com.tencent.smtt.sdk.WebStorage |
android.webkit.WebView | com.tencent.smtt.sdk.WebView |
android.webkit.WebViewClient | com.tencent.smtt.sdk.WebViewClient |
需要注意的是:
1)请不要在代码里使用下述写法:
import android.*;
import android.webkit.*;
import android.webkit.WebStorage.*;
import android.net.*;
import android.net.http.*;
2)除了源码里需要把相关的包名和类名进行替换,布局xml里的声明也需要替换,例如:
<com.tencent.smtt.sdk.WebView
android:id="@+id/forum_context"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="5dp"
android:paddingRight="5dp" />
请确保将所有的相关class的包名均按照上述规则替换,以免造成类型强转crash、cookie 设置失败等问题发生。
Tips:您可使用工具进行批量扫描和替换,将脚本放置于根目录并执行脚本即可。下载地址:
TBS已提供9种主流文件格式的本地打开,如果您需要使用更高级的能力请使用QQ浏览器打开文件
doc、docx、ppt、pptx、xls、xlsx、pdf、txt、epub
rar(包含加密格式)、zip(包含加密格式)、tar、bz2、gz、7z(包含加密格式)、doc、docx、ppt、pptx、xls、xlsx、txt、pdf、epub、chm、html/htm、xml、mht、url、ini、log、bat、php、js、lrc、jpg、jpeg、png、gif、bmp、tiff 、webp、mp3、m4a、aac、amr、wav、ogg、mid、ra、wma、mpga、ape、flac
public static int openFileReader(
Context context,
String filePath,
HashMap<String, String> extraParams,
ValueCallback<String> callback
)
(1)此方法在Qbsdk
类下
(2)调用之后,优先调起 QQ 浏览器打开文件。如果没有安装 QQ 浏览器,在 X5 内核下调起简版 QB 打开文 件。如果使用的系统内核,则调起文件阅读器弹框。
(3)暂时只支持本地文件打开
context
: 调起 miniqb
的 Activity
的 context
。此参数只能是 activity
类型的 context
,不能设置为 Application
的 context。
filePath
: 文件路径。格式为 android
本地存储路径格式,例如:/sdcard/Download/xxx.doc
. 不支持 file:///
格式。暂不支持在线文件。
extraParams
: miniqb
的扩展功能。为非必填项,若无需特殊配置,默认可传入null
。扩展功能参考“文件功能定制”
ValueCallback
: 提供 miniqb
打开/关闭时给调用方回调通知,以便应用层做相应处理,您可以在出现以下回调时结束您的进程,节约内存占用。主要回调值如下:
filepath error
TbsReaderDialogClosed
default browser
fileReaderClosed
返回值:
1:用 QQ 浏览器打开
2:用 MiniQB 打开
3:调起阅读器弹框
-1:filePath 为空 打开失败
public static void closeFileReader(Context context)
主动关闭文件打开ui,并清理相应内存占用。
HashMap<String, String> params = new HashMap<String, String>();
params.put("style", "1");
params.put("local", "true");
params.put("memuData", jsondata);
QbSdk.openFileReader(ctx,”/sdcard/xxx.doc”, params,callback);