Android Gradle AGP8 部署
添加插件
-
在 settings.gradle 文件中添加基调听云仓库。
pluginManagement {
repositories {
gradlePluginPortal()
google()
maven { url "https://nexus2.tingyun.com/nexus/content/repositories/snapshots/" }// 添加基调听云仓库
//maven { url uri('./repo') }// 本地集成需将 repo 文件夹复制到项目根目录
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
maven { url "https://nexus2.tingyun.com/nexus/content/repositories/snapshots/" }// 添加基调听云仓库
//maven { url uri('./repo') }// 本地集成需将 repo 文件夹复制到项目根目录
}
} -
在 Project 级别的 build.gradle 文件中添加基调听云插件。
plugins {
id 'com.android.application' version "8.1.0" apply false //demo used
id 'com.networkbench.gradleplugin.agp8on' version "2.17.6" apply false //添加基调听云插件
} -
在 App 级别的 build.gradle 文件中添加以下内容。
apply plugin:'newlens'// 放在 apply plugin: 'com.android.application' 下面
dependencies {
implementation "com.networkbench:tingyun-ea-agent-android:2.17.6"
implementation "com.networkbench.newlens.agent.android2:nbs.newlens.nativecrash:2.0.8"// 采集native carsh 需集成此包
implementation "com.networkbench:nbs.newlens.android.log:1.0.2" // 日志回捞需集成此包
// 采集 OOM 数据需要 kotlin-gradle-plugin 插件 1.3+ ,并依赖 androidx.core:core-ktx、androidx.appcompat:appcompat、androidx.lifecycle:lifecycle-process、com.squareup.okio:okio 等库
implementation "com.networkbench:tingyun-javaleak:1.0.2" // 采集 OOM 需集成此包
implementation "org.bouncycastle:bcprov-jdk15to18:1.78.1"// 启用国密加密需集成此包
implementation "org.bouncycastle:bcpkix-jdk15to18:1.78.1"// 启用国密加密需集成此包
}
添加基调听云SDK
-
获取基调听云 App Key。
-
初始化 SDK。
i. 在 Application 中的 onCreate() 方法初始化Android SDK。
//"Appkey" 请从基调听云平台获取
//"Host" 为基调听云平台「Redirect」服务器地址,无需添加协议头
// setStartOption(7) 设置首次启动开启网络、用户体验、崩溃采集,仅首次启动生效
NBSAppAgent.setLicenseKey("AppKey").setRedirectHost("Host").setStartOption(7).start(this.getApplicationContext());ii. SDK默认以HTTPS上传数据,若服务端只支持HTTP,需设置 「setHttpEnabled(true)」。
NBSAppAgent.setLicenseKey("AppKey").setRedirectHost("Host").setHttpEnabled(true).setStartOption(7).start(this.getApplicationContext());
权限配置说明
基调听云App SDK 为了与服务端交互「网络权限」为必要权限。
<!--必要权限,用以与服务端交互-->
<uses-permission android:name="android.permission.INTERNET"/>
<!--非必要权限,用以获取当前设备的网络状态和WiFi状态,如:2G、3G、4G、WiFi,建议添加-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!--非必要权限,用以获取 targentVersion 29 及以上 Android 10 设备的网络状态-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!--非必要权限,用以使用「可视化操作命名功能」-->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW"/>
<!--非必要权限,用以获取当前移动网络连接的基站信息-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
添加 WebView 配置
说明:自动注入 JS 探针和腾讯「 X5 Webview」配置详情参见接口说明。
采集 WebView 数据需设置 setDomStorageEnabled(true),还需调用 setWebViewClient() 方法,如嵌码 App 中未调用该方法,请添加以下内容:
WebSettings webSettings = webview.getSettings();
webSettings.setDomStorageEnabled(true);
webview.setWebViewClient(new WebViewClient(){});
- 相关接口
/*
采集 WebView 数据需在 WebChromeClient 的 onProgressChanged() 方法中调用本接口
*/
NBSWebChromeClient.initJSMonitor(view, newProgress);
- 代码示例
webview.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
NBSWebChromeClient.initJSMonitor(view, newProgress);
super.onProgressChanged(view, newProgress);
}
});
获取用户标识
通过添加「用户标识」可在基调听云报表平台通过该标识检索到具体用户的性能问题。
- 相关接口
//userIdentifier 最多包含256个字符,支持中文、英文、数字、下划线,但不能包含空格或其他的转义字符
NBSAppAgent.setUserIdentifier(String userIdentifier);
- 代码示例
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String userIdentifier = getUserID();
NBSAppAgent.setLicenseKey("AppKey").start(this.getApplicationContext());
//用户标识可为邮箱、手机号等能够标识用户身份的信息,如:xxx@tingyun.com
NBSAppAgent.setUserIdentifier(userIdentifier);
}
}