icerockdev / moko-resources

Resources access for mobile (android & ios) Kotlin Multiplatform development
https://moko.icerock.dev/
Apache License 2.0
1.07k stars 120 forks source link

NullPointerException in NSBundle.Companion.loadableBundle() #99

Closed hardysim closed 4 years ago

hardysim commented 4 years ago

I'm getting a crash on iOS when trying to show a string. Following the README and calling getMyString() works fine on android but crashes on iOS with the following stacktrace:

Instances of kotlin.Error, kotlin.RuntimeException and subclasses aren't propagated from Kotlin to Objective-C/Swift.
Other exceptions can be propagated as NSError if method has or inherits @Throws annotation.
Uncaught Kotlin exception: kotlin.NullPointerException
        at 0   iosApp                              0x0000000105cc7ebe kfun:kotlin.Throwable.<init>()kotlin.Throwable + 62
        at 1   iosApp                              0x0000000105cbaa37 kfun:kotlin.Exception.<init>()kotlin.Exception + 55
        at 2   iosApp                              0x0000000105cbac57 kfun:kotlin.RuntimeException.<init>()kotlin.RuntimeException + 55
        at 3   iosApp                              0x0000000105cbae77 kfun:kotlin.NullPointerException.<init>()kotlin.NullPointerException + 55
        at 4   iosApp                              0x0000000105d3d277 ThrowNullPointerException + 119
        at 5   iosApp                              0x0000000106bbc897 kfun:dev.icerock.moko.resources.utils.loadableBundle@platform.Foundation.NSBundle.Companion.(kotlin.String)platform.Foundation.NSBundle + 4951
        at 6   iosApp                              0x000000010597c252 kfun:com.example.demo.MR.<init>$lambda-0#internal + 162
        at 7   iosApp                              0x000000010597c374 kfun:com.example.demo.MR.$<init>$lambda-0$FUNCTION_REFERENCE$1.invoke#internal + 132
        at 8   iosApp                              0x0000000105d13adc kfun:kotlin.native.concurrent.FreezeAwareLazyImpl.getOrInit#internal + 1004
        at 9   iosApp                              0x0000000105d14209 kfun:kotlin.native.concurrent.FreezeAwareLazyImpl.<get-value>()T + 393
        at 10  iosApp                              0x000000010597b493 kfun:com.example.demo.MR.<get-bundle>#internal + 323
        at 11  iosApp                              0x000000010597b63c kfun:com.example.demo.MR.strings.<init>()com.example.demo.MR.strings + 268
        at 12  iosApp                              0x00000001060eece5 InitSharedInstanceStrict + 197
        at 13  iosApp                              0x0000000105981851 kfun:com.example.demo.Test.getMyString()dev.icerock.moko.resources.desc.StringDesc + 353
        at 14  iosApp                              0x000000010599ce2b objc2kotlin.61 + 203
        at 15  iosApp                              0x000000010575333a $s6iosApp24DemoViewControllerC16prepareFabButton33_A24D38258ACBBC184B5DA3890509A050LLyyF + 58 (/Users/AppTeam/Desktop/projects/multiplatform/demo/iosApp/iosApp/ViewController/TabViewController/DemoViewController.swift:84:9)
        at 16  iosApp                              0x0000000105753ef4 $s6iosApp24DemoViewControllerC27viewSafeAreaInsetsDidChangeyyF + 180 (/Users/AppTeam/Desktop/projects/multiplatform/demo/iosApp/iosApp/ViewController/TabViewController/DemoViewController.swift:118:14)
        at 17  iosApp                              0x0000000105753f6b $s6iosApp24DemoViewControllerC27viewSafeAreaInsetsDidChangeyyFTo + 43 (/Users/AppTeam/Desktop/projects/multiplatform/demo/iosApp/<compiler-generated>:<unknown>)
        at 18  UIKitCore                           0x00007fff497ffed1 -[UIView _safeAreaInsetsDidChangeFromOldInsets:] + 64
        at 19  UIKitCore                           0x00007fff4980010b -[UIView _setSafeAreaInsets:updateSubviewsDuringNextLayoutPass:] + 486
        at 20  UIKitCore                           0x00007fff48c6e8a7 -[UIViewController _updateViewSafeAreaInsetsAndEagerlyUpdateContentScrollView:] + 124
        at 21  UIKitCore                           0x00007fff48c6f0f6 -[UIViewController _setContentOverlayInsets:andLeftMargin:rightMargin:] + 917
        at 22  UIKitCore                           0x00007fff48c71125 __121-[UIViewController __updateContentOverlayInsetsWithOurRect:inBoundsOfAncestorViewController:viaImmediateChildOfAncestor:]_block_invoke + 467
        at 23  UIKitCore                           0x00007fff48c70f0a -[UIViewController __updateContentOverlayInsetsWithOurRect:inBoundsOfAncestorViewController:viaImmediateChildOfAncestor:] + 3760
        at 24  UIKitCore                           0x00007fff48c6fd3e -[UIViewController _updateContentOverlayInsetsFromParentIfNecessary] + 658
        at 25  UIKitCore                           0x00007fff48c6f43c -[UIViewController _updateContentOverlayInsetsForSelfAndChildren] + 85
        at 26  UIKitCore                           0x00007fff48c7d248 -[UIViewController _updateLayoutForStatusBarAndInterfaceOrientation] + 863
        at 27  UIKitCore                           0x00007fff48c8220c -[UIViewController viewDidMoveToWindow:shouldAppearOrDisappear:] + 218
        at 28  UIKitCore                           0x00007fff49823c1a -[UIView(Internal) _didMoveFromWindow:toWindow:] + 1410
        at 29  UIKitCore                           0x00007fff49823968 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 720

