(KotlinSourceSet with name 'androidMain' not found.) How can I add android as a build target to a Kotlin Multiplatform project - kotlin

I am trying to add android() as a build target to a Kotlin Multiplatform library so that I can add a specific library for the android target. All the other targets (jvm, linux, ios) work fine, but android seems to have issues, because the KotlinSourceSet is not being create like the others.
That's where I get the error:
KotlinSourceSet with name 'androidMain' not found.
I wonder, do I have to add the SourceSet manually? Am I missing some crucial build step?
Here is my gradle.build.kts
plugins {
id("maven-publish")
kotlin("multiplatform") version "1.8.0"
}
buildscript {
repositories {
google()
}
dependencies {
classpath ("com.android.tools.build:gradle:4.2.2")
classpath ("org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.0")
}
}
group = "zzz.xxxx"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
kotlin {
jvm()
linuxX64("linux")
ios()
android()
sourceSets {
val commonMain by getting {
dependencies {
implementation("...")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
val jvmMain by getting {
dependencies {
implementation("...")
}
}
val androidMain by getting {
dependencies {
implementation ("...")
}
}
val jvmTest by getting
}
}
I tried adding a androidMain folder, but it is not getting recognized as a KotlinSourceSet. Non of the resources online seem to help either.

To add android as a build target your need to setup android-gradle-plugin first.
With example setting of android plugin gradle.build.kts will be:
plugins {
id("maven-publish")
kotlin("multiplatform") version "1.8.0"
id("com.android.library") version "7.3.0" // (1) add plugin
}
//...
// (2) setup plugin
android {
compileSdk = 33
defaultConfig {
minSdk = 24
multiDexEnabled = true
}
sourceSets {
getByName("main") {
manifest.srcFile("src/androidMain/AndroidManifest.xml")
}
}
}
UPDATE
Before you are adding Android Gradle plugin:
Check that Google Maven repository is listed in your settings.gradle(.kts) file
Check the Android Gradle plugin is added to the project build.gradle(.kts) file
Check the Android Gradle plugin is compatible with your Gradle version

Related

Kotlin NoSuchMethodError after installing a library that enables a plugin

I want to experience a library called arrow analysis
My build.gradle.kts file looks as follows:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.7.10"
}
group = "org.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
testImplementation(kotlin("test"))
implementation("io.arrow-kt:arrow-core:1.1.2")
}
buildscript {
dependencies {
classpath("io.arrow-kt.analysis.kotlin:io.arrow-kt.analysis.kotlin.gradle.plugin:2.0")
}
}
apply(plugin = "io.arrow-kt.analysis.kotlin")
tasks.test {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
after an attempt to build the project, I get the following error:
java.lang.NoSuchMethodError: 'org.jetbrains.kotlin.psi.KtExpression org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilKt.getReceiverExpression(org.jetbrains.kotlin.resolve.calls.model.ResolvedCall)'
at arrow.meta.plugins.analysis.phases.analysis.solver.ast.kotlin.KotlinResolvedCall.getReceiverExpression(KotlinResolvedCall.kt:37)
at arrow.meta.plugins.analysis.phases.analysis.solver.ResolvedCallUtilsKt.allArgumentExpressions(ResolvedCallUtils.kt:75)
at arrow.meta.plugins.analysis.phases.analysis.solver.ResolvedCallUtilsKt.arg(ResolvedCallUtils.kt:119)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt.controlFlowAnyFunction(Expressions.kt:438)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt.checkCallExpression(Expressions.kt:397)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt.fallThrough(Expressions.kt:260)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt.access$fallThrough(Expressions.kt:1)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt$checkExpressionConstraints$2.invoke(Expressions.kt:244)
at arrow.meta.plugins.analysis.phases.analysis.solver.check.ExpressionsKt$checkExpressionConstraints$2.invoke(Expressions.kt:157)
at arrow.meta.continuations.ContSeq$flatMap$1.invokeSuspend(ContSeq.kt:60)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
I checked the source code, and everything seems to be in place. Did I something wrong as per the instructions to install this library which is denoted as follows:
buildscript {
dependencies {
classpath("io.arrow-kt.analysis.kotlin:io.arrow-kt.analysis.kotlin.gradle.plugin:2.0")
}
}
apply(plugin = "io.arrow-kt.analysis.kotlin")
You need to apply the plugin inside the plugins block and in the same fashion you're applying the Kotlin JVM plugin.
They mention to use it like this on their official docs
plugins {
kotlin("multiplatform") version "1.6.21"
// other plugins
id("io.arrow-kt.analysis.kotlin") version "2.0.2"
}
buildscript {
repositories {
mavenCentral()
}
}

Kotlin Multiplatform Mobile: Project already has a CocoaPods dependency with name SDWebImage

I have created a demo project to integrate cocoapods into the KMM project. I have followed this link from the official website. At step 3 while reimporting the project, I am receiving the following error.
Project already has a CocoaPods dependency with name SDWebImage , after which I am unable to even import this lib in KMM.
Can anyone please help with this?
Update: Adding build.gradle.kts
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
kotlin("multiplatform")
id("com.android.library")
kotlin("native.cocoapods")
}
// CocoaPods requires the podspec to have a version.
version = "1.0"
kotlin {
android()
ios {
cocoapods {
// Configure fields required by CocoaPods.
summary = "Some description for a Kotlin/Native module"
homepage = "Link to a Kotlin/Native module homepage"
pod("SDWebImage")
// You can change the name of the produced framework.
// By default, it is the name of the Gradle project.
frameworkName = "shared"
}
}
sourceSets {
val commonMain by getting
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting {
dependencies {
implementation("com.google.android.material:material:1.2.1")
}
}
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13")
}
}
val iosMain by getting
val iosTest by getting
}
}
android {
compileSdkVersion(29)
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdkVersion(24)
targetSdkVersion(29)
}
}
val packForXcode by tasks.creating(Sync::class) {
group = "build"
val mode = System.getenv("CONFIGURATION") ?: "DEBUG"
val sdkName = System.getenv("SDK_NAME") ?: "iphonesimulator"
val targetName = "ios" + if (sdkName.startsWith("iphoneos")) "Arm64" else "X64"
val framework = kotlin.targets.getByName<KotlinNativeTarget>(targetName).binaries.getFramework(mode)
inputs.property("mode", mode)
dependsOn(framework.linkTask)
val targetDir = File(buildDir, "xcode-frameworks")
from({ framework.outputDirectory })
into(targetDir)
}
tasks.getByName("build").dependsOn(packForXcode)
When you’re using cocoapods plugin you don’t need to manually declare packForXcode target, maybe that’s the problem. Try to remove everything after val packForXcode
cocoapods section should be inside kotlin, not inside ios

