Android studio: Kotlin scope functions Unresolved reference. But Project compiles

|▌冷眼眸甩不掉的悲伤 提交于 2020-06-17 08:04:13

问题


I am facing this strange issue where my project compiles and runs successfully but in my kotlin scope functions red errors are coming. It also shows errors on some of the kotlin functions like toLong(), toDouble() etc.

and I have this in my gradle file

   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 Properties

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

build gradle

    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
}

I have tried Invalidate cache/Restart and also tried deleting .idea and .gradle files nothing helped, I have also tried to restart the android studio and also tried to change branches from git nothing helped. any help appreciated.


回答1:


I had similar experience on Android Studio with Kotlin since 2 weeks ago. When I import a project on GitHub with old Kotlin plugin version 1.3.31. I quickly changed it to 1.3.72 (the latest kotlin) then, I had had this error too.

I could fix the error when I changed the plugin version to 1.3.61 (older version) and sync my project. It removed the error and update it to the latest plugin version 1.3.72. And everything worked smoothly now.

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"
    }
}

I was like magic.

Just changed the Kotlin plugin version to a 2 or 3 older versions backward and sync your project. I should work

I hoped it helps




回答2:


Change this line

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

Into

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

And then do again, clean build then invalidate cache and restart.



来源:https://stackoverflow.com/questions/59112993/android-studio-kotlin-scope-functions-unresolved-reference-but-project-compile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!