From 6cc8055cfdd43b712743a257e124b6f3f42f5ce9 Mon Sep 17 00:00:00 2001 From: Aleksey Mikhailov Date: Sat, 7 Dec 2024 15:50:22 +0700 Subject: [PATCH] #747 rollback to old search way but still failed --- gradle.properties | 1 - resources/build.gradle.kts | 17 --------- resources/src/appleMain/def/appleNative.def | 5 --- .../moko/resources/utils/MRBundleAnchor.kt | 35 ------------------- .../moko/resources/utils/NSBundleExt.kt | 19 ++++++---- .../appleMain/objective-c/MRResourcesBundle.h | 8 ----- .../appleMain/objective-c/MRResourcesBundle.m | 15 -------- 7 files changed, 13 insertions(+), 87 deletions(-) delete mode 100644 resources/src/appleMain/def/appleNative.def delete mode 100644 resources/src/appleMain/kotlin/dev/icerock/moko/resources/utils/MRBundleAnchor.kt delete mode 100644 resources/src/appleMain/objective-c/MRResourcesBundle.h delete mode 100644 resources/src/appleMain/objective-c/MRResourcesBundle.m diff --git a/gradle.properties b/gradle.properties index d149d3a9..fd97ac0e 100755 --- a/gradle.properties +++ b/gradle.properties @@ -8,7 +8,6 @@ kotlin.mpp.stability.nowarn=true kotlin.mpp.androidGradlePluginCompatibility.nowarn=true kotlin.mpp.androidSourceSetLayoutVersion=2 kotlin.mpp.applyDefaultHierarchyTemplate=false -kotlin.mpp.enableCInteropCommonization=true org.jetbrains.compose.experimental.jscanvas.enabled=true org.jetbrains.compose.experimental.uikit.enabled=true diff --git a/resources/build.gradle.kts b/resources/build.gradle.kts index 6f319173..ba13311f 100644 --- a/resources/build.gradle.kts +++ b/resources/build.gradle.kts @@ -2,9 +2,6 @@ * Copyright 2019 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. */ -import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation -import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget - plugins { id("multiplatform-library-extended-convention") id("multiplatform-android-publish-convention") @@ -28,20 +25,6 @@ kotlin { } } } - - // setup bundle searcher for apple -// targets -// .withType() -// .matching { it.konanTarget.family.isAppleFamily } -// .configureEach { -// compilations.getByName(KotlinCompilation.MAIN_COMPILATION_NAME) { -// val appleNative by cinterops.creating { -// defFile(project.file("src/appleMain/def/appleNative.def")) -// -// includeDirs("$projectDir/src/appleMain/objective-c") -// } -// } -// } } android { diff --git a/resources/src/appleMain/def/appleNative.def b/resources/src/appleMain/def/appleNative.def deleted file mode 100644 index 56f08a5c..00000000 --- a/resources/src/appleMain/def/appleNative.def +++ /dev/null @@ -1,5 +0,0 @@ -# https://kotlinlang.org/docs/native-definition-file.html -language = Objective-C -package = dev.icerock.moko.resources.apple.native -#staticLibraries = libMRResourcesBundle.a -headers = MRResourcesBundle.h diff --git a/resources/src/appleMain/kotlin/dev/icerock/moko/resources/utils/MRBundleAnchor.kt b/resources/src/appleMain/kotlin/dev/icerock/moko/resources/utils/MRBundleAnchor.kt deleted file mode 100644 index 3b0cd898..00000000 --- a/resources/src/appleMain/kotlin/dev/icerock/moko/resources/utils/MRBundleAnchor.kt +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2024 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. - */ - -package dev.icerock.moko.resources.utils - -import kotlinx.cinterop.BetaInteropApi -import kotlinx.cinterop.ExperimentalForeignApi -import kotlinx.cinterop.ExportObjCClass -import kotlinx.cinterop.ObjCClass -import kotlinx.cinterop.objcPtr -import platform.Foundation.NSBundle -import platform.Foundation.NSLog -import platform.darwin.NSObject - -// can't be object, because https://github.com/JetBrains/kotlin-native/issues/3855#issuecomment-586990632 -// in case of object we got error - java.lang.AssertionError: Assertion failed -@OptIn(BetaInteropApi::class) -@ExportObjCClass -class MRBundleAnchor : NSObject() { - - @OptIn(ExperimentalForeignApi::class) - fun getBundle(): NSBundle { -// return NSBundle.mainBundle - NSLog("MOKO now i will search class anchor from $this") - val anchor: ObjCClass = requireNotNull(this.`class`()) { - "can't get class of $this" - } - - NSLog("MOKO now i see anchor ${anchor.objcPtr()} and super is ${this.superclass().objcPtr()}") - return NSBundle.bundleForClass(anchor).also { - NSLog("MOKO i see bundle $it") - } - } -} diff --git a/resources/src/appleMain/kotlin/dev/icerock/moko/resources/utils/NSBundleExt.kt b/resources/src/appleMain/kotlin/dev/icerock/moko/resources/utils/NSBundleExt.kt index dcff4a60..398a17dd 100644 --- a/resources/src/appleMain/kotlin/dev/icerock/moko/resources/utils/NSBundleExt.kt +++ b/resources/src/appleMain/kotlin/dev/icerock/moko/resources/utils/NSBundleExt.kt @@ -12,11 +12,18 @@ import platform.Foundation.NSURL import platform.Foundation.pathExtension fun NSBundle.Companion.loadableBundle(identifier: String): NSBundle { - // we should use search by our class because dynamic framework with resources can be placed in - // external directory, not inside app directory (NSBundle.main). for example in case of - // SwiftUI preview - app directory empty, but dynamic framework with resources will be in - // different directory (DerivedData) - val bundlePath: String = MRBundleAnchor().getBundle().bundlePath + // at first we try to find required bundle inside Bundle.main, because it's faster way + // https://github.com/icerockdev/moko-resources/issues/708 + // but in some cases (for example in SwiftUI Previews) dynamic framework with bundles can be located + // in different location, not inside Bundle.main. So in this case we run less performant way - bundleWithIdentifier + // https://github.com/icerockdev/moko-resources/issues/747 + return findBundleInMain(identifier) + ?: NSBundle.bundleWithIdentifier(identifier) + ?: throw IllegalArgumentException("bundle with identifier $identifier not found") +} + +private fun findBundleInMain(identifier: String): NSBundle? { + val bundlePath: String = NSBundle.mainBundle.bundlePath val enumerator: NSDirectoryEnumerator = requireNotNull( NSFileManager.defaultManager.enumeratorAtPath(bundlePath) @@ -39,7 +46,7 @@ fun NSBundle.Companion.loadableBundle(identifier: String): NSBundle { } } - throw IllegalArgumentException("bundle with identifier $identifier not found") + return null } var isBundleSearchLogEnabled = false diff --git a/resources/src/appleMain/objective-c/MRResourcesBundle.h b/resources/src/appleMain/objective-c/MRResourcesBundle.h deleted file mode 100644 index b3afab7d..00000000 --- a/resources/src/appleMain/objective-c/MRResourcesBundle.h +++ /dev/null @@ -1,8 +0,0 @@ -#import -#import - -@interface ResourcesBundleAnchor : NSObject - -+ (NSBundle*) getResourcesBundle; - -@end diff --git a/resources/src/appleMain/objective-c/MRResourcesBundle.m b/resources/src/appleMain/objective-c/MRResourcesBundle.m deleted file mode 100644 index 690c146a..00000000 --- a/resources/src/appleMain/objective-c/MRResourcesBundle.m +++ /dev/null @@ -1,15 +0,0 @@ -// clang -target arm64-apple-ios -isysroot $(xcrun --sdk iphoneos --show-sdk-path) -c MRResourcesBundle.m -o source.o -// ar rcs libMRResourcesBundle.a source.o -// lipo -info libMRResourcesBundle.a - -#import -#import -#import "MRResourcesBundle.h" - -@implementation ResourcesBundleAnchor - -+ (NSBundle*) getResourcesBundle { - return [NSBundle bundleForClass:[ResourcesBundleAnchor class]]; -} - -@end