我有一个 kotlin 多平台项目适用于 iOS 和 Android 的设置,它运行良好。它有一个 common
用于共享业务逻辑的模块,以及 platform-android
和 platform-ios
用于实现平台 API 的模块。
在我添加 common
之后和 platform-android
从项目 A 到另一个 Android 项目 B 的模块,Android Studio IDE 报告大量语法错误,但代码从 Android Studio 构建和运行没有问题。
语法类似于 kotlin-stdlib
不存在,而它确实在 build.gradle
中, 否则它不会构建。
例如:
val filterMap = mutableMapOf<String, MenuFilter>()
Android studio 会说 Unresolved reference: mutableMapOf
一些事实:
-
common
模块有问题 platform-android
没问题.- 当然,我用IDEA编辑项目A也没问题
我的 build.gradle
对于 common
模块看起来像这样:
apply plugin: 'kotlin-platform-common'
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlin_version"
testCompile "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version"
}
sourceSets {
main.kotlin.srcDirs += 'main/'
test.kotlin.srcDirs += 'test/'
}
task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.kotlin
}
artifacts {
archives sourcesJar
}
kotlin {
experimental {
coroutines "enable"
}
}
根项目 build.gradle
在 Android Studio 中是:
buildscript {
ext{
kotlin_version = '1.2.41'
anko_version = '0.10.4'
dagger_version = '2.15'
support_lib_version = '27.1.1'
}
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
maven { url "https://jitpack.io" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
这是 settings.gradle
Android项目B的文件
def projectA_path = "path/to/projectA"
include(":common")
project(":common").projectDir = new File("$projectA_path/common")
include(":platforms:android")
project(":platforms:android").projectDir = new File("$projectA_path/platforms/android")
根据此处的设置,即使两者来自不同的项目。两者不应该一起工作吗?我在这里缺少什么?
IDE 版本:
- IDEA 终极版 2018.1
- 安卓工作室 3.1.2
最佳答案
尝试在 build.gradle
中添加这个
apply plugin: 'org.jetbrains.kotlin.multiplatform'
关于android - 为什么在将 gradle 项目添加到另一个 repo 后,IDE 中出现语法错误,即使在构建工作时也是如此?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50363138/