【问题标题】:Why I have syntax error in IDE after adding a gradle project to another repo even when the building works?为什么在将 gradle 项目添加到另一个 repo 后,即使构建工作,IDE 中也会出现语法错误?
【发布时间】:2018-10-26 01:13:32
【问题描述】:

我有一个适用于 iOS 和 Android 的 kotlin 多平台项目,它运行良好。它有一个common 模块用于共享业务逻辑,platform-androidplatform-ios 模块用于实现平台API。

在我将项目 A 中的 commonplatform-android 模块添加到另一个 Android 项目 B 后,Android Studio IDE 报告大量语法错误,但代码从 Android Studio 构建和运行没有问题。

语法看起来像 kotlin-stdlib 不存在,而它确实在 build.gradle 中,否则它不会构建。

例如:

val filterMap = mutableMapOf<String, MenuFilter>()

Android 工作室会说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"
    }
}

Android Studio 中的rootProject build.gradle 是:

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
}

这是 Android 项目 B 的 settings.gradle 文件

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 Ultimate 2018.1
  • Android 工作室 3.1.2

【问题讨论】:

  • 应该是 IDE 错误,AFAIK
  • 你能分享一个示例项目吗?

标签: android android-studio gradle intellij-idea kotlin


【解决方案1】:

尝试在build.gradle 中添加此内容

apply plugin: 'org.jetbrains.kotlin.multiplatform'

【讨论】:

    猜你喜欢
    • 2019-09-01
    • 2021-01-24
    • 2019-04-02
    • 2012-01-15
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    相关资源
    最近更新 更多