简体   繁体   中英

KMM & KTOR - iOS project with Framework library crashes when shared pod is imported in both targets

I have created a new KMM project, added only KTOR dependencies (following online guides step by step), and it's working perfectly on both Android and iOS devices.
For my purposes I'm using

  • KTOR 2.0.1 libraries
  • KMM plugin V. 0.3.3(212-1.7.0-RC-release-217-IJ)-104
  • Kotlin plugin V. 212-1.7.0-release-281-AS5457.46
  • Android Studio Chipmunk V. 2021.2.1

Then I have made a simple HttpClient in shared commonMain package like this

private val client = HttpClient {
        install(ContentNegotiation) {
            json(
                Json {
                    prettyPrint = true
                    isLenient = true
                    ignoreUnknownKeys = true
                }
            )
        }
        install(Logging) {
            logger = Logger.DEFAULT
            level = LogLevel.HEADERS
        }
    } 

Tried a simple post call, working.

Then I have created a Framework inside iOS project, adding basically a target -> Framework. The situation, on XCode is now this:

Xcode 项目目标列表

Everything is working properly, I can add files and classes inside my library and the project is compiling great. No crash when I make the call from inside main target. Now I can modify the pod file to import shared framework also in my framework library, and the pod install command shows no errors.

From now (shared pod installed on both targets), every POST or GET call (KTOR) makes my application crash, even if I don't work with shared files inside my Framework (Framework is empty, I have added shared pod only in pod file, but never imported).

This is the crash:

在 iOS 上崩溃

Any ideas to make KMM shared pod available on multiple targets on the same iOS project?

I've tried with KTOR old libraries, but crash is the same. The crash starts when I import shared pod in pod file on XCode project, and then I install it. If I delete the dependency in the pod file, everything will work perfectly.

EDIT: Full stack trace