kotlin-compiler-embeddable missing within gradle build

I am trying to setup a multiproject gradle/kotlin build and I am getting following error:
Could not determine the dependencies of task ':compileKotlin'.
> Could not resolve all files for configuration ':kotlinCompilerClasspath'.
> Cannot resolve external dependency org.jetbrains.kotlin:kotlin-compiler-embeddable:1.4.10 because no repositories are defined.
Required by:
project :
Strange thing is the empty project :.
My simplifiged build.gradle.kts looks like this:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
base
kotlin("jvm") version "1.4.10"
}
buildscript {
extra["kotlin_version"] = "1.4.10"
repositories {
jcenter()
mavenCentral()
}
}
subprojects {
apply(plugin = "kotlin")
repositories {
jcenter()
mavenCentral()
}
tasks.compileKotlin {
kotlinOptions.jvmTarget = "11"
}
tasks.compileTestKotlin {
kotlinOptions.jvmTarget = "11"
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:${rootProject.extra["kotlin_version"]}")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${rootProject.extra["kotlin_version"]}")
}
}
Do I need to duplicate the repositories within the buildscript section?
What is that is causing the error above?
It seems like your repositories { ... } configuration done in the subproject section is sufficient, but it is only applied to the supbrojects but not the root project itself (which has the Kotlin plugin applied, too, and whose task :compileKotlin is failing).
There are two ways to fix this.
First, you could move the repositories { ... } section from subprojects { ... } to a new block allprojects { ... } (thus applied to the root project as well).
Or, if you don't actually need the Kotlin plugin in the root project (i.e. you don't have Kotlin code there), you can add .apply(false) to your plugin declaration:
plugins {
kotlin("jvm").version("1.4.10").apply(false)
}

