Kotlin Multiplatform : Plugin [id: 'com.android.library'] was not found
Asked Answered
O

2

13

I am trying to set up Android Library via Kotlin Multiplatform Library.

I am getting error:

Plugin [id: 'com.android.library'] was not found in any of the following sources:

Please help me by pointing out what is wrong and how to resolve this issue.

My plugin set up is this(with supposedly proper buildscript):

plugins {
    id("org.jetbrains.kotlin.plugin.serialization").version("1.3.72")
    id("com.android.library")
    id("org.jetbrains.kotlin.multiplatform").version("1.3.72")
    id("com.squareup.sqldelight")
}

when I replace that with following

plugins {
    id("org.jetbrains.kotlin.multiplatform").version("1.3.72")
    id("org.jetbrains.kotlin.plugin.serialization").version("1.3.72")
}

apply plugin: 'com.android.library'
apply plugin: 'com.squareup.sqldelight'

it's working fine. I am not sure what causes the issue.

Following is the complete build.gradle:

buildscript {
    repositories {
        mavenCentral()
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.squareup.sqldelight:gradle-plugin:1.3.0'
        classpath 'com.android.tools.build:gradle:3.6.3'
    }
}

plugins {
    id("org.jetbrains.kotlin.plugin.serialization").version("1.3.72")
    id("com.android.library")
    id("org.jetbrains.kotlin.multiplatform").version("1.3.72")
    id("com.squareup.sqldelight")
}

repositories {
    mavenCentral()
    jcenter()
    google()
}

group 'com.jolas.sdk.kn.newsycore'
version '0.0.1'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles 'consumer-rules.pro'
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

def ktor_version = '1.3.2'
kotlin {
    android {
        publishLibraryVariants("release", "debug")
    }
    // This is for iPhone simulator
    // Switch here to iosArm64 (or iosArm32) to build library for iPhone device
    iosX64("ios") {
        binaries {
            framework()
        }
    }
    sourceSets {
        commonMain {
            dependencies {
                implementation kotlin('stdlib-common')
                implementation "io.ktor:ktor-client-core:$ktor_version"
                implementation "io.ktor:ktor-client-serialization:$ktor_version"
            }
        }
        commonTest {
            dependencies {
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }
        androidMain {
            dependencies {
                implementation kotlin('stdlib')
                implementation "io.ktor:ktor-client-okhttp:$ktor_version"
                implementation "io.ktor:ktor-client-serialization-jvm:$ktor_version"
                implementation "com.squareup.sqldelight:sqlite-driver:1.3.0"
            }
        }
        iosMain {
            dependencies {
                implementation kotlin('stdlib')
                implementation "io.ktor:ktor-client-ios:$ktor_version"
                implementation "io.ktor:ktor-client-serialization-native:$ktor_version"
                implementation "com.squareup.sqldelight:native-driver:1.3.0"
            }
        }
    }
}

sqldelight {
    NewsyDatabase {
        packageName = "com.jolas.sdk.kn.newsycore"
    }
}

configurations {
    compileClasspath
}
Overpower answered 28/5, 2020 at 3:20 Comment(2)
Hello, @Harley! Can you please add more details about your error? There should also be a list of sources, maybe the one with this plugin is missing.Aoristic
Sure. @ArtyomDegtyarev. Please download this project github.com/jolasjoe/newsy-core you'll find the build.gradle file in root-dir. In that file, if you remove the line apply plugin: 'com.android.library' and add id("com.android.library") under plugins section you will be able to reproduce the issue.Overpower
S
7

Nelson is correct regarding the pluginManagement block, but there is an additional requirement for a plugin to be found without additional configuration. If you require plugins { id("com.android.application") version "3.6.1" }, Gradle would search for the following Maven artifact: com.android.application:com.android.application.gradle.plugin:3.6.1. This is a convention. When developing your custom plugin, Gradle's default java-gradle-plugin will provide such an artifact out of the box. See the corresponding section of Gradle docs for more details.

To override module resolution for any plugin, you can use the resolutionStrategy block. I've got the following for Android plugins:

pluginManagement {
    repositories {
        google()
        gradlePluginPortal()
        jcenter()
    }
    resolutionStrategy {
        eachPlugin {
            if (requested.id.namespace == "com.android") {
                useModule("com.android.tools.build:gradle:${requested.version}")
            } else if(requested.id.namespace == "com.google.gms") {
                useModule("com.google.gms:${requested.id.name}:${requested.version}")
            }
        }
    }
}

Having this in your settings.gradle.kts will allow you to use the usual plugins block as follows:

plugins {
    id("com.android.application") version "3.6.1"
    id("com.google.gms.google-services") version "4.3.3"
}

Of course, to re-use the same version of the plugin in all submodules you can go with the usual route of apply false:

/build.gradle.kts:

plugins {
    id("com.android.application") version "3.6.1" apply false
    id("com.android.library") version "3.6.1" apply false
    // ...
}

/lib/build.gradle.kts:

plugins {
    id("com.android.library")
    // ...
}
Spalla answered 28/5, 2020 at 23:5 Comment(0)
P
0

The plugins block gets its dependencies from the pluginManagement block in settings.gradle not the build.gradle buildscript block. Please add the following to your settings.gradle file and try again.

pluginManagement {
    repositories {
        google()
        gradlePluginPortal()
    }
}
Paraph answered 28/5, 2020 at 10:11 Comment(1)
The issue still occurs, Nelson. Please download the project from github.com/jolasjoe/newsy-core and please see if you can make your solution work.Overpower

© 2022 - 2024 — McMap. All rights reserved.