iosApp`kfun:kotlin.native.internal.<no name provided>_1.invokeSuspend_4#internal:
    0x1003fd004 <+0>:   sub    sp, sp, #0x160
    0x1003fd008 <+4>:   stp    x28, x27, [sp, #0x140]
    0x1003fd00c <+8>:   stp    x29, x30, [sp, #0x150]
    0x1003fd010 <+12>:  add    x29, sp, #0x150
    0x1003fd014 <+16>:  stur   x2, [x29, #-0x90]
    0x1003fd018 <+20>:  stur   x1, [x29, #-0x88]
    0x1003fd01c <+24>:  stur   x0, [x29, #-0x80]
    0x1003fd020 <+28>:  movi.16b v0, #0x0
    0x1003fd024 <+32>:  stur   q0, [x29, #-0x20]
    0x1003fd028 <+36>:  stur   q0, [x29, #-0x30]
    0x1003fd02c <+40>:  stur   q0, [x29, #-0x40]
    0x1003fd030 <+44>:  stur   q0, [x29, #-0x50]
    0x1003fd034 <+48>:  stur   q0, [x29, #-0x60]
    0x1003fd038 <+52>:  stur   q0, [x29, #-0x70]
    0x1003fd03c <+56>:  sub    x8, x29, #0x70
    0x1003fd040 <+60>:  stur   x8, [x29, #-0x78]
    0x1003fd044 <+64>:  b      0x1003fd048               ; <+68> at ObjCExportCoroutines.kt
    0x1003fd048 <+68>:  ldur   x9, [x29, #-0x88]
    0x1003fd04c <+72>:  ldur   x11, [x29, #-0x80]
    0x1003fd050 <+76>:  ldur   x8, [x29, #-0x78]
    0x1003fd054 <+80>:  str    x8, [sp, #0x68]
    0x1003fd058 <+84>:  mov    x10, x8
    0x1003fd05c <+88>:  str    x11, [x10, #0x18]!
    0x1003fd060 <+92>:  str    x10, [sp, #0x70]
    0x1003fd064 <+96>:  str    x9, [x8, #0x20]!
    0x1003fd068 <+100>: str    x8, [sp, #0x78]
    0x1003fd06c <+104>: mov    x9, x8
    0x1003fd070 <+108>: str    x9, [sp, #0x80]
    0x1003fd074 <+112>: add    x0, x8, #0x8
    0x1003fd078 <+116>: str    x0, [sp, #0x88]
    0x1003fd07c <+120>: add    x0, x8, #0x10
    0x1003fd080 <+124>: str    x0, [sp, #0x90]
    0x1003fd084 <+128>: add    x9, x8, #0x18
    0x1003fd088 <+132>: str    x9, [sp, #0x98]
    0x1003fd08c <+136>: add    x0, x8, #0x20
    0x1003fd090 <+140>: str    x0, [sp, #0xa0]
    0x1003fd094 <+144>: add    x9, x8, #0x28
    0x1003fd098 <+148>: str    x9, [sp, #0xa8]
    0x1003fd09c <+152>: add    x0, x8, #0x30
    0x1003fd0a0 <+156>: stur   x0, [x29, #-0xa0]
    0x1003fd0a4 <+160>: add    x0, x8, #0x38
    0x1003fd0a8 <+164>: stur   x0, [x29, #-0x98]
    0x1003fd0ac <+168>: b      0x1003fd0b0               ; <+172> at ObjCExportCoroutines.kt
    0x1003fd0b0 <+172>: ldr    x0, [sp, #0x68]
    0x1003fd0b4 <+176>: mov    w1, #0x2
    0x1003fd0b8 <+180>: mov    w2, #0xc
    0x1003fd0bc <+184>: bl     0x100777a54               ; EnterFrame
    0x1003fd0c0 <+188>: bl     0x10077befc               ; Kotlin_mm_safePointFunctionPrologue
    0x1003fd0c4 <+192>: b      0x1003fd0c8               ; <+196> at ObjCExportCoroutines.kt
    0x1003fd0c8 <+196>: ldr    x8, [sp, #0x70]
    0x1003fd0cc <+200>: ldr    x0, [x8]
    0x1003fd0d0 <+204>: bl     0x1003fcf1c               ; kfun:kotlin.native.internal.<no name provided>_1.<get-invoked>_2#internal at ObjCExportCoroutines.kt
    0x1003fd0d4 <+208>: str    w0, [sp, #0x64]
    0x1003fd0d8 <+212>: b      0x1003fd0dc               ; <+216> at ObjCExportCoroutines.kt
    0x1003fd0dc <+216>: ldr    w8, [sp, #0x64]
    0x1003fd0e0 <+220>: tbnz   w8, #0x0, 0x1003fd1bc     ; <+440> at ObjCExportCoroutines.kt
    0x1003fd0e4 <+224>: b      0x1003fd0e8               ; <+228> at ObjCExportCoroutines.kt
    0x1003fd0e8 <+228>: ldr    x8, [sp, #0x70]
    0x1003fd0ec <+232>: ldr    x0, [x8]
    0x1003fd0f0 <+236>: mov    w1, #0x1
    0x1003fd0f4 <+240>: bl     0x1003fcf7c               ; kfun:kotlin.native.internal.<no name provided>_1.<set-invoked>_3#internal at ObjCExportCoroutines.kt
    0x1003fd0f8 <+244>: b      0x1003fd0fc               ; <+248> [inlined] contract at ContractBuilder.kt:151:2045
    0x1003fd0fc <+248>: b      0x1003fd100               ; <+252> [inlined] fold + 4 at ContractBuilder.kt
    0x1003fd100 <+252>: ldr    x1, [sp, #0x90]
    0x1003fd104 <+256>: ldr    x8, [sp, #0x80]
    0x1003fd108 <+260>: ldr    x0, [x8]
    0x1003fd10c <+264>: bl     0x1004caf64               ; kfun:kotlin.Result#exceptionOrNull(){}kotlin.Throwable? at Result.kt
    0x1003fd110 <+268>: str    x0, [sp, #0x58]
    0x1003fd114 <+272>: b      0x1003fd118               ; <+276> [inlined] fold + 28 at Result.kt
    0x1003fd118 <+276>: ldr    x0, [sp, #0x98]
    0x1003fd11c <+280>: ldr    x1, [sp, #0x58]
    0x1003fd120 <+284>: bl     0x100777940               ; UpdateStackRef
    0x1003fd124 <+288>: ldr    x8, [sp, #0x98]
    0x1003fd128 <+292>: ldr    x8, [x8]
    0x1003fd12c <+296>: cbnz   x8, 0x1003fd194           ; <+400> at ObjCExportCoroutines.kt
    0x1003fd130 <+300>: b      0x1003fd134               ; <+304> [inlined] <anonymous> + 56 at Result.kt
    0x1003fd134 <+304>: ldr    x1, [sp, #0xa0]
    0x1003fd138 <+308>: ldr    x8, [sp, #0x80]
    0x1003fd13c <+312>: ldr    x0, [x8]
    0x1003fd140 <+316>: bl     0x1004cae0c               ; kfun:kotlin.Result#<get-value>(){}kotlin.Any? at Result.kt
    0x1003fd144 <+320>: str    x0, [sp, #0x50]
    0x1003fd148 <+324>: b      0x1003fd14c               ; <+328> [inlined] <anonymous> + 80 at ObjCExportCoroutines.kt
    0x1003fd14c <+328>: ldr    x0, [sp, #0xa8]
    0x1003fd150 <+332>: ldr    x1, [sp, #0x50]
    0x1003fd154 <+336>: bl     0x100777940               ; UpdateStackRef
    0x1003fd158 <+340>: ldr    x9, [sp, #0x70]
    0x1003fd15c <+344>: ldr    x8, [sp, #0xa8]
    0x1003fd160 <+348>: ldr    x9, [x9]
    0x1003fd164 <+352>: ldr    x0, [x9, #0x20]
    0x1003fd168 <+356>: ldr    x1, [x8]
    0x1003fd16c <+360>: bl     0x10079aaf4               ; Kotlin_ObjCExport_runCompletionSuccess
->  0x1003fd170 <+364>: b      0x1003fd174               ; <+368> [inlined] <anonymous> + 20 at ObjCExportCoroutines.kt:70:4971
    0x1003fd174 <+368>: b      0x1003fd178               ; <+372> [inlined] <anonymous> + 24 at ObjCExportCoroutines.kt:21:14
    0x1003fd178 <+372>: b      0x1003fd17c               ; <+376> [inlined] fold + 28 at Result.kt:230:6
    0x1003fd17c <+376>: b      0x1003fd180               ; <+380> [inlined] <anonymous> + 132 at ObjCExportCoroutines.kt:70:8361
    0x1003fd180 <+380>: b      0x1003fd184               ; <+384> at ObjCExportCoroutines.kt
    0x1003fd184 <+384>: adrp   x0, 1043
    0x1003fd188 <+388>: add    x0, x0, #0x858            ; theUnitInstance
    0x1003fd18c <+392>: str    x0, [sp, #0x48]
    0x1003fd190 <+396>: b      0x1003fd1f4               ; <+496> at Preconditions.kt
    0x1003fd194 <+400>: ldr    x9, [sp, #0x98]
    0x1003fd198 <+404>: ldr    x8, [sp, #0x70]
    0x1003fd19c <+408>: ldr    x8, [x8]
    0x1003fd1a0 <+412>: ldr    x0, [x8, #0x20]
    0x1003fd1a4 <+416>: ldr    x1, [x9]
    0x1003fd1a8 <+420>: ldr    x2, [x8, #0x28]
    0x1003fd1ac <+424>: bl     0x10079abac               ; Kotlin_ObjCExport_runCompletionFailure
    0x1003fd1b0 <+428>: b      0x1003fd1b4               ; <+432> [inlined] <anonymous> + 24 at ObjCExportCoroutines.kt:70:5008
    0x1003fd1b4 <+432>: b      0x1003fd1b8               ; <+436> [inlined] <anonymous> + 28 at ObjCExportCoroutines.kt:24:14
    0x1003fd1b8 <+436>: b      0x1003fd17c               ; <+376> [inlined] fold + 28 at Result.kt:230:6
    0x1003fd1bc <+440>: ldr    x1, [sp, #0x88]
    0x1003fd1c0 <+444>: adrp   x0, 1042
    0x1003fd1c4 <+448>: add    x0, x0, #0x2e0            ; kclass:kotlin.IllegalStateException
    0x1003fd1c8 <+452>: bl     0x100776874               ; AllocInstance
    0x1003fd1cc <+456>: mov    x1, x0
    0x1003fd1d0 <+460>: str    x1, [sp, #0x40]
    0x1003fd1d4 <+464>: adrp   x1, 1200
    0x1003fd1d8 <+468>: add    x1, x1, #0xe20            ; __unnamed_70
    0x1003fd1dc <+472>: bl     0x1003d0c24               ; kfun:kotlin.IllegalStateException#<init>(kotlin.String?){} at Exceptions.kt
    0x1003fd1e0 <+476>: b      0x1003fd1e4               ; <+480> [inlined] error + 36 at Preconditions.kt
    0x1003fd1e4 <+480>: ldr    x0, [sp, #0x40]
    0x1003fd1e8 <+484>: bl     0x100794f84               ; ThrowException
    0x1003fd1ec <+488>: b      0x1003fd1f0               ; <+492> [inlined] error + 48 at Preconditions.kt:143:97
    0x1003fd1f0 <+492>: brk    #0x1
    0x1003fd1f4 <+496>: ldur   x0, [x29, #-0x90]
    0x1003fd1f8 <+500>: ldr    x1, [sp, #0x48]
    0x1003fd1fc <+504>: str    x1, [sp, #0x38]
    0x1003fd200 <+508>: bl     0x100776a5c               ; UpdateReturnRef
    0x1003fd204 <+512>: ldr    x0, [sp, #0x68]
    0x1003fd208 <+516>: mov    w1, #0x2
    0x1003fd20c <+520>: mov    w2, #0xc
    0x1003fd210 <+524>: bl     0x100777c20               ; LeaveFrame
    0x1003fd214 <+528>: ldr    x0, [sp, #0x38]
    0x1003fd218 <+532>: ldp    x29, x30, [sp, #0x150]
    0x1003fd21c <+536>: ldp    x28, x27, [sp, #0x140]
    0x1003fd220 <+540>: add    sp, sp, #0x160
    0x1003fd224 <+544>: ret    
    0x1003fd228 <+548>: mov    x2, x1
    0x1003fd22c <+552>: mov    x1, x0
    0x1003fd230 <+556>: ldr    x0, [sp, #0x68]
    0x1003fd234 <+560>: mov    x8, x2
    0x1003fd238 <+564>: str    w8, [sp, #0x34]
    0x1003fd23c <+568>: str    x1, [sp, #0x28]
    0x1003fd240 <+572>: bl     0x100777edc               ; SetCurrentFrame
    0x1003fd244 <+576>: ldr    w8, [sp, #0x34]
    0x1003fd248 <+580>: subs   w8, w8, #0x2
    0x1003fd24c <+584>: b.ne   0x1003fd27c               ; <+632> [inlined] <anonymous> + 72 at ObjCExportCoroutines.kt
    0x1003fd250 <+588>: b      0x1003fd254               ; <+592> [inlined] <anonymous> + 32 at ObjCExportCoroutines.kt
    0x1003fd254 <+592>: ldr    x0, [sp, #0x28]
    0x1003fd258 <+596>: bl     0x1007b1ea0               ; symbol stub for: __cxa_begin_catch
    0x1003fd25c <+600>: ldur   x1, [x29, #-0xa0]
    0x1003fd260 <+604>: bl     0x100795224               ; Kotlin_getExceptionObject
    0x1003fd264 <+608>: str    x0, [sp, #0x20]
    0x1003fd268 <+612>: bl     0x1007b1eac               ; symbol stub for: __cxa_end_catch
    0x1003fd26c <+616>: ldr    x0, [sp, #0x20]
    0x1003fd270 <+620>: bl     0x100794f84               ; ThrowException
    0x1003fd274 <+624>: b      0x1003fd278               ; <+628> [inlined] <anonymous> + 68 at ObjCExportCoroutines.kt:20:17
    0x1003fd278 <+628>: brk    #0x1
    0x1003fd27c <+632>: ldr    x0, [sp, #0x28]
    0x1003fd280 <+636>: bl     0x1007b1ea0               ; symbol stub for: __cxa_begin_catch
    0x1003fd284 <+640>: bl     0x1007b1e70               ; symbol stub for: std::terminate()
    0x1003fd288 <+644>: b      0x1003fd28c               ; <+648> [inlined] <anonymous> + 88 at ObjCExportCoroutines.kt:20:17
    0x1003fd28c <+648>: b      0x1003fd28c               ; <+648> [inlined] <anonymous> + 88 at ObjCExportCoroutines.kt:20:17
    0x1003fd290 <+652>: mov    x2, x1
    0x1003fd294 <+656>: mov    x1, x0
    0x1003fd298 <+660>: ldr    x0, [sp, #0x68]
    0x1003fd29c <+664>: mov    x8, x2
    0x1003fd2a0 <+668>: str    w8, [sp, #0x1c]
    0x1003fd2a4 <+672>: str    x1, [sp, #0x10]
    0x1003fd2a8 <+676>: bl     0x100777edc               ; SetCurrentFrame
    0x1003fd2ac <+680>: ldr    w8, [sp, #0x1c]
    0x1003fd2b0 <+684>: subs   w8, w8, #0x2
    0x1003fd2b4 <+688>: b.ne   0x1003fd2e4               ; <+736> [inlined] <anonymous> + 72 at ObjCExportCoroutines.kt
    0x1003fd2b8 <+692>: b      0x1003fd2bc               ; <+696> [inlined] <anonymous> + 32 at ObjCExportCoroutines.kt
    0x1003fd2bc <+696>: ldr    x0, [sp, #0x10]
    0x1003fd2c0 <+700>: bl     0x1007b1ea0               ; symbol stub for: __cxa_begin_catch
    0x1003fd2c4 <+704>: ldur   x1, [x29, #-0x98]
    0x1003fd2c8 <+708>: bl     0x100795224               ; Kotlin_getExceptionObject
    0x1003fd2cc <+712>: str    x0, [sp, #0x8]
    0x1003fd2d0 <+716>: bl     0x1007b1eac               ; symbol stub for: __cxa_end_catch
    0x1003fd2d4 <+720>: ldr    x0, [sp, #0x8]
    0x1003fd2d8 <+724>: bl     0x100794f84               ; ThrowException
    0x1003fd2dc <+728>: b      0x1003fd2e0               ; <+732> [inlined] <anonymous> + 68 at ObjCExportCoroutines.kt:23:17
    0x1003fd2e0 <+732>: brk    #0x1
    0x1003fd2e4 <+736>: ldr    x0, [sp, #0x10]
    0x1003fd2e8 <+740>: bl     0x1007b1ea0               ; symbol stub for: __cxa_begin_catch
    0x1003fd2ec <+744>: bl     0x1007b1e70               ; symbol stub for: std::terminate()
    0x1003fd2f0 <+748>: b      0x1003fd2f4               ; <+752> [inlined] <anonymous> + 88 at ObjCExportCoroutines.kt:23:17
    0x1003fd2f4 <+752>: b      0x1003fd2f4               ; <+752> [inlined] <anonymous> + 88 at ObjCExportCoroutines.kt:23:17

For future developers that may have same issue

The suggested solution is a workaround, because there is a difference between Kotlin and Cocoapods frameworks:

  • For Kotlin plugin frameworks are DYNAMIC by default.
  • For Cocoapods plugin frameworks are STATIC by default.

So the solution is to add this code into build.gradle file in shared module, thanks to JetBrains support:

cocoapods {
    ...
    framework {
        isStatic = false
        ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM