1. 程式人生 > >Kotlin 並沒有想的那麼好

Kotlin 並沒有想的那麼好

kotlin 越來越火,但使用的越多發現不順手的地方也越多。

最近發現了崩潰:

11-17 00:54:07.355 266-266/? A/DEBUG: signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
11-17 00:54:07.359 266-266/? A/DEBUG: Abort message: 'art/runtime/art_method.cc:219] Failed to find Dex offset for PC offset 0xa4e(PC 0xe032e5ba, entry_point=0xe032db6c current entry_point=0xe032db6c) in java.lang.String com.adikia.Origin.print(java.lang.String)'
        eax 00000000  ebx 00003578  ecx 00003578  edx 00000006
        esi f7795c50  edi 00000000
        xcs 00000023  xds 0000002b  xes 0000002b  xfs 00000007  xss 0000002b
        eip f73406c6  ebp 00003578  esp ff8b85f0  flags 00200206
11-17 00:54:07.361 266-266/? A/DEBUG: backtrace:
        #00 pc 000836c6  /system/lib/libc.so (tgkill+22)
        #01 pc 00081728  /system/lib/libc.so (pthread_kill+70)
        #02 pc 00027255  /system/lib/libc.so (raise+36)
        #03 pc 00020a34  /system/lib/libc.so (abort+80)
        #04 pc 0051b2bb  /system/lib/libart.so (art::Runtime::Abort()+377)
        #05 pc 00151759  /system/lib/libart.so (art::LogMessage::~LogMessage()+1383)
        #06 pc 0014bc66  /system/lib/libart.so (art::Barrier::~Barrier()+966)
        #07 pc 00564aff  /system/lib/libart.so (art::ThreadList::Dump(std::__1::basic_ostream<char, std::__1::char_traits<char> >&)+271)
        #08 pc 0051b438  /system/lib/libart.so (art::Runtime::Abort()+758)
        #09 pc 00151759  /system/lib/libart.so (art::LogMessage::~LogMessage()+1383)
        #10 pc 001484f6  /system/lib/libart.so (art::ArtMethod::ToDexPc(unsigned int, bool)+2662)
        #11 pc 0052b6ca  /system/lib/libart.so (art::StackVisitor::GetDexPc(bool) const+68)
        #12 pc 00541a01  /system/lib/libart.so (art::BuildInternalStackTraceVisitor<false>::VisitFrame()+131)
        #13 pc 0052eef3  /system/lib/libart.so (art::StackVisitor::WalkStack(bool)+243)
        #14 pc 0055277b  /system/lib/libart.so (_jobject* art::Thread::CreateInternalStackTrace<false>(art::ScopedObjectAccessAlreadyRunnable const&) const+329)
        #15 pc 00483da1  /system/lib/libart.so (art::Throwable_nativeFillInStackTrace(_JNIEnv*, _jclass*)+52)
        #16 pc 730496be  /data/dalvik-cache/x86/
[email protected]
@boot.oat (offset 0x1ef8000)

這是什麼鬼,完全看不到原因,後來發現這是由書寫不規範而與專案本身的一系列連鎖反應引起的。

首先我寫了一個java的類:

public class CallbackTest {

    public Object callback(){
        return null;
    }
}

然後再kotlin中複寫呼叫:

             object :CallbackTest(){
                 override fun callback(): Any {
                     Log.v("XPC","origin_method");
                     return super.callback()
                 }
             }.callback()

這樣就會報下面的錯誤

11-17 02:36:01.446 18132-18132/com.adikia E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.adikia, PID: 18132
    java.lang.IllegalStateException: super.callback() must not be null
        at com.adikia.MainActivity$onClick$2.callback(MainActivity.kt:49)
        at com.adikia.MainActivity.onClick(MainActivity.kt:51)
        at android.view.View.performClick(View.java:5198)
        at android.view.View$PerformClick.run(View.java:21147)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:107)

其實這個錯誤很簡單,就是返回值不能是null,如果是null我們需要指定:Any?,下面的寫法是正確的

             object :CallbackTest(){
                 override fun callback(): Any? {
                     return super.callback()
                 }
             }.callback()

但問題就在於自動生成過載方法的時候,並沒有自動加?這就導致類經常會忘記新增,而真正的專案中情況複雜,一旦發生連鎖反應,將會很難排查。