I'm using new beta version of Android Studio 3.4 and kotlin dsl as bulding gradle scripts. I have a lot of modules in the project and that's why I write KotlinBuildScript extension for building projects, but I have error message for all external dependencies like "ERROR: Failed to resolve: com.google.dagger:dagger:2.18", "ERROR: Failed to resolve: androidx.recyclerview:recyclerview:1.0.0". How can I fix it?
Here is my root build.gradle.kts:
buildscript {
repositories {
mavenCentral()
jcenter()
google()
maven(url = uri("https://dl.bintray.com/kotlin/kotlin-eap"))
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.10")
}
}
allprojects {
repositories {
mavenCentral()
jcenter()
google()
maven(url = uri("https://dl.bintray.com/kotlin/kotlin-eap"))
}
}
And my KotlinBuildScript:
object Version {
val kotlin = "1.3.10"
val androidx = "1.0.0"
val constraintlayout = "1.1.3"
val lifecycle = "2.0.0"
val paging = "2.1.0-alpha01"
}
fun KotlinBuildScript.setupApplication(applicationId:String, versionCode:Int, versionName:String) =
setup(applicationId, versionCode, versionName, false)
fun KotlinBuildScript.setupLibrary(applicationId:String) = setup(applicationId, null, null, true)
private fun KotlinBuildScript.setup(
applicationId:String, versionCode:Int?, versionName:String?, library:Boolean
) {
setupPlugins(library)
setupConfiguration(library, applicationId, (versionCode ?: 1) * 2, versionName ?: "1.0")
setupDependencies(library)
}
private fun KotlinBuildScript.setupConfiguration(
library:Boolean, applicationId:String, versionCode:Int, versionName:String
) {
configure<BaseExtension> {
compileSdkVersion(28)
buildToolsVersion("28.0.3")
defaultConfig {
if (!library) {
this.applicationId = applicationId
this.versionCode = versionCode
this.versionName = versionName
}
minSdkVersion(16)
targetSdkVersion(28)
multiDexEnabled = true
vectorDrawables.useSupportLibrary = true
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
}
}
packagingOptions {
exclude("META-INF/LICENSE.txt")
}
lintOptions {
isCheckReleaseBuilds = false
}
if (library) {
defaultConfig.ndk {
abiFilters("armeabi-v7a", "arm64-v8a", "x86", "x86_64")
}
externalNativeBuild {
cmake {
setPath("CMakeLists.txt")
}
}
} else {
flavorDimensions("architecture")
productFlavors {
create("arm") {
dimension = "architecture"
this.versionCode = versionCode - 1
versionNameSuffix = "-arm"
ndk {
abiFilters("armeabi")
}
}
create("x86") {
dimension = "architecture"
this.versionCode = versionCode
versionNameSuffix = "-x86"
ndk {
abiFilters("x86")
}
}
}
}
}
}
private fun KotlinBuildScript.setupPlugins(library:Boolean) {
apply {
plugin("com.android." + (if (library) "library" else "application"))
plugin("kotlin-android")
plugin("kotlin-android-extensions")
plugin("kotlin-kapt")
}
}
private fun KotlinBuildScript.setupDependencies(library:Boolean) {
dependencies {
add("testImplementation", "junit:junit:4.12")
add("implementation", fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
if (!library) {
add("implementation", project(":Library"))
}
add("kapt", "com.google.dagger:dagger-compiler:2.18")
add("implementation", "com.google.dagger:dagger:2.18")
add("kapt", "androidx.lifecycle:lifecycle-compiler:${Version.lifecycle}")
add("implementation", "androidx.lifecycle:lifecycle-extensions:${Version.lifecycle}")
add("implementation", "androidx.lifecycle:lifecycle-common-java8:${Version.lifecycle}")
add("implementation", "javax.inject:javax.inject:1")
add("implementation", "javax.annotation:javax.annotation-api:1.2")
add("implementation", "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${Version.kotlin}")
add("implementation", "org.jetbrains.kotlin:kotlin-reflect:${Version.kotlin}")
add("implementation", "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.0-RC1")
}
}
App module gradle:
import Setup.Version
import Setup.setupLibrary
setupLibrary("com.esminis.server.library")
I've found how to solve the problem. Using kotlin dsl you should always set buildFileName property in the settings.gradle of the project.
rootProject.buildFileName = 'build.gradle.kts'
Also, try to delete .idea and .gradle folder and restart Android Studio.
Related
When I upgrade gradle version from 7.33 to 7.41 or higher. Every line in every gradle.kts become unresolved. I tried to replace all lines(except namespace) in corresponding files of a new project created from Android Studio template and there is no problem. I can't figure out what's wrong in my existing project.
project build.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("com.android.application")
kotlin("android")
kotlin("kapt")
//id("dagger.hilt.android.plugin")
id("com.google.devtools.ksp") version "1.6.21-1.0.5"
}
val composeVersion: String = rootProject.extra["composeVersion"] as String
val coroutinesVersion: String = rootProject.extra["coroutinesVersion"] as String
android {
compileSdk = 32
defaultConfig {
applicationId = "com.app.myapp"
minSdk = 24
targetSdk = 32
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
)
}
getByName("debug") {
applicationIdSuffix = ".debug"
versionNameSuffix = "-debug"
}
}
compileOptions {
isCoreLibraryDesugaringEnabled = true
sourceCompatibility(JavaVersion.VERSION_11)
targetCompatibility(JavaVersion.VERSION_11)
}
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions.freeCompilerArgs += "-opt-in=org.mylibrary.OptInAnnotation"
kotlinOptions.jvmTarget = JavaVersion.VERSION_11.toString()
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = composeVersion
}
packagingOptions {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
namespace = "com.app.myapp"
applicationVariants.all {
kotlin.sourceSets {
getByName(name) {
kotlin.srcDir("build/generated/ksp/$name/kotlin")
}
}
}
}
dependencies {
implementation("androidx.core:core-ktx:1.7.0")
// Jetpack Compose
implementation("androidx.compose.ui:ui:$composeVersion")
implementation("androidx.compose.material:material:$composeVersion")
implementation("androidx.compose.ui:ui-tooling-preview:$composeVersion")
debugImplementation("androidx.compose.ui:ui-tooling:$composeVersion")
implementation("androidx.compose.foundation:foundation:$composeVersion")
implementation("androidx.compose.material:material-icons-core:$composeVersion")
implementation("androidx.activity:activity-compose:1.4.0")
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.4.1")
implementation("com.google.accompanist:accompanist-systemuicontroller:0.24.9-beta")
implementation("com.google.accompanist:accompanist-permissions:0.24.9-beta")
// Material design 3
implementation("androidx.compose.material3:material3:1.0.0-alpha12")
implementation("androidx.compose.material3:material3-window-size-class:1.0.0-alpha12")
// Navigation
implementation("androidx.navigation:navigation-compose:2.4.2")
implementation("io.github.raamcosta.compose-destinations:animations-core:1.5.7-beta")
ksp("io.github.raamcosta.compose-destinations:ksp:1.5.7-beta")
// Hilt
/*implementation("com.google.dagger:hilt-android:2.42")
kapt("com.google.dagger:hilt-compiler:2.42")*/
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion")
// Nordic ble library
implementation("no.nordicsemi.android.support.v18:scanner:1.6.0")
implementation("no.nordicsemi.android:ble-ktx:2.4.1")
implementation("no.nordicsemi.android:ble-common:2.4.1")
// Java 8+ API desugaring
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.5")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.4.1")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.3")
androidTestImplementation("androidx.test.espresso:espresso-core:3.4.0")
androidTestImplementation("androidx.compose.ui:ui-test-junit4:$composeVersion")
}
kapt {
correctErrorTypes = true
}
app build.gradle.kts
buildscript {
val composeVersion by extra("1.2.0-beta02")
val coroutinesVersion by extra("1.6.1")
dependencies {
classpath("com.google.dagger:hilt-android-gradle-plugin:2.42")
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id ("com.android.application") version "7.2.0" apply false
id ("com.android.library") version "7.2.0" apply false
id ("org.jetbrains.kotlin.android") version "1.6.21" apply false
}
tasks.register("clean",Delete::class){
delete(rootProject.buildDir)
}
settings.gradle.kts
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "My App"
include(":app")
i migrated my gradle file to kotlin dsl from groovy. Everything works fine but there is a problem with improting values to gradle from another kotlin file. I can import variables and autocomplete just works fine but when i build project, it gives me Unresolved reference error.
Appreciate any help.
Gradle version : 7.1.1
Android studio : Arctic Fox
build.gradle.kts that i created under buildSrc :
plugins {
`java-gradle-plugin`
`kotlin-dsl`
`kotlin-dsl-precompiled-script-plugins`
}
repositories {
mavenCentral()
}
File that i import from in buildSrc/src/main/java/Dependencies :
object Versions {
const val compose_version = "1.0.0"
}
object Dependencies {
const val compose_ui = "androidx.compose.ui:ui:${Versions.compose_version}"
const val compose_material = "androidx.compose.material:material:${Versions.compose_version}"
const val compose_ui_tooling_preview = "androidx.compose.ui:ui-tooling-preview:${Versions.compose_version}"
const val compose_ui_tooling = "androidx.compose.ui:ui-tooling:${Versions.compose_version}"
const val compose_ui_test = "androidx.compose.ui:ui-test-junit4:${Versions.compose_version}"
}
app build.gradle.kts file :
plugins {
id("com.android.application")
kotlin("android")
}
android {
compileSdk = 30
defaultConfig {
applicationId = "com.example.compose"
minSdk = 21
targetSdk = 30
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}
buildTypes {
debug {
isMinifyEnabled = false
isDebuggable = true
}
release {
isMinifyEnabled = true
isDebuggable = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility(JavaVersion.VERSION_1_8)
targetCompatibility(JavaVersion.VERSION_1_8)
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.0.0"
}
packagingOptions {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}
dependencies {
implementation("androidx.core:core-ktx:1.6.0")
implementation("androidx.appcompat:appcompat:1.3.1")
implementation("com.google.android.material:material:1.4.0")
// Jetpack Compose
implementation(Dependencies.compose_ui)
implementation(Dependencies.compose_material)
implementation(Dependencies.compose_ui_tooling_preview)
debugImplementation(Dependencies.compose_ui_tooling)
androidTestImplementation(Dependencies.compose_ui_test)
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.3.1")
implementation("androidx.activity:activity-compose:1.3.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.3")
androidTestImplementation("androidx.test.espresso:espresso-core:3.4.0")
}
You said that your other file is under buildSrc/src/main/java/Dependencies. Since this is a Kotlin file, it should be buildSrc/src/main/kotlin/Dependencies instead.
im getting this gradle sync error - The Hilt Android Gradle plugin is applied but no com.google.dagger:hilt-android dependency was found. can someone help please ? :)
im using kotlin dsl, i have libs.kt in buildSrc
buildSrc - build.gradle.kts:
plugins {
`kotlin-dsl`
}
repositories {
jcenter()
}
libs.kt in buildSrc
object libs {
object hilt {
private const val dagger_hilt_version = "2.30.1-alpha"
private const val jetpack_hilt_version = "1.0.0-alpha02"
const val gradlePlugin = "com.google.dagger:hilt-android-gradle-plugin:$dagger_hilt_version"
const val android = "com.google.dagger:hilt-android:$dagger_hilt_version"
const val android_compiler = "com.google.dagger:hilt-android-compiler:$dagger_hilt_version"
const val jetpack_viewmodel = "androidx.hilt:hilt-lifecycle-viewmodel:$jetpack_hilt_version"
const val jetpack_compiler = "androidx.hilt:hilt-compiler:$jetpack_hilt_version"
}
/* more objects + more constants */
}
my app build.gradle.kts looks like:
plugins {
id("com.android.application")
id("kotlin-android")
id("kotlin-kapt")
id("dagger.hilt.android.plugin")
id("androidx.navigation.safeargs.kotlin")
id("com.google.firebase.crashlytics")
id("com.google.gms.google-services")
id("com.google.firebase.firebase-perf")
}
android {
useLibrary("org.apache.http.legacy")
compileSdkVersion = "30"
dependenciesInfo {
includeInApk = true
includeInBundle = true
}
buildFeatures {
dataBinding = true
compose = true
}
defaultConfig {
vectorDrawables.useSupportLibrary = true
applicationId = "aa.bb.cc"
targetSdkVersion(30)
minSdkVersion(21)
versionCode = 1
versionName = "1.0.0"
}
signingConfigs {
register("configReleaseCZ").configure {
/*hidden part*/
}
}
bundle {
language {
enableSplit = false
}
}
buildTypes {
named("debug").configure {
isDebuggable = true
extra.set("enableCrashlytics", false)
}
named("release").configure {
isShrinkResources = true
isMinifyEnabled = true
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
isDebuggable = false
extra.set("enableCrashlytics", true)
signingConfig = signingConfigs.getByName("configReleaseCZ")
}
}
composeOptions {
kotlinCompilerVersion = libs.kotlin.version
kotlinCompilerExtensionVersion = libs.compose.version
}
lintOptions {
isAbortOnError = false
}
}
kapt {
correctErrorTypes = true
}
dependencies {
//implementation("com.google.dagger:hilt-android:2.30.1-alpha")
//kapt ("com.google.dagger:hilt-android-compiler:2.30.1-alpha") //this doesnt work wither
implementation(libs.hilt.android)
kapt(libs.hilt.android_compiler)
implementation(libs.hilt.jetpack_viewmodel)
kapt(libs.hilt.jetpack_compiler)
/* many more dependencies */
}
kapt {
correctErrorTypes = true
}
my project build.gradle.kts:
buildscript {
repositories {
google()
mavenCentral()
maven(url = "https://jitpack.io")
maven(url = "https://repo1.maven.org/maven2/")
maven(url = "https://plugins.gradle.org/m2/")
maven(url = "https://github.com/userxpro/userx/raw/maven/")
maven(url = "https://dl.bintray.com/kotlin/kotlin-eap/")
maven(url = "https://dl.bintray.com/kotlin/kotlinx/")
maven(url = "https://kotlin.bintray.com/kotlinx")
maven(url = "https://oss.sonatype.org/content/repositories/snapshots")
maven(url = "http://storage.googleapis.com/r8-releases/raw")
gradlePluginPortal()
}
dependencies {
classpath(libs.r8)
classpath(libs.androidGradlePlugin)
classpath(libs.kotlin.gradlePlugin)
classpath(libs.hilt.gradlePlugin)
classpath("com.google.gms:google-services:4.3.4")
classpath("com.google.firebase:firebase-crashlytics-gradle:2.4.1")
classpath("com.google.firebase:perf-plugin:1.3.4")
}
}
subprojects {
repositories {
google()
mavenCentral()
maven(url = "https://jitpack.io")
maven(url = "https://repo1.maven.org/maven2/")
maven(url = "https://plugins.gradle.org/m2/")
maven(url = "https://dl.bintray.com/kotlin/kotlin-eap/")
maven(url = "https://dl.bintray.com/kotlin/kotlinx/")
maven(url = "https://kotlin.bintray.com/kotlinx")
maven(url = "https://oss.sonatype.org/content/repositories/snapshots")
maven(url = "https://github.com/userxpro/userx/raw/maven/")
jcenter()
}
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
// Treat all Kotlin warnings as errors
//useIR = true
//allWarningsAsErrors = true
freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn"
// Enable experimental coroutines APIs, including Flow
freeCompilerArgs += "-Xopt-in=kotlinx.coroutines.ExperimentalCoroutinesApi"
freeCompilerArgs += "-Xopt-in=kotlinx.coroutines.FlowPreview"
freeCompilerArgs += "-Xopt-in=kotlin.Experimental"
freeCompilerArgs += "-Xallow-jvm-ir-dependencies"
// Set JVM target to 1.8
jvmTarget = "1.8"
}
}
}
remove
id("dagger.hilt.android.plugin")
sync gradle
add
id("dagger.hilt.android.plugin")
In my case the issue was related to an incompatible version of the Hilt library/plugin and the Android Gradle plugin, that occurred after I updated to the Android Gradle Plugin to 7.0.1.
I updated Hilt from 2.35 -> 2.38.1 and the app compiled once again.
Im trying to publish an Android library (debug/release) with the new Kotlin MultiPlatform set up. Im successfully able to publish iOS frameworks, but not android libraries. This is the error I end up getting:
A problem occurred configuring project ':shared'.
Failed to notify project evaluation listener.
Kotlin target 'android' tried to set up publishing for Android build variants that are not library variants or do not exist:
* release
* debug
Check the 'publishLibraryVariants' property, it should point to existing Android library variants. Publishing of application and test variants is not supported.
However, I can clearly see the variants in my Android Studio. Im also able to run the Android/IOS app from Android Studio using the library dependecy as a project dependency
implementation(project(":shared"))
Only facing problems when trying to publish it.
This is my build.gradle.kts:
plugins {
kotlin("multiplatform")
kotlin("plugin.serialization")
id("com.android.library")
id("kotlin-android-extensions")
id("com.squareup.sqldelight")
id("dev.icerock.mobile.multiplatform-resources")
`maven-publish`
}
group = "com.sekhar.testkmp"
version = "0.1"
val coroutinesVersion = "1.3.9-native-mt"
val serializationVersion = "1.0.0-RC"
val ktorVersion = "1.4.0"
val sqlDelightVersion: String by project
repositories {
gradlePluginPortal()
google()
jcenter()
mavenCentral()
}
kotlin {
android {
publishLibraryVariants("release", "debug")
}
ios {
binaries {
framework {
baseName = "shared"
}
}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")
implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("io.ktor:ktor-client-serialization:$ktorVersion")
implementation("com.squareup.sqldelight:runtime:$sqlDelightVersion")
implementation("dev.icerock.moko:resources:0.13.1")
implementation("dev.icerock.moko:parcelize:0.4.0")
implementation("dev.icerock.moko:graphics:0.4.0")
}
}
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")
implementation("io.ktor:ktor-client-android:$ktorVersion")
implementation("com.squareup.sqldelight:android-driver:$sqlDelightVersion")
}
}
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.12")
}
}
val iosMain by getting {
dependencies {
implementation("io.ktor:ktor-client-ios:$ktorVersion")
implementation("com.squareup.sqldelight:native-driver:$sqlDelightVersion")
}
}
val iosTest by getting
}
}
dependencies {
commonMainApi("dev.icerock.moko:resources:0.13.1")
}
multiplatformResources {
multiplatformResourcesPackage = "com.sekhar.testkmp" // required
}
android {
compileSdkVersion(29)
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdkVersion(24)
targetSdkVersion(29)
versionCode = 1
versionName = "1.0"
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
}
getByName("debug") {
isMinifyEnabled = false
}
}
lintOptions {
isAbortOnError = false
}
}
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)```
The most common solution to this is to apply the android plugin: id("com.android.library") before the kotlin multiplatform plugin: kotlin("multiplatform").
plugins {
id("com.android.library")
kotlin("multiplatform")
kotlin("plugin.serialization")
id("kotlin-android-extensions")
id("com.squareup.sqldelight")
id("dev.icerock.mobile.multiplatform-resources")
`maven-publish`
}
I am creating a Kotlin Multiplatform library with Android support.
When I try to build with gradle I get an error (./gradlew build --warning-mode all).
Either this:
Could not determine the dependencies of task ':lib:compileDebugAidl'.
> Failed to find target with hash string '29' in: /home/user/Android/Sdk
Or that:
Could not determine the dependencies of task ':lib:verifyReleaseResources'.
> Failed to find target with hash string '29' in: /home/user/Android/Sdk
The first right after indexing and the second when I do ./gradlew clean
However, there is this dependency in any case since I have already created Android apps based on that.
I have also tried Target 28 and an RC version of 30 (since 30 has not yet been officially released, but I have still created apps)
My structure of the gradle files is like this:
root
|-build.gradle.kts
|-settings.gradle.kts
|-gradlew
|-gradlew.bat
|-gradle
|-wrapper
|-gradle-wrapper.jar
|-gradle-wrapper.properties
|-lib
|-build.gradle.kts
settings.gradle.kts:
rootProject.name = "Library"
include(":lib")
root build.gradle.kts:
buildscript {
repositories {
addRepos()
maven(uri("https://dl.bintray.com/kotlin/kotlin-eap")) {
metadataSources {
gradleMetadata()
mavenPom()
}
}
}
dependencies {
classpath("com.android.tools.build:gradle:4.1.0-beta04")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72")
}
}
allprojects {
repositories {
addRepos()
maven(uri("https://dl.bintray.com/kotlin/kotlin-eap")) {
metadataSources {
gradleMetadata()
mavenPom()
}
}
}
}
lib build.gradle.kts:
plugins {
id("com.android.library")
kotlin("multiplatform")
id("maven-publish")
}
group = "de.datlag"
version = "1.0"
android {
compileSdkVersion = 29.toString()
buildToolsVersion = "29.0.3"
defaultConfig {
targetSdkVersion(29)
versionCode = 1
versionName = "1.0"
}
buildTypes {
val debug by getting {
isMinifyEnabled = false
isDebuggable = true
isShrinkResources = false
}
val release by getting {
isMinifyEnabled = false
isDebuggable = false
isShrinkResources = false
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
repositories {
addRepos()
}
kotlin {
jvm {
compilations.all {
kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString()
}
}
js {
browser()
nodejs()
}
android {
publishAllLibraryVariants()
}
val hostOs = System.getProperty("os.name")
val isMingwX64 = hostOs.startsWith("Windows")
val nativeTarget = when {
hostOs == "Mac OS X" -> macosX64("native")
hostOs == "Linux" -> linuxX64("native")
isMingwX64 -> mingwX64("native")
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
}
sourceSets {
val commonMain by getting {
dependencies {
implementation(kotlin("stdlib-common"))
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val jvmMain by getting {
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
}
val jvmTest by getting {
dependencies {
implementation(kotlin("test"))
implementation(kotlin("test-junit"))
}
}
val androidMain by getting {
dependsOn(jvmMain)
}
val androidTest by getting {
dependsOn(jvmTest)
}
val jsMain by getting {
dependencies {
implementation(kotlin("stdlib-js"))
}
}
val jsTest by getting {
dependencies {
implementation(kotlin("test-js"))
}
}
val nativeMain by getting { }
val nativeTest by getting { }
}
}
addRepos method:
fun RepositoryHandler.addRepos() {
mavenLocal {
metadataSources {
gradleMetadata()
mavenPom()
}
}
mavenCentral {
metadataSources {
gradleMetadata()
mavenPom()
}
}
jcenter {
metadataSources {
gradleMetadata()
mavenPom()
}
}
google {
metadataSources {
gradleMetadata()
mavenPom()
}
}
gradlePluginPortal()
}
In lib.build.gradle.kts you should use the compileSdkVersion(29) syntax rather than setting a string. You can see in the source for these extensions that the behavior is different if you call the string version.
/** #see #getCompileSdkVersion() */
public void compileSdkVersion(String version) {
checkWritability();
this.target = version;
}
/** #see #getCompileSdkVersion() */
public void compileSdkVersion(int apiLevel) {
compileSdkVersion("android-" + apiLevel);
}
The when passing an int it will append "android-" for you. So you could do compileSdkVersion = "android-29" if you wanted, but just passing the int is easier