I've added CFBundleLocalizations to the plist-file so I'm not sure whats wrong. The sample is working as well as a clean project but not my existing one where I need this.

hardysim commented 4 years ago

It seems to work when I do not use cocoapods but export a framework to use the shared/kotlin-code in swift (using packForXCode from the kotlin-mpp examples).

Is this a bug? Should this library work when the shared code is exported as a cocoapod?

Alex009 commented 4 years ago

hello! it's strange that usage of cocoapods have some changes. we use in our projects integration by cocoapods. but not official kotlin plugin - we create own podspec file and it use sync task from https://github.com/icerockdev/mobile-multiplatform-gradle-plugin same configuration used in sample of moko-resources.

error said what bundle with resources not found in application at runtime. reason of it now i don't know. i hope this sample project https://github.com/icerockdev/moko-resources/issues/97#issuecomment-676097851 will help me to debug this problem.

fix will be available in 0.12.0 or 0.12.1 versions, with kotlin 1.4.0 support

hardysim commented 4 years ago

The error is gone in 0.11.1 but a new one arises:

Uncaught Kotlin exception: kotlin.IllegalArgumentException: bundle with identifier com.example.demo.MR not found

Instances of kotlin.Error, kotlin.RuntimeException and subclasses aren't propagated from Kotlin to Objective-C/Swift.
Other exceptions can be propagated as NSError if method has or inherits @Throws annotation.
Uncaught Kotlin exception: kotlin.IllegalArgumentException: bundle with identifier com.example.demo.MR not found
        at 0   iosApp                              0x000000010726ca87 kfun:kotlin.Throwable.<init>(kotlin.String?)kotlin.Throwable + 87
        at 1   iosApp                              0x000000010725f855 kfun:kotlin.Exception.<init>(kotlin.String?)kotlin.Exception + 85
        at 2   iosApp                              0x000000010725fa75 kfun:kotlin.RuntimeException.<init>(kotlin.String?)kotlin.RuntimeException + 85
        at 3   iosApp                              0x000000010725fe95 kfun:kotlin.IllegalArgumentException.<init>(kotlin.String?)kotlin.IllegalArgumentException + 85
        at 4   iosApp                              0x000000010816149a kfun:dev.icerock.moko.resources.utils.loadableBundle@platform.Foundation.NSBundle.Companion.(kotlin.String)platform.Foundation.NSBundle + 4474
        at 5   iosApp                              0x0000000106f20d32 kfun:com.example.demo.MR.<init>$lambda-0#internal + 162
        at 6   iosApp                              0x0000000106f20e54 kfun:com.example.demo.MR.$<init>$lambda-0$FUNCTION_REFERENCE$1.invoke#internal + 132
        at 7   iosApp                              0x00000001072b887c kfun:kotlin.native.concurrent.FreezeAwareLazyImpl.getOrInit#internal + 1004
        at 8   iosApp                              0x00000001072b8fa9 kfun:kotlin.native.concurrent.FreezeAwareLazyImpl.<get-value>()T + 393
        at 9   iosApp                              0x0000000106f1ff73 kfun:com.example.demo.MR.<get-bundle>#internal + 323
        at 10  iosApp                              0x0000000106f2011c kfun:com.example.demo.MR.strings.<init>()com.example.demo.MR.strings + 268
        at 11  iosApp                              0x0000000107693a85 InitSharedInstanceStrict + 197
        at 12  iosApp                              0x0000000106f3381e kfun:com.example.demo.feature.arcadelist.ArcadeListViewModel.<init>(com.example.demo.feature.arcadelist.LoadArcadesUseCase;com.example.demo.feature.arcadelist.GetArcadeUseCase;com.example.demo.feature.arcadelist.RemoveArcadeUseCase)com.example.demo.feature.arcadelist.ArcadeListViewModel + 1342
        at 13  iosApp                              0x0000000106f31993 kfun:com.example.demo.di.InjectorUtils.provideArcadeListViewModel(com.example.demo.repository.Repository)com.example.demo.feature.arcadelist.ArcadeListViewModel + 691
        at 14  iosApp                              0x0000000106f44d13 objc2kotlin.115 + 275
        at 15  iosApp                              0x0000000106b59b8c $s6iosApp24ArcadeListViewControllerC7nibName6bundleACSSSg_So8NSBundleCSgtcfcTo + 172 (/Users/AppTeam/Desktop/projects/multiplatform/arcade/iosApp/<compiler-generated>:<unknown>)
        at 16  iosApp                              0x0000000106b5bcd3 $sSo16UIViewControllerCABycfcTO + 19 (/Users/AppTeam/Desktop/projects/multiplatform/arcade/iosApp/<compiler-generated>:<unknown>)
        at 17  iosApp                              0x0000000106b5bc6f $sSo16UIViewControllerCABycfC + 31 (/Users/AppTeam/Desktop/projects/multiplatform/arcade/iosApp/<compiler-generated>:<unknown>)
        at 18  iosApp                              0x0000000106b5b7b7 $s6iosApp0B8DelegateC11application_29didFinishLaunchingWithOptionsSbSo13UIApplicationC_SDySo0j6LaunchI3KeyaypGSgtF + 183 (/Users/AppTeam/Desktop/projects/multiplatform/arcade/iosApp/<compiler-generated>:<unknown>)
        at 19  iosApp                              0x0000000106b5be23 $s6iosApp0B8DelegateC11application_29didFinishLaunchingWithOptionsSbSo13UIApplicationC_SDySo0j6LaunchI3KeyaypGSgtFTo + 211 (/Users/AppTeam/Desktop/projects/multiplatform/arcade/iosApp/<compiler-generated>:<unknown>)
        at 20  UIKitCore                           0x00007fff4931c614 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 232
        at 21  UIKitCore                           0x00007fff4931dfb3 -[UIApplication _callInitializationDelegatesWithActions:forCanvas:payload:fromOriginatingProcess:] + 3985
        at 22  UIKitCore                           0x00007fff49323b75 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1226
        at 23  UIKitCore                           0x00007fff48a2c1a6 -[_UISceneLifecycleMultiplexer completeApplicationLaunchWithFBSScene:transitionContext:] + 179
        at 24  UIKitCore                           0x00007fff49320063 -[UIApplication _compellApplicationLaunchToCompleteUnconditionally] + 59
        at 25  UIKitCore                           0x00007fff49320362 -[UIApplication _run] + 754
        at 26  UIKitCore                           0x00007fff49325968 UIApplicationMain + 1605
        at 27  iosApp                              0x0000000106b5cd1b main + 75 (/Users/AppTeam/Desktop/projects/multiplatform/arcade/iosApp/iosApp/Navigation/AppDelegate/AppDelegate.swift:13:7)
        at 28  libdyld.dylib                       0x00007fff520ce1fd start + 1
        at 29  ???                                 0x0000000000000001 0x0 + 1
