Denis Steinman
Denis Steinman

Reputation: 7809

Kotlin can't find class when I use coroutines

I have the next repository entities:

data class User(
    @SerializedName("id")
    val id: Long,
    @SerializedName("email")
    val email: String,
    @SerializedName("first_name")
    val firstName: String,
    @SerializedName("last_name")
    val lastName: String,
    @SerializedName("patronymic")
    val patronymic: String? = null
) {
    val fullName: String
        get() = "$firstName  $lastName"
}

interface IRestApiService {
    @GET("users/me")
    suspend fun getMe(@Query("email") email: String): Response<User>

    object Builder {
        fun build(): IRestApiService {
            val retrofit = Retrofit.Builder()
                .baseUrl(ROOT_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
            return retrofit.create(IRestApiService::class.java)
        }
    }

    companion object {
        const val ROOT_URL = "http://localhost:5000/api/v1/"
    }
}

class DataSource(private val restApi: IRestApiService) {

    suspend fun login(email: String, password: String): Result<User> {
        val response = restApi.getMe(email)

        return if (response.isSuccessful) {
            val user = response.body()

            if (user != null) {
                Result.Success(user)
            } else {
                Result.Error(Exception("User is null"))
            }
        } else {
            Result.Error(Exception("Can't fetch user"))
        }
    }

    fun logout() {
        // TODO: revoke authentication
    }
}

And Repository.kt:

class Repository(val dataSource: DataSource) : CoroutineScope {

    // in-memory cache of the loggedInUser object
    var user: User? = null
        private set

    val isLoggedIn: Boolean
        get() = user != null

    init {
        user = null
    }

    fun logout() {
        user = null
        dataSource.logout()
    }

    fun login(username: String, password: String) = liveData(context = coroutineContext) {
        val result = dataSource.login(username, password)

        if (result is Result.Success) {
            setUser(result.data)
        }

        emit(result)
    }

    private fun setUser(user: User) {
        this.user = user
    }

    override val coroutineContext: CoroutineContext
        get() = Dispatchers.IO
}

But when I try to build my app I get the error:

blah-blah/DataSource.java:16: error: cannot find symbol
    kotlin.coroutines.Continuation<? super com.blah-blah.Result<com.blah-blah.User>> p2) {
                                                                                                   ^
  symbol:   class User
  location: package blah-blah/IRestApiService.java:16: error: cannot find symbol
    kotlin.coroutines.Continuation<? super retrofit2.Response<com.blah-blah.User>> p1);

Here is my build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    buildToolsVersion '30.0.0 rc4'
    compileSdkVersion 29

    defaultConfig {
        applicationId 'blah-blah'
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName '1.0'

        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    }

    dataBinding {
        enabled = true
    }

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

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.annotation:annotation:1.1.0'
    //LifeCycle
    implementation 'androidx.lifecycle:lifecycle-common:2.2.0'
    implementation 'androidx.lifecycle:lifecycle-runtime:2.2.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
    // Retrofit 2
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.google.code.gson:gson:2.8.6'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:4.7.2'
    //Coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.5'

    kapt 'com.android.databinding:compiler:3.1.4'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

Why do I get this error? What's wrong with my code? Android Studio doesn't highlight any errors.

UPD
After I've deleted the fullName property from the User all begun works like magic. But I still don't get why?

Upvotes: 1

Views: 1506

Answers (1)

Arthur Rodrigues
Arthur Rodrigues

Reputation: 21

See the package in your class, this same problem happened to me. The package was wrong and at the time of import, I couldn't find it. Make sure you have this line at the beginning of your class file with the path used in your code.

Image in the link below: file path

Just add and build again

Upvotes: 2

Related Questions