I have an Android monorepo that runs the app. With...
# multiplatform
e
I have an Android monorepo that runs the app. Within this monorepo, I have various modules, and one of them is a Kotlin Multiplatform module. I attempted to create a task to export everything within the
commonMain
directory into a library for iOS, apparently in the form of a
.framework
. However, when running the task, I receive a generalized Gradle error that doesn't provide much insight into what might be wrong. I would like to know if my configuration is correct for my purpose of creating a
.framework
to be imported by an external iOS application.
The script for it is as follows:
Copy code
plugins {
    id 'kotlin-multiplatform'
    id 'com.android.library'
    id 'kotlin-kapt'
    id 'org.jetbrains.kotlin.native.cocoapods'
}

android {
    compileSdkVersion Config.compileSdk

    defaultConfig {
        minSdkVersion Config.minSdk
        targetSdkVersion Config.targetSdk
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    }

    packagingOptions {
        resources {
            excludes += ['META-INF/LICENSE.md', 'META-INF/LICENSE-notice.md']
        }
    }
}

kotlin {
    android()
    iosArm64()
    iosX64()
    iosSimulatorArm64()

    cocoapods {
        version = "1.9.22"
    }

    sourceSets {
        commonMain {
            dependencies {
                // Add common dependencies here
            }
        }

        commonTest {
            dependencies {
                // Add common test dependencies here
            }
        }

        androidMain {
            dependencies {
                // Add Android main dependencies here
            }
        }

        androidTest {
            dependencies {
                // Add Android test dependencies here
            }
        }

        iosMain {
            dependencies {
                // Add iOS main dependencies here
            }
        }

        iosTest {
            dependsOn iosMain
        }
    }
}

task generateFramework {
    dependsOn 'cocoapodsInstall'
    doLast {
        // Destination path for the generated framework
        def targetFrameworkDir = "${project.buildDir}/kmmFramework"

        // If the path doesn't exist, create the directory
        file(targetFrameworkDir).mkdirs()

        // Your code to copy the generated framework to the desired destination
        copy {
            from "build/cocoapods" // Make sure this is the correct path for the generated framework
            into targetFrameworkDir
            include '**/*.framework'
            include '**/*.h'
        }
    }
}
Runing in terminal : sudo ./gradlew generateFramework
Captura de Tela 2024-01-19 às 14.07.58.png
p
I haven't used cocoapods plugin in quite some time but I see in my notes the task I used was named
podPublishXCFramework
Give it a try to see if it generates what you want.
e
Would you have an example? I tried adjusting it here, and the above error persists. Here's the updated script:
Copy code
plugins {
    id 'org.jetbrains.kotlin.multiplatform'
    id 'com.android.library'
    id 'org.jetbrains.kotlin.native.cocoapods'
}

android {
    compileSdkVersion Config.compileSdk

    defaultConfig {
        minSdkVersion Config.minSdk
        targetSdkVersion Config.targetSdk
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    }

    packagingOptions {
        resources {
            excludes += ['META-INF/LICENSE.md', 'META-INF/LICENSE-notice.md']
        }
    }
}

kotlin {
    android()
    ios {
        binaries {
            framework {
                baseName = "YourFrameworkName"
            }
        }
    }

    cocoapods {
        version = "1.11.2" // Versão desejada do CocoaPods
        summary = "Your framework summary"
        homepage = "<https://your-framework-homepage.com>"
    }
}

task generateFramework {
    dependsOn 'cocoapodsInstall'
    doLast {
        def targetFrameworkDir = "${project.buildDir}/kmmFramework"
        file(targetFrameworkDir).mkdirs()

        copy {
            from "build/cocoapods"
            into targetFrameworkDir
            include '**/*.framework'
            include '**/*.h'
        }

        tasks.podPublishXCFramework.dependsOn 'generateFramework'
    }
}
@Pablichjenkov
p
What error do you get? I have a project that uses cocoapods but I don't have that custom task generateFramework you have. Let me find the link so you can check the gradle files. When you open your project with AS under your module task you don't see the cocoapods tasks?
Try running it manually and see if it works then experiment calling it from gradle. Then ensure the logic in your custom task is ok
I actually think your task should run after not before the podPublishXCFramework task
e
cies of task 'paymentkmm:generateFramework'. [org.gradle.internal.buildevents.BuildExceptionReporter] > Task with path 'cocoapodsInstall' not found in project 'paymentkmm'.
Captura de Tela 2024-01-19 às 15.53.03.png
where to see cocoapods tasks in android studio? For me it wouldn't need to be a custom task, if I just generated a .framework to import and use on iOS everything I have in the commonMain folder would be enough.
p
Check this project, it uses cocoapods with compose-multiplatform. Pretty simple integration with not pods dependency. It should run in your environment, if it doesn’t, probably you pods installation is missing something. https://github.com/pablichjenkov/amadeus-hotel-app/blob/main/shared/build.gradle.kts In regards to AS gradle task, they should show up here:
If they don’t show up, then enable bellow checkbox under
Settings/Experimental
e
Copy code
This part does not exist in my kmm module. I'll try to see how to add it
p
Yes you need that. Ensure you have the above mentioned experimental setting checked in,
a
just briefly looked over your messages, do you have cocoapods installed and xcode? https://github.com/Kotlin/kdoctor Try kdoctor to check you have it all setup
k
e
Copy code
Thanks for your help, it's working correctly now
🎉 1