JetBrains / kotlin-native

Kotlin/Native infrastructure
Apache License 2.0
7.02k stars 564 forks source link

Could not find method kapt() multiplatform project #2551

Closed SBNTT closed 5 years ago

SBNTT commented 5 years ago

Start a new project in intellij IDEA and choose kotlin (Mobile ANdroid/iOS)template. in app/build.gradle

add apply plugin: 'kotlin-kapt' after apply plugin: 'kotlin-android-extensions'

add

implementation 'com.github.bumptech.glide:glide:4.8.0'
kapt 'com.github.bumptech.glide:compiler:4.8.0'

in the main dependencies.

error: Could not find method kapt() for arguments [com.github.bumptech.glide:compiler:4.8.0] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

h0tk3y commented 5 years ago

@SBNTT Please make sure you specify the kapt dependency within a top-level dependencies { ... } block, not inside a dependencies block for some of the source sets.

SBNTT commented 5 years ago

Hi, as you can see, it is what i've done:

plugins {
    id 'kotlin-multiplatform' version '1.3.10'
}
repositories {
    google()
    jcenter()
    mavenCentral()
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "org.jetbrains.kotlin.mpp_app_android"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'

    implementation 'com.github.bumptech.glide:glide:4.8.0'
    kapt 'com.github.bumptech.glide:compiler:4.8.0'
}

kotlin {
    targets {
        fromPreset(presets.android, 'android')
        // This preset is for iPhone emulator
        // Switch here to presets.iosArm64 (or iosArm32) to build library for iPhone device
        fromPreset(presets.iosX64, 'ios') {
            compilations.main.outputKinds('FRAMEWORK')
        }
    }
    sourceSets {
        commonMain {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
            }
        }
        commonTest {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-test-common'
                implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common'
            }
        }
        androidMain {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib'
            }
        }
        androidTest {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-test'
                implementation 'org.jetbrains.kotlin: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 "link${buildType.toLowerCase().capitalize()}Framework${target.capitalize()}"

    doLast {
        def srcFile = kotlin.targets."$target".compilations.main.getBinary("FRAMEWORK", buildType)
        def targetDir = getProperty("configuration.build.dir")
        copy {
            from srcFile.parent
            into targetDir
            include 'app.framework/**'
            include 'app.framework.dSYM'
        }
    }
}
h0tk3y commented 5 years ago

@SBNTT Ok, thanks for the details. I can now see that the kapt dependency is declared before the Kotlin Android target is created, and at that point, the Kapt plugin cannot know which configurations it should create. Please try to move that dependency declaration to a place in the build script after the Android target is created.

SBNTT commented 5 years ago

Ho thanks ! maybe this should be moved in the project template also

KursX commented 4 years ago

@SBNTT Ok, thanks for the details. I can now see that the kapt dependency is declared before the Kotlin Android target is created, and at that point, the Kapt plugin cannot know which configurations it should create. Please try to move that dependency declaration to a place in the build script after the Android target is created.

What is the solution for this problem? I also can not figure it out

Could not find method kapt() for arguments [androidx.room:room-compiler:2.2.3] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

plugins {
    id 'kotlin-multiplatform'
}
repositories {
    google()
    jcenter()
    mavenCentral()
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "a.b.c"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1"
    }
}
kotlin {
    targets {
        fromPreset(presets.android, 'android')
        fromPreset(presets.iosX64, 'ios') {
            compilations.main.outputKinds('FRAMEWORK')
        }
        final def iOSTarget = System.getenv('SDK_NAME')?.startsWith("iphoneos") \
                              ? presets.iosArm64 : presets.iosX64
        fromPreset(iOSTarget, 'iOS') {
            compilations.main.outputKinds('FRAMEWORK')
        }
        fromPreset(presets.android, 'android')
    }
    sourceSets {
        commonMain {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
            }
        }
        androidMain {
            dependencies {
                implementation 'org.jetbrains.kotlin:kotlin-stdlib'
            }
        }
        iosMain {
            dependencies {
            }
        }
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"   
    implementation 'androidx.room:room-runtime:2.2.3'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    kapt 'androidx.room:room-compiler:2.2.3'
}
artem-mi commented 4 years ago

@KursX Try the following order of directives:

...
kotlin {
    ...
}
apply plugin: 'kotlin-kapt'
dependencies {
    ...
}