LeakCanary 2.8.1 アプリ起動時に NullPointerException

LeakCanary is a memory leak detection library for Android.
👉 LeakCanary 

アプリが起動できずにこんなの出ましたけど。


2022-02-28 21:18:29.578 16816-16816/com.benigumo.apn D/AndroidRuntime: Shutting down VM
2022-02-28 21:18:29.589 16816-16816/com.benigumo.apn E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.app, PID: 16816
    java.lang.NullPointerException
        at q5.c$c.b(:112)
        at q5.c$c.f(:154)
        at p5.f.d(:233)
        at c7.x$a.c(:46)
        at p5.b$a.a(:38)
        at c7.x$a.b(:43)
        at q5.b$b.a(:25)
        at q5.b$b.add(:23)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:393)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:133)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4847)
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54)
        at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2214)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7842)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

minifyEnabled false ではクラッシュしない。

回避方法

難読化されている部分は以下の模様。


Fatal Exception: java.lang.NullPointerException
curtains.internal.WindowCallbackWrapper$Companion.getJetpackWrapped (WindowCallbackWrapper.kt:112)
curtains.internal.WindowCallbackWrapper$Companion.unwrap (WindowCallbackWrapper.kt:154)
curtains.WindowsKt.getWrappedCallback (Windows.kt:233)
leakcanary.RootViewWatcher$listener$1.onRootViewAdded (RootViewWatcher.kt:46)
curtains.OnRootViewAddedListener$DefaultImpls.onRootViewsChanged (Listeners.kt:38)
leakcanary.RootViewWatcher$listener$1.onRootViewsChanged (RootViewWatcher.kt:43)
curtains.internal.RootViewsSpy$delegatingViewList$1.add (RootViewsSpy.kt:25)
curtains.internal.RootViewsSpy$delegatingViewList$1.add (RootViewsSpy.kt:23)
android.view.WindowManagerGlobal.addView (WindowManagerGlobal.java:454)
android.view.WindowManagerImpl.addView (WindowManagerImpl.java:121)
...

👉 Obfucation and 2.8.1 version cause an app launch crash · Issue #2286 · square/leakcanary 

書いてあるように、proguard rule に以下を追加で暫定回避できました。


-keep class androidx.appcompat.view.WindowCallbackWrapper { *; }
-keep class android.support.v7.view.WindowCallbackWrapper { *; }

次期バージョンで修正され、不要になるのでしょうが。

👉 Add Proguard rules for WindowCallbackWrapper by carlonzo · Pull Request #33 · square/curtains 

2022-04-08 追記

さらに更新されてる。


-keep class androidx.appcompat.view.WindowCallbackWrapper {
    android.view.Window$Callback mWrapped;
}

-keep class android.support.v7.view.WindowCallbackWrapper {
    android.view.Window$Callback mWrapped;
}

👉 just keep it · carlonzo/curtains@686b12c hatena-bookmark



関連ワード:  AndroidGradleKotlinアプリツール便利な設定開発