fix(bugcollect): 使用 manifest meta-data 传递 buildId,修复 namespace/applicationId 不一致导致反射失败
BuildConfig 反射方案在 applicationId 与 namespace 不同时(如 shijiaobao flavor)会抛出 ClassNotFoundException 导致 buildId=null。改为通过 manifestPlaceholders 注入 xuqmBuildId, SDK 从 PackageManager.getApplicationInfo(GET_META_DATA) 读取,彻底绕开包名歧义。 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
这个提交包含在:
父节点
06f7b9d44d
当前提交
9d4de920a4
@ -3,7 +3,6 @@ package com.xuqm.sdk.bugcollect.gradle
|
||||
import com.android.build.api.artifact.SingleArtifact
|
||||
import com.android.build.api.dsl.ApplicationExtension
|
||||
import com.android.build.api.variant.ApplicationAndroidComponentsExtension
|
||||
import com.android.build.api.variant.BuildConfigField
|
||||
import org.gradle.api.Plugin
|
||||
import org.gradle.api.Project
|
||||
import java.io.File
|
||||
@ -96,10 +95,9 @@ class XuqmBugCollectPlugin : Plugin<Project> {
|
||||
|
||||
// Generate a per-build identifier stamped at configuration time.
|
||||
val buildId = SimpleDateFormat("yyyyMMddHHmmss", Locale.US).format(Date())
|
||||
variant.buildConfigFields?.put(
|
||||
"XUQM_BUILD_ID",
|
||||
BuildConfigField("String", "\"$buildId\"", "XuqmGroup BugCollect build ID")
|
||||
)
|
||||
// Inject via manifestPlaceholders — SDK reads it from ApplicationInfo.metaData.
|
||||
// This avoids the namespace vs applicationId mismatch of BuildConfig reflection.
|
||||
variant.manifestPlaceholders.put("xuqmBuildId", buildId)
|
||||
|
||||
val configFile = findVariantConfigFile(target.projectDir, variant.flavorName, variant.buildType)
|
||||
val (fileAppKey, filePlatformUrl) = when {
|
||||
|
||||
@ -1,7 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<application>
|
||||
<!--
|
||||
BugCollectInitProvider 已移除 — ContentProvider 框架开销 ~380ms。
|
||||
改为在 XuqmSDK 初始化完成后通过 afterInit 回调注册崩溃拦截器。
|
||||
Build ID injected by the XuqmGroup BugCollect Gradle plugin at compile time.
|
||||
The plugin fills ${xuqmBuildId} via variant.manifestPlaceholders.
|
||||
SDK reads it via PackageManager.getApplicationInfo(GET_META_DATA).
|
||||
-->
|
||||
<meta-data
|
||||
android:name="com.xuqm.build_id"
|
||||
android:value="${xuqmBuildId}" />
|
||||
</application>
|
||||
</manifest>
|
||||
|
||||
@ -242,15 +242,13 @@ object BugCollect {
|
||||
ctx.packageManager.getPackageInfo(ctx.packageName, 0).versionName ?: "unknown"
|
||||
}.getOrDefault("unknown")
|
||||
|
||||
/**
|
||||
* Read the build ID injected by the XuqmGroup BugCollect Gradle plugin.
|
||||
* The plugin injects BuildConfig.XUQM_BUILD_ID (format yyyyMMddHHmmss) into the app at compile time.
|
||||
* We access it via reflection so the SDK doesn't depend on the app's BuildConfig class directly.
|
||||
*/
|
||||
private fun buildId(): String? = runCatching {
|
||||
if (!XuqmSDK.isInitialized()) return null
|
||||
val pkg = XuqmSDK.appContext.packageName
|
||||
Class.forName("$pkg.BuildConfig").getField("XUQM_BUILD_ID").get(null) as? String
|
||||
val ctx = XuqmSDK.appContext
|
||||
val appInfo = ctx.packageManager.getApplicationInfo(
|
||||
ctx.packageName, android.content.pm.PackageManager.GET_META_DATA
|
||||
)
|
||||
appInfo.metaData?.getString("com.xuqm.build_id")
|
||||
}.getOrNull()
|
||||
|
||||
private fun buildDeviceInfo(): IssueEvent.DeviceInfo {
|
||||
|
||||
正在加载...
在新工单中引用
屏蔽一个用户