【问题标题】:Android studio: Kotlin scope functions Unresolved reference. But Project compilesAndroid 工作室:Kotlin 范围函数未解决的参考。但是项目编译
【发布时间】:2019-11-30 03:39:45
【问题描述】:

我正面临这个奇怪的问题,我的项目编译并成功运行,但在我的 kotlin 范围函数中出现红色错误。它还显示了一些 kotlin 函数的错误,如 toLong()、toDouble() 等。

我的 gradle 文件中有这个

   apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.xyz"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 4
        multiDexEnabled true
        versionName "1.2"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    lintOptions {
        checkReleaseBuilds false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }

    dataBinding {
        enabled = true
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
repositories {
    maven { url 'https://jitpack.io' }
}
dependencies {
    def lifecycle_version = "2.0.0"
    def room_version = "beta01"
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //android X
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"

    implementation 'com.android.support:multidex:1.0.3'

    api 'com.theartofdev.edmodo:android-image-cropper:2.8.0'
    api 'de.hdodenhof:circleimageview:3.0.0'
    api 'com.jakewharton:butterknife:10.1.0'
    implementation 'com.google.android.gms:play-services-location:17.0.0'

    // For Kotlin use kapt instead of annotationProcessor
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.20'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'
    implementation "androidx.room:room-runtime:2.2.0-$room_version"
    implementation "androidx.room:room-ktx:2.2.0-$room_version"
    implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
    api(name: 'sdk-release-1.6.1', ext: 'aar') {
        transitive = true
    }
    // Add the Firebase SDK for Google Analytics
    implementation 'com.google.firebase:firebase-analytics:17.2.0'
    implementation 'com.google.firebase:firebase-messaging:20.0.0'
    //annotation processors
    annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
    annotationProcessor "androidx.room:room-compiler:2.2.0-$room_version"
    // Add dependency
    implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
    //facebook app events sdk
    implementation 'com.facebook.android:facebook-android-sdk:[5,6)'
}
apply plugin: 'com.google.gms.google-services'  // Google Play services Gradle plugin

Gradle 属性

org.gradle.jvmargs=-Xmx1536m
android.useAndroidX=true
android.enableJetifier=true

构建等级

    buildscript {

    repositories {
        google()
        jcenter()
        maven {
            url 'https://maven.fabric.io/public'
        }
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.20"
        classpath 'com.google.gms:google-services:4.3.1'
        classpath 'io.fabric.tools:gradle:1.31.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        flatDir {
            dirs 'libs'
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

我尝试过使缓存/重新启动无效,还尝试删除 .idea 和 .gradle 文件,但没有任何帮助,我还尝试重新启动 android studio,还尝试从 git 更改分支,没有任何帮助。 任何帮助表示赞赏。

【问题讨论】:

  • 尝试使用 kotlin 1.3.60。它们几乎兼容
  • 这终于解决了我的问题。非常感谢
  • 你会为同样的问题推荐协程版本吗? @弗拉德
  • 最新版本是1.3.2bintray,它使用了kotlin 1.3.50。我觉得没关系,gradle会用最新的版本

标签: android android-studio kotlin kotlin-android-extensions


【解决方案1】:

自 2 周前以来,我在使用 Kotlin 的 Android Studio 上也有过类似的体验。 当我在 GitHub 上使用旧 Kotlin 插件版本 1.3.31 导入项目时。我很快将其更改为1.3.72(最新的kotlin),然后我也遇到了这个错误。

当我将插件版本更改为1.3.61(旧版本)并同步我的项目时,我可以修复错误。它删除了错误并将其更新到最新的插件版本1.3.72。现在一切都很顺利。

buildscript {
    ext.kotlin_version = '1.3.72'
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.0-alpha06"
    }
}

我就像魔术一样。

只需将 Kotlin 插件版本更改为 2 或 3 个旧版本 向后 并同步您的项目。我应该工作

希望对你有帮助

【讨论】:

    【解决方案2】:

    改变这一行

    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.50'
    

    进入

    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.50'
    

    然后再做一次,清理构建,然后使缓存无效并重新启动。

    【讨论】:

    • 仍然面临同样的问题
    • 您能否向您展示整个应用程序 build.gradle 和项目 build.gradle 以及 gradle.properties 和 local.properties
    猜你喜欢
    • 2021-02-17
    • 2019-03-19
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 2021-11-02
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多