当我启用缩小时,我面临一些奇怪的异常堆栈跟踪。奇怪的是它只发生在内部
setContent
。
release {
isDebuggable = false
isMinifyEnabled = true
isShrinkResources = true
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
signingConfig = signingConfigs.getByName(signingCfg)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
throw InvalidClassException("Test Exception")
LDAContent()
}
}
这段代码输出类似:
Caused by: java.io.InvalidClassException: Test Exception
at g4.a.h(Unknown Source:31)
at h0.c.h(Unknown Source:53)
at androidx.compose.ui.platform.k1.a(Unknown Source:24)
at s.l0.h(Unknown Source:163)
at h0.c.h(Unknown Source:53)
at y9.z.b(Unknown Source:151)
at androidx.compose.ui.platform.l1.a(Unknown Source:282)
at y.v.a(Unknown Source:38)
at y.v.h(Unknown Source:15)
at h0.c.h(Unknown Source:53)
at y9.z.b(Unknown Source:151)
at androidx.compose.ui.platform.w0.a(Unknown Source:484)
at androidx.compose.ui.platform.f3.a(Unknown Source:170)
at androidx.compose.ui.platform.f3.h(Unknown Source:27)
at h0.c.h(Unknown Source:53)
at y9.z.a(Unknown Source:216)
at androidx.compose.ui.platform.f3.a(Unknown Source:144)
at androidx.compose.ui.platform.f3.h(Unknown Source:15)
at h0.c.h(Unknown Source:53)
at d5.a.O(Unknown Source:14)
at a0.q.q(Unknown Source:117)
at a0.q.k(Unknown Source:10)
at a0.x.n(Unknown Source:33)
at a0.e2.a(Unknown Source:33)
at a0.x.o(Unknown Source:8)
at a0.x.g(Unknown Source:2)
at androidx.compose.ui.platform.g3.invoke(SourceFile:24)
at androidx.compose.ui.platform.AndroidComposeView.setOnViewTreeOwnersAvailable(Unknown Source:6)
at androidx.compose.ui.platform.WrappedComposition.g(Unknown Source:8)
如果我将异常抛出到
setContent
之外,我会得到一个可读的异常。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
throw InvalidClassException("Test Exception") // I get a proper exception stack trace
setContent {
LDAContent()
}
}
我尝试更新所有与撰写相关的依赖项,目前我使用:
agp = 8.2.0
kotlinCompilerExtensionVersion = "1.5.12"
kotlin = 1.9.23
但没有任何运气。 “修复”此问题的唯一方法是设置
isMinifyEnabled = false
。
这似乎是与 R8/ Proguard 相关的一些异常。
isMinifyEnabled
的官方文档:
为了使您的应用程序尽可能小和快,您应该使用 isMinifyEnabled = true 优化和缩小您的发布版本。
这样做可以实现收缩,从而删除未使用的代码和资源; 混淆,缩短应用程序的类和成员的名称;和优化,它应用更积极的策略来进一步减小应用程序的大小并提高应用程序的性能。
当您使用
isminifyEnabled = true
时,您的代码会被混淆,这意味着所有类、方法和字段名称都会被一些随机字母替换。从这个意义上讲,您发布的错误日志与预期完全一致。
当您在
throw
之前setContent
时,错误日志看起来很正常,因为程序在执行特定于您的应用程序的任何模糊代码之前崩溃,因此不会出现在堆栈跟踪中。 onCreate
方法本身不能被混淆,因为它覆盖了onCreate
中的android.app.Activity
。如果它被混淆,您的应用程序将不再工作。一般来说,重写Android库方法的方法不会被混淆。
要解码模糊的堆栈跟踪,请阅读文档中的此部分。