代码之家  ›  专栏  ›  技术社区  ›  Hector

如何识别W/System的根本原因:资源无法调用关闭。在Android上

  •  0
  • Hector  · 技术社区  · 3 年前

    我当前的android应用程序定期报告此logcat消息

    W/System: A resource failed to call close. 
    

    在寻找如何确定根本原因后,我尝试了以下方法,但都没有奏效。。。

    StrictMode.setVmPolicy(
                    VmPolicy.Builder()
                        .detectLeakedClosableObjects()
                        .penaltyDeath()
                        .build()
                )
    

    这个

    StrictMode.setVmPolicy(
                    VmPolicy.Builder()
                        .detectAll()
                        .penaltyDeath()
                        .build()
                )
    

    try {
                Class.forName("dalvik.system.CloseGuard")
                    .getMethod("setEnabled", Boolean::class.javaPrimitiveType)
                    .invoke(null, true)
            } catch (e: ReflectiveOperationException) {
                throw java.lang.RuntimeException(e)
            }
    

    我的应用程序针对这些版本的android

     compileSdkVersion 32
        buildToolsVersion "32.0.0"
    
        defaultConfig {
            applicationId "com.my.app"
            minSdkVersion 26
            targetSdkVersion 32
            versionCode 1
            versionName "1.0"
    
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
    

    我哪里错了?

    如何发现哪个资源没有调用close?

    0 回复  |  直到 3 年前
        1
  •  0
  •   Don David    2 年前

    我收到了同样的信息。我查看了在主活动中声明的资源,并猜测是ListAdapter。所以我在Destroy上清除了()。 消息消失了。

    对不起,我没有更好的答案。