gpt4 book ai didi

gradle - Kotlin MPP - 在 IntelliJ 上使用 Gradle 构建问题

转载 作者:行者123 更新时间:2023-12-03 05:27:29 28 4
gpt4 key购买 nike

我正在尝试使用一些 Koltin MPP 模块构建一个多模块 Gradle 项目。

我在尝试构建 Kotlin MPP 模块时遇到了一个非常奇怪的问题。所有指定的依赖都解决了,可以从 Gradle 配置中看到,但是当我尝试使用移动目标(Android 和 iOS)中的某些类时,我做不到。类根本无法解决。

这是我有问题的模块的 gradle 构建文件:

plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.multiplatform'
id 'org.jetbrains.kotlin.plugin.serialization'
}

group 'bg.dalexiev'
version '0.0.1'

def ktor_version = '1.3.1'
def serialization_version = '0.14.0'
def coroutines_version = '1.3.3'
def timber_version = '4.7.1'

android {
compileSdkVersion 29

defaultConfig {
minSdkVersion 21
versionName '0.0.1'
versionCode 1
}

sourceSets {
main {
manifest.srcFile 'src/androidMain/AndroidManifest.xml'
java.srcDirs = ['src/androidMain/kotlin']
res.srcDirs = ['src/androidMain/res']
}

test {
java.srcDirs = ['src/androidTest/kotlin']
}
}

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

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

}

repositories {
google()
}

dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

implementation "io.ktor:ktor-client-okhttp:$ktor_version"
implementation "io.ktor:ktor-client-json-jvm:$ktor_version"
implementation "io.ktor:ktor-client-serialization-jvm:$ktor_version"
implementation "io.ktor:ktor-client-logging-jvm:$ktor_version"

implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version"

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"

implementation "com.jakewharton.timber:timber:$timber_version"

testImplementation("junit:junit:4.12")
testImplementation("org.jetbrains.kotlin:kotlin-test")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}

kotlin {
android("android")

def sdkName = System.getenv('SDK_NAME')
def iosTarget = sdkName?.startsWith('iphoneos') ? presets.iosArm64 : presets.iosX64
targetFromPreset(iosTarget, "ios") {
binaries {
framework()
}
}
sourceSets {
commonMain {
dependencies {
implementation kotlin('stdlib-common')

implementation "io.ktor:ktor-client-core:$ktor_version"
implementation "io.ktor:ktor-client-json:$ktor_version"
implementation "io.ktor:ktor-client-serialization:$ktor_version"
implementation "io.ktor:ktor-client-logging:$ktor_version"

implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version"

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
}
}
commonTest {
dependencies {
implementation kotlin('test-common')
implementation kotlin('test-annotations-common')
}
}

iosMain {
dependencies {
implementation "io.ktor:ktor-client-ios:$ktor_version"
implementation "io.ktor:ktor-client-json-native:$ktor_version"
implementation "io.ktor:ktor-client-serialization-native:$ktor_version"
implementation "io.ktor:ktor-client-logging-native:$ktor_version"

implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$serialization_version"

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$coroutines_version"
}
}

iosTest {
}
}
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
jvmTarget = "1.8"
}
}

configurations {
compileClasspath
}

我能够构建一个具有相同配置的独立 gradle 项目而没有任何问题。任何帮助和建议将不胜感激。

最佳答案

您的 android 依赖项位于错误的 block 中。将您的 build.gradle 重构为

plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.multiplatform'
id 'org.jetbrains.kotlin.plugin.serialization'
}

group 'bg.dalexiev'
version '0.0.1'

def ktor_version = '1.3.1'
def serialization_version = '0.14.0'
def coroutines_version = '1.3.3'
def timber_version = '4.7.1'

android {
compileSdkVersion 29

defaultConfig {
minSdkVersion 21
versionName '0.0.1'
versionCode 1
}

sourceSets {
main {
manifest.srcFile 'src/androidMain/AndroidManifest.xml'
java.srcDirs = ['src/androidMain/kotlin']
res.srcDirs = ['src/androidMain/res']
}

test {
java.srcDirs = ['src/androidTest/kotlin']
}
}

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

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

}

repositories {
google()
}

kotlin {
android("android")

def sdkName = System.getenv('SDK_NAME')
def iosTarget = sdkName?.startsWith('iphoneos') ? presets.iosArm64 : presets.iosX64
targetFromPreset(iosTarget, "ios") {
binaries {
framework()
}
}
sourceSets {
commonMain {
dependencies {
implementation kotlin('stdlib-common')

implementation "io.ktor:ktor-client-core:$ktor_version"
implementation "io.ktor:ktor-client-json:$ktor_version"
implementation "io.ktor:ktor-client-serialization:$ktor_version"
implementation "io.ktor:ktor-client-logging:$ktor_version"

implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version"

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
}
}
commonTest {
dependencies {
implementation kotlin('test-common')
implementation kotlin('test-annotations-common')
}
}

androidMain.dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation "io.ktor:ktor-client-okhttp:$ktor_version"
implementation "io.ktor:ktor-client-json-jvm:$ktor_version"
implementation "io.ktor:ktor-client-serialization-jvm:$ktor_version"
implementation "io.ktor:ktor-client-logging-jvm:$ktor_version"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
implementation "com.jakewharton.timber:timber:$timber_version"
}

androidTest.dependencies {
implementation("junit:junit:4.12")
implementation("org.jetbrains.kotlin:kotlin-test")
implementation("org.jetbrains.kotlin:kotlin-test-junit")
}

iosMain {
dependencies {
implementation "io.ktor:ktor-client-ios:$ktor_version"
implementation "io.ktor:ktor-client-json-native:$ktor_version"
implementation "io.ktor:ktor-client-serialization-native:$ktor_version"
implementation "io.ktor:ktor-client-logging-native:$ktor_version"

implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$serialization_version"

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$coroutines_version"
}
}

iosTest {
}
}
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
jvmTarget = "1.8"
}
}

configurations {
compileClasspath
}

关于gradle - Kotlin MPP - 在 IntelliJ 上使用 Gradle 构建问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60723519/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com