(lldb) 

I've ran all gradle tasks to generate MR (generateMRandroidMain, generateMRcommonMain, generateMRiosArm64Main, generateMRiosX64Main) and it still works on android.

Is this a followup or should I create a new issue?

Alex009 commented 4 years ago

@hardysim hi! thanks for you feedback. please help with fix - can you give me project reproducer with problem? something in configuration of your project is reason of bug, i need to debug it

dalewking commented 4 years ago

Here is a minimal project that demonstrates the bug: https://github.com/dalewking/moko_bug

Just run ./gradlew iosTest and you will get a test failure:

kotlin.IllegalArgumentException: bundle with identifier org.example.library.MR not found

hardysim commented 4 years ago

@dalewking your project does not use cocoapods, right? I only geht this error when using cocoapods. Strange that you get the same error...

dalewking commented 4 years ago

All I did was use IntelliJ to generate a MPP project and added Moko Resources and a dummy test to it to get tests to actually run. Our actual project uses cocoapods and gets the same error.

Alex009 commented 4 years ago

@hardysim reproducer given @dalewking is only for tests (output after compilation is kexe, not framework, so resources bundle not provided). can you give me reproducer of your case?

dalewking commented 4 years ago

@hardysim reproducer given @dalewking is only for tests (output after compilation is kexe, not framework, so resources bundle not provided). can you give me reproducer of your case?

