Skip to content

Commit

Permalink
Merge pull request #713 from icerockdev/#709-fix-custom-baseName
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex009 authored May 21, 2024
2 parents 29ac76e + 87f264f commit 6ed5168
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 40 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,20 @@ use `XCFramework` [from kotlin plugin](https://kotlinlang.org/docs/mpp-build-nat
But if you use **static frameworks** required additional setup - add to Xcode build phase (at end):

```bash
"$SRCROOT/../gradlew" -p "$SRCROOT/../" :shared:copyResourcesMPLReleaseXCFrameworkToApp \
"$SRCROOT/../gradlew" -p "$SRCROOT/../" :shared:copyResourcesMultiPlatformLibraryReleaseXCFrameworkToApp \
-Pmoko.resources.BUILT_PRODUCTS_DIR=$BUILT_PRODUCTS_DIR \
-Pmoko.resources.CONTENTS_FOLDER_PATH=$CONTENTS_FOLDER_PATH
```

and add in your build.gradle config:
```kotlin
multiplatformResources {
configureCopyXCFrameworkResources("MultiPlatformLibrary")
}
```

replace "MultiPlatformLibrary" with name that you use in `XCFramework` creation.

Details you can check in sample `samples/ios-static-xcframework`.

## Usage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import dev.icerock.gradle.generator.platform.android.setupAndroidTasks
import dev.icerock.gradle.generator.platform.android.setupAndroidVariantsSync
import dev.icerock.gradle.generator.platform.apple.registerCopyFrameworkResourcesToAppTask
import dev.icerock.gradle.generator.platform.apple.setupAppleKLibResources
import dev.icerock.gradle.generator.platform.apple.setupCopyXCFrameworkResourcesTask
import dev.icerock.gradle.generator.platform.apple.setupExecutableResources
import dev.icerock.gradle.generator.platform.apple.setupFatFrameworkTasks
import dev.icerock.gradle.generator.platform.apple.setupFrameworkResources
Expand Down Expand Up @@ -57,7 +56,6 @@ open class MultiplatformResourcesPlugin : Plugin<Project> {
kmpExtension = kmpExtension
)

setupCopyXCFrameworkResourcesTask(project = project)
setupFatFrameworkTasks(project = project)
registerGenerateAllResources(project = project)
registerCopyFrameworkResourcesToAppTask(project = project)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

package dev.icerock.gradle

import dev.icerock.gradle.generator.platform.apple.registerCopyXCFrameworkResourcesToAppTask
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project
import org.gradle.api.file.SourceDirectorySet
import org.gradle.api.provider.Property
import org.jetbrains.kotlin.gradle.plugin.mpp.NativeBuildType

@Suppress("UnnecessaryAbstractClass")
abstract class MultiplatformResourcesPluginExtension {
Expand All @@ -17,6 +19,14 @@ abstract class MultiplatformResourcesPluginExtension {
abstract val resourcesVisibility: Property<MRVisibility>
abstract val iosMinimalDeploymentTarget: Property<String>
abstract val resourcesSourceSets: NamedDomainObjectContainer<SourceDirectorySet>

fun Project.configureCopyXCFrameworkResources(xcFrameworkName: String = name) {
NativeBuildType.values()
.map { it.name.lowercase().capitalize() }
.plus("")
.map { xcFrameworkName.capitalize() + it + "XCFramework" }
.forEach { registerCopyXCFrameworkResourcesToAppTask(project = this, xcFrameworkName = it) }
}
}

internal fun MultiplatformResourcesPluginExtension.setupConvention(project: Project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.gradle.api.Action
import org.gradle.api.DomainObjectSet
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.file.Directory
import org.gradle.api.plugins.ExtensionAware
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.TaskProvider
Expand Down Expand Up @@ -95,34 +96,37 @@ internal fun createCopyFrameworkResourcesTask(framework: Framework) {
project.tasks.register(
/* name = */ taskName,
/* type = */ CopyFrameworkResourcesToAppTask::class.java
) {
it.configuration = framework.buildType.name
it.konanTarget = framework.compilation.konanTarget.name
it.frameworkPrefix = framework.nameWithoutBuildType
) { task ->
task.configuration = framework.buildType.name
task.konanTarget = framework.compilation.konanTarget.name
task.frameworkPrefix = framework.nameWithoutBuildType

it.frameworkIsStatic.set(
task.frameworkIsStatic.set(
project.provider {
framework.isStatic
}
)
it.inputFrameworkDirectory.set(
project.layout.projectDirectory.dir(framework.outputFile.absolutePath)
val projectDirectory: Directory = project.layout.projectDirectory
task.inputFrameworkDirectory.set(
framework.linkTaskProvider
.flatMap { it.outputFile }
.let { project.layout.dir(it) }
)
it.outputDirectory.set(
task.outputDirectory.set(
project.provider {
val buildProductsDir =
project.property("moko.resources.BUILT_PRODUCTS_DIR") as String
val contentsFolderPath =
project.property("moko.resources.CONTENTS_FOLDER_PATH") as String

val targetDir = File("$buildProductsDir/$contentsFolderPath")
val baseDir: File = project.layout.projectDirectory.asFile
val baseDir: File = projectDirectory.asFile

project.layout.projectDirectory.dir(targetDir.relativeTo(baseDir).path)
projectDirectory.dir(targetDir.relativeTo(baseDir).path)
}
)

it.dependsOn(framework.linkTaskProvider)
task.dependsOn(framework.linkTaskProvider)
}
}

Expand Down Expand Up @@ -188,37 +192,38 @@ internal fun registerCopyFrameworkResourcesToAppTask(
}
}

internal fun setupCopyXCFrameworkResourcesTask(project: Project) {
// Seems that there were problem with this block in the past with mystic task adding. Need more info
// Now, that works perfectly, I've tested on the real project with Kotlin 1.9.10 and KSP enabled
// Suppose that on that moment there were no lazy register method for task container
project.tasks.withType(XCFrameworkTask::class).all { task ->
val copyTaskName: String = task.name
.replace("assemble", "copyResources").plus("ToApp")
internal fun registerCopyXCFrameworkResourcesToAppTask(
project: Project,
xcFrameworkName: String
) {
val copyTaskName: String = "copyResources" + xcFrameworkName + "ToApp"

project.tasks.register<CopyXCFrameworkResourcesToApp>(copyTaskName) {
xcFrameworkDir.set(task.outputDir)
outputDir.set(
project.layout.dir(
project.provider {
val buildProductsDir =
project.property("moko.resources.BUILT_PRODUCTS_DIR") as String
val contentsFolderPath =
project.property("moko.resources.CONTENTS_FOLDER_PATH") as String
project.tasks.register<CopyXCFrameworkResourcesToApp>(copyTaskName) {
val xcFrameworkTask = this.project.tasks
.getByName("assemble$xcFrameworkName") as XCFrameworkTask

File("$buildProductsDir/$contentsFolderPath")
}
)
xcFrameworkDir.set(xcFrameworkTask.outputDir)
outputDir.set(
project.layout.dir(
project.provider {
val buildProductsDir =
project.property("moko.resources.BUILT_PRODUCTS_DIR") as String
val contentsFolderPath =
project.property("moko.resources.CONTENTS_FOLDER_PATH") as String

File("$buildProductsDir/$contentsFolderPath")
}
)
dependsOn(task)
}
)
dependsOn(xcFrameworkTask)
}
}

internal fun setupExecutableResources(target: KotlinNativeTarget) {
val project: Project = target.project
target.binaries.withType<AbstractExecutable>().configureEach { executable ->
val copyTaskName: String = executable.linkTaskProvider.name.replace("link", "copyResources")
val copyTaskName: String =
executable.linkTaskProvider.name.replace("link", "copyResources")

project.tasks.register<CopyExecutableResourcesToApp>(copyTaskName) {
dependsOn(executable.linkTaskProvider)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import UIKit
import SwiftUI
import shared
import SharedKit


struct ContentView: View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ kotlin {
ios.deploymentTarget = "15"
podfile = project.file("../iosApp/Podfile")
framework {
baseName = "shared"
baseName = "SharedKit"
binaryOptions["bundleId"] = "com.share.resources.module"

isStatic = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "cd ..\n\n./gradlew assembleMultiPlatformLibraryXCFramework\n";
shellScript = "cd ..\n\n./gradlew $GRADLE_TASK_ASSEMBLE_XCFRAMEWORK\n";
};
22BE0C76279C12A200CDA47A /* Copy MOKO Resources */ = {
isa = PBXShellScriptBuildPhase;
Expand All @@ -190,7 +190,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "cd ..\n\n./gradlew copyResourcesMultiPlatformLibraryReleaseXCFrameworkToApp \\\n -Pmoko.resources.BUILT_PRODUCTS_DIR=$BUILT_PRODUCTS_DIR \\\n -Pmoko.resources.CONTENTS_FOLDER_PATH=$CONTENTS_FOLDER_PATH\n";
shellScript = "cd ..\n\n./gradlew $GRADLE_TASK_COPY_RESOURCES_XCFRAMEWORK \\\n -Pmoko.resources.BUILT_PRODUCTS_DIR=$BUILT_PRODUCTS_DIR \\\n -Pmoko.resources.CONTENTS_FOLDER_PATH=$CONTENTS_FOLDER_PATH\n";
};
/* End PBXShellScriptBuildPhase section */

Expand Down Expand Up @@ -278,6 +278,8 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
GENERATE_INFOPLIST_FILE = YES;
GRADLE_TASK_ASSEMBLE_XCFRAMEWORK = ":mpp-library:assembleMultiPlatformLibraryDebugXCFramework";
GRADLE_TASK_COPY_RESOURCES_XCFRAMEWORK = ":mpp-library:copyResourcesMultiPlatformLibraryDebugXCFrameworkToApp";
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
Expand Down Expand Up @@ -355,6 +357,8 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
GENERATE_INFOPLIST_FILE = YES;
GRADLE_TASK_ASSEMBLE_XCFRAMEWORK = ":mpp-library:assembleMultiPlatformLibraryReleaseXCFramework";
GRADLE_TASK_COPY_RESOURCES_XCFRAMEWORK = ":mpp-library:copyResourcesMultiPlatformLibraryReleaseXCFrameworkToApp";
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
Expand Down Expand Up @@ -433,6 +437,8 @@
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
GENERATE_INFOPLIST_FILE = YES;
GRADLE_TASK_ASSEMBLE_XCFRAMEWORK = ":mpp-library:assembleMultiPlatformLibraryReleaseXCFramework";
GRADLE_TASK_COPY_RESOURCES_XCFRAMEWORK = ":mpp-library:copyResourcesMultiPlatformLibraryReleaseXCFrameworkToApp";
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
Expand Down
2 changes: 2 additions & 0 deletions samples/ios-static-xcframework/mpp-library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ dependencies {

multiplatformResources {
resourcesPackage.set("com.icerockdev.library")

configureCopyXCFrameworkResources("MultiPlatformLibrary")
}

0 comments on commit 6ed5168

Please sign in to comment.