fix(sdk): 完善初始化异常处理和日志记录
- 在 XuqmInitializerProvider 中添加 autoInitialize 失败时的错误日志记录 - 在 autoInitialize 方法中添加许可证文件缺失时的详细错误日志 - 添加包名匹配失败时的错误日志记录 - 添加许可证读取成功时的调试日志 - 新增 listAssets 辅助方法用于列出资产目录文件
这个提交包含在:
父节点
e958b2a42c
当前提交
42b644ac11
@ -44,16 +44,36 @@ object XuqmSDK {
|
|||||||
*/
|
*/
|
||||||
fun autoInitialize(context: Context, logLevel: LogLevel = LogLevel.WARN) {
|
fun autoInitialize(context: Context, logLevel: LogLevel = LogLevel.WARN) {
|
||||||
val licenseData = readLicenseFileData(context)
|
val licenseData = readLicenseFileData(context)
|
||||||
?: throw IllegalStateException(
|
if (licenseData == null) {
|
||||||
|
android.util.Log.e(
|
||||||
|
"XuqmSDK",
|
||||||
|
"[autoInitialize] License file not found or unreadable. " +
|
||||||
|
"Expected assets/xuqm/license.xuqm or assets/xuqm/*.xuqmlicense. " +
|
||||||
|
"Actual files in assets/xuqm/: ${listAssets(context, "xuqm")}",
|
||||||
|
)
|
||||||
|
throw IllegalStateException(
|
||||||
"No license file found in assets/xuqm/. " +
|
"No license file found in assets/xuqm/. " +
|
||||||
"Download license.xuqm from the tenant platform and place it in src/main/assets/xuqm/."
|
"Download license.xuqm from the tenant platform and place it in src/main/assets/xuqm/."
|
||||||
)
|
)
|
||||||
|
}
|
||||||
val appKey = licenseData.first
|
val appKey = licenseData.first
|
||||||
val serverUrl = licenseData.second
|
val serverUrl = licenseData.second
|
||||||
val licensePackageName = licenseData.third
|
val licensePackageName = licenseData.third
|
||||||
|
|
||||||
val localPackageName = context.packageName
|
val localPackageName = context.packageName
|
||||||
|
|
||||||
|
android.util.Log.d(
|
||||||
|
"XuqmSDK",
|
||||||
|
"[autoInitialize] License read OK — " +
|
||||||
|
"appKey=${appKey.take(8)}***, serverUrl=$serverUrl, " +
|
||||||
|
"licensePackageName=$licensePackageName, localPackageName=$localPackageName",
|
||||||
|
)
|
||||||
|
|
||||||
if (licensePackageName != null && licensePackageName.isNotBlank() && licensePackageName != localPackageName) {
|
if (licensePackageName != null && licensePackageName.isNotBlank() && licensePackageName != localPackageName) {
|
||||||
|
android.util.Log.e(
|
||||||
|
"XuqmSDK",
|
||||||
|
"[autoInitialize] Package name mismatch — " +
|
||||||
|
"license expects '$licensePackageName', app running as '$localPackageName'.",
|
||||||
|
)
|
||||||
throw IllegalStateException(
|
throw IllegalStateException(
|
||||||
"License package name mismatch: license=$licensePackageName, local=$localPackageName. " +
|
"License package name mismatch: license=$licensePackageName, local=$localPackageName. " +
|
||||||
"Please download the correct license file for this app."
|
"Please download the correct license file for this app."
|
||||||
@ -63,6 +83,9 @@ object XuqmSDK {
|
|||||||
initialize(context, appKey, serverUrl, logLevel)
|
initialize(context, appKey, serverUrl, logLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun listAssets(context: Context, dir: String): String =
|
||||||
|
runCatching { context.assets.list(dir)?.toList().toString() }.getOrDefault("(error listing)")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manual initialization without license file.
|
* Manual initialization without license file.
|
||||||
* The SDK will validate the appKey against the server by calling /api/sdk/config
|
* The SDK will validate the appKey against the server by calling /api/sdk/config
|
||||||
|
|||||||
@ -18,6 +18,13 @@ class XuqmInitializerProvider : ContentProvider() {
|
|||||||
if (!XuqmSDK.isInitialized()) {
|
if (!XuqmSDK.isInitialized()) {
|
||||||
XuqmSDK.autoInitialize(ctx)
|
XuqmSDK.autoInitialize(ctx)
|
||||||
}
|
}
|
||||||
|
}.onFailure { e ->
|
||||||
|
android.util.Log.e(
|
||||||
|
"XuqmSDK",
|
||||||
|
"[XuqmInitializerProvider] autoInitialize failed — SDK will be uninitialized. " +
|
||||||
|
"Cause: ${e.javaClass.simpleName}: ${e.message}",
|
||||||
|
e,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
正在加载...
在新工单中引用
屏蔽一个用户