Even though it is tests it still needs to work

Alex009 commented 4 years ago

@hardysim in 0.12.0 release you can use cocoapods plugin, but you should add some script in Xcode project. Please read new readme and try new version.

ln-12 commented 3 years ago

@Alex009 After playing around a bit, I am stuck with the XCode config. I am using this example with their cocoapods plugin fork and added the build phase mentioned here to my project. Unfortunately, XCode outputs the error Task 'copyFrameworkResourcesToApp' not found in project ':shared'.. Could you give me a hint what I am missing?

ln-12 commented 3 years ago

I also looked at the samples and added this block, but I didn't find what I am missing.

Alex009 commented 3 years ago

@ln-12 maybe you can share project? i will see and try fix

ln-12 commented 3 years ago

@Alex009 Thanks for your offer! I uploaded a minimal project here which I quickly copied together to get the same error. The two commit show the different errors I get before and after adding the build phase script to XCode. Now I'm stuck with Task 'copyFrameworkResourcesToApp' not found in project ':shared'..

Alex009 commented 3 years ago

@ln-12 hi! in your case plugins apply is order-dependend. i fix your setup here - https://github.com/ln-12/moko-resources-issue-99/pull/1 reason of unexist task is that co.touchlab.native.cocoapods change isStatic field of Framework in configuration time...but our plugin can't observe this action (and i not think that it is real)

ln-12 commented 3 years ago

Oh no, I am feeling really stupid now for such a mistake. Thanks a thousand times four your help!!

PhilipDukhov commented 3 years ago

Hi @Alex009, I'm facing same crash (IllegalArgumentException: bundle with identifier com.well.myapplication.MR not found). My sample project: https://github.com/PhilipDukhov/testCocoapodsKMM/tree/moko-resources-test I'm using cocoapods plugin, and according to the docs, I need to pass extra properties to the script call, but how can I do it, if podspec file gets regenerated after each gradle sync? Also adding those manually didn't helped with this issue.

Alex009 commented 3 years ago

@PhilipDukhov you should not edit generated podspec. you should edit your xcode project settings. just like in sample at target TestHierarhical: image