Can't import dependencies for Kotlin multi platform common

I am trying out Kotlin multi-platform and trying to setup all my dependencies for it. Starting with commonMain
I am trying to add Koin and Ktor dependencies to the common portion but I cant seem to be able to use any of them.
This is my Gradle script
plugins {
id 'org.jetbrains.kotlin.multiplatform' version '1.3.61'
}
repositories {
google()
jcenter()
mavenCentral()
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
defaultConfig {
applicationId 'org.jetbrains.kotlin.mpp_app_android'
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName '1.0'
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
minifyEnabled false
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'org.koin:koin-android:2.0.1'
implementation 'org.koin:koin-androidx-viewmodel:2.0.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel:2.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
}
kotlin {
android("android")
// This is for iPhone emulator
// Switch here to iosArm64 (or iosArm32) to build library for iPhone device
iosX64("ios") {
binaries {
framework()
}
}
sourceSets {
commonMain {
dependencies {
implementation kotlin('stdlib-common')
implementation 'org.koin:koin-ktor:2.0.1'
implementation 'org.koin:koin-core:2.0.1'
// HTTP
implementation "io.ktor:ktor-client-core:1.2.6"
implementation "io.ktor:ktor-client-json:1.2.6"
implementation "io.ktor:ktor-client-serialization:1.2.6"
// Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.2.1"
}
}
commonTest {
dependencies {
implementation kotlin('test-common')
implementation kotlin('test-annotations-common')
}
}
androidMain {
dependencies {
implementation kotlin('stdlib')
}
}
androidTest {
dependencies {
implementation kotlin('test')
implementation kotlin('test-junit')
}
}
iosMain {
}
iosTest {
}
}
}
// This task attaches native framework built from ios module to Xcode project
// (see iosApp directory). Don't run this task directly,
// Xcode runs this task itself during its build process.
// Before opening the project from iosApp directory in Xcode,
// make sure all Gradle infrastructure exists (gradle.wrapper, gradlew).
task copyFramework {
def buildType = project.findProperty('kotlin.build.type') ?: 'DEBUG'
def target = project.findProperty('kotlin.target') ?: 'ios'
dependsOn kotlin.targets."$target".binaries.getFramework(buildType).linkTask
doLast {
def srcFile = kotlin.targets."$target".binaries.getFramework(buildType).outputFile
def targetDir = getProperty('configuration.build.dir')
copy {
from srcFile.parent
into targetDir
include 'app.framework/**'
include 'app.framework.dSYM'
}
}
}
In the Sample.kt file that was generated when I created the project I tried to setup Koin in the main method
fun main() {
startKoin {
// declare modules
modules(myModule)
}
println(hello())
}
but startKoin cannot be resolved.
I cleaned/rebuilt the project, did a gradle sync and still cant import koin or any other dependency so what am I missing
Koin is not multiplatform capable. We have [multiplatform forks][1], but would need to chat about which to use and what version. We're still discussing multiplatform strategy.
Ktor's dependencies are more complex than that. See below. I removed your other dependencies for clarity, but you should leave them obviously :)
sourceSets {
commonMain {
dependencies {
implementation "io.ktor:ktor-client-core:1.2.6"
implementation "io.ktor:ktor-client-json:1.2.6"
implementation "io.ktor:ktor-client-serialization:1.2.6"
}
}
androidMain {
dependencies {
implementation "io.ktor:ktor-client-core-jvm:1.2.6"
implementation "io.ktor:ktor-client-json-jvm:1.2.6"
implementation "io.ktor:ktor-client-serialization-jvm:1.2.6"
}
}
iosMain {
dependencies {
implementation "io.ktor:ktor-client-ios:1.2.6"
implementation "io.ktor:ktor-client-core-native:1.2.6"
implementation "io.ktor:ktor-client-json-native:1.2.6"
implementation "io.ktor:ktor-client-serialization-native:1.2.6"
}
}
}
[1]: https://github.com/kpgalligan/koin/tree/kpg/khan

Cannot update kotlin to v1.3.50 for multiplatform project

I'm using kotlin 1.3.41 for my multiplatform project (included ios, jvm, js) and everything is ok. Now I'm trying to update to the latest version 1.3.50 but get this error:
Could not determine the dependencies of task ':kotlinNpmInstall'.
> org.jetbrains.kotlin.gradle.targets.js.npm.KotlinNpmResolutionManager$ResolutionState$Installed cannot be cast to org.jetbrains.kotlin.gradle.targets.js.npm.KotlinNpmResolutionManager$ResolutionState$Configuring
I have tried to search everywhere (github, kotlin issue tracker) but got nothing. Does anyone know what causes this error? Thanks.
Part of my configuration:
Root project configuration
buildscript {
apply from: "buildsystem/dependencies.gradle"
repositories {
google()
jcenter()
mavenCentral()
maven { url "https://dl.bintray.com/kotlin/kotlinx/" }
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath deps.gradlePlugins.android
classpath deps.gradlePlugins.androidNavigation
classpath deps.gradlePlugins.node
classpath deps.gradlePlugins.kotlin
classpath deps.gradlePlugins.kotlinSerialization
classpath deps.gradlePlugins.kotlinAtomicfun
classpath deps.gradlePlugins.dokka
}
}
Multiplatform module configuration
apply plugin: 'org.jetbrains.kotlin.multiplatform'
apply plugin: 'kotlinx-serialization'
kotlin {
targets {
jvm("jvm")
js("js") {
nodejs {}
}
iosArm64("ios64")
iosX64("iosSim")
configure([ios64, iosSim]) {
binaries.framework {
baseName = "LIB"
}
}
}
sourceSets {
def commonDependencies = rootProject.ext.deps.common
commonMain {
dependencies {
implementation commonDependencies.kotlin
implementation commonDependencies.kotlinCoroutines
}
}
commonTest {
dependencies {
implementation commonDependencies.kotlinTest
implementation commonDependencies.kotlinTestAnnotations
implementation commonDependencies.mockk
}
}
def jvmDependencies = rootProject.ext.deps.jvm
jvmMain {
dependencies {
implementation jvmDependencies.kotlin
implementation jvmDependencies.kotlinCoroutines
}
}
jvmTest {
dependencies {
implementation jvmDependencies.kotlinTest
implementation jvmDependencies.kotlinTestJunit
implementation jvmDependencies.kotlinCoroutinesTest
implementation jvmDependencies.mockk
}
}
def jsDependencies = rootProject.ext.deps.js
jsMain {
dependencies {
implementation jsDependencies.kotlin
implementation jsDependencies.kotlinCoroutines
// npm
implementation npm("uuid", "^3.3.2")
}
}
jsTest {
dependsOn jsMain
dependsOn commonMain
dependencies {
implementation jsDependencies.kotlinTest
}
}
def iosDependencies = rootProject.ext.deps.ios
iosMain {
dependencies {
implementation iosDependencies.kotlinCoroutines
}
}
ios64Main {
dependsOn iosMain
}
iosSimMain {
dependsOn iosMain
}
}
}
// other configuration
I can confirm that the issue is in Android Studio.
When I run the official KotlinJS react app tutorial on IntelliJ, the app works. But on AS, I get the above error when executing any Gradle task dependent on npm.
Until the issue is resolved, I recommend using the Community version of IntelliJ. You can download it here