Daniel Iroka
Daniel Iroka

Reputation: 143

error : Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:

I am building a simple multi-module app, and haven't really even added much but I am encountering this error when trying to build the app. I have this base-like ViewModel implementation(which is an open class) that seats in a separate module called core:presentation and then I have a ViewModel for a screen that calls this BaseViewModel that seats in another module called feature:list:ui. First I will show the error, then show the code and their respective build.gradle.kts files. I am also using Hilt for DI.

The Error

Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath: class com.mvitemplate.feature.list.ui.ListScreenViewModel, unresolved supertypes: com.mvitemplate.core.presentation.mvi.FlowViewModel Adding -Xextended-compiler-checks argument might provide additional information.

BaseViewModel in Core Presentation Module and its build.gradle.kts

import androidx.lifecycle.ViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch

open class FlowViewModel<Intent, Result, ViewState> constructor(
    interactor: Interactor<Intent, Result>,
    private val reducer: Reducer<Result, ViewState>,
    initialState: ViewState,
    private val coroutineScope: CoroutineScope = CoroutineScope(SupervisorJob() + 
Dispatchers.Main.immediate)
): ViewModel(), BaseViewModel<Intent, ViewState> {

    // Took out some code but doesn't really affect this issue as I still encountered them 
even after taking this said code out.

    override fun emit(intent: Intent) {
        coroutineScope.launch {
            // Do something...
        }
    }

    override fun onCleared() {
        super.onCleared()
        coroutineScope.cancel()
    }
}

// Build.gradle.kts

plugins {
    alias(libs.plugins.android.library)
    alias(libs.plugins.jetbrains.kotlin.android)
    alias(libs.plugins.dagger.hilt.android)
    alias(libs.plugins.google.devtools.ksp)
}

android {
    namespace = "com.mvitemplate.core"
    compileSdk = 34

    defaultConfig {
        minSdk = 24

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles("consumer-rules.pro")
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "17"
    }
}

dependencies {

    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.appcompat)
    implementation(libs.androidx.compose.runtime)
    implementation(libs.material)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)

    implementation(libs.kotlinx.coroutines.android)
    implementation(libs.kotlinx.coroutines.core)

    // hilt
    implementation(libs.hilt.android)
    ksp(libs.hilt.android.compiler)
}

And then the ListScreenViewModel in the feature:list:ui module and its Build.gradle.kts

import com.mvitemplate.core.presentation.mvi.FlowViewModel
import com.mvitemplate.core.presentation.mvi.Interactor
import com.mvitemplate.core.presentation.mvi.Reducer
import com.mvitemplate.feature.list.ui.ListScreenContract.Intent
import com.mvitemplate.feature.list.ui.ListScreenContract.Result
import com.mvitemplate.feature.list.ui.ListScreenContract.ViewState
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class ListScreenViewModel @Inject constructor(
    interactor: Interactor<Intent, Result>,
    reducer: Reducer<Result, ViewState>,
    viewState: ViewState
): FlowViewModel<Intent, Result, ViewState>(
    interactor = interactor,
    reducer = reducer,
    initialState = viewState
)

// Build.gradle.kts

plugins {
    alias(libs.plugins.android.library)
    alias(libs.plugins.jetbrains.kotlin.android)
    alias(libs.plugins.dagger.hilt.android)
    alias(libs.plugins.google.devtools.ksp)
}

android {
    namespace = "com.mvitemplate.feature.list.ui"
    compileSdk = 34

    defaultConfig {
        minSdk = 24

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles("consumer-rules.pro")
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    buildFeatures {
        compose = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.5.1"
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "17"
        freeCompilerArgs += "-Xextended-compiler-checks"
    }
}

dependencies {

    implementation(project(":core:presentation"))

    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.appcompat)
    implementation(libs.androidx.activity.compose)
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.ui)
    implementation(libs.androidx.ui.graphics)
    implementation(libs.androidx.ui.tooling.preview)
    implementation(libs.androidx.material3)
    implementation(libs.material)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)

    // kotlin coroutines
    implementation(libs.kotlinx.coroutines.android)
    implementation(libs.kotlinx.coroutines.core)

    // navigation
    implementation(libs.androidx.navigation.compose)

    // hilt
    implementation(libs.hilt.android)
    ksp(libs.hilt.android.compiler)
 }

I know that there are a lot of questions with this Title, but none of them seem to help as the nature of the cause of the problem is very different from mine. I've cross-checked all the dependencies and made sure that the List module is correctly calling the presentation module and yet still encountering this error when trying to build the Project. Hoping anyone can help with this, since the code isn't so much.

Upvotes: 0

Views: 33

Answers (1)

Daniel Iroka
Daniel Iroka

Reputation: 143

Okay so I resolved this problem by declaring the core:presentation dependency in the build.gradle for list:ui as api instead of implementation.

Before implementation(project(":core:presentation"))

After api(project(":core:presentation"))

Or just declare the dependency in the app's build.gradle or build.gradle.kts if you are using kotlin DSL.

Upvotes: 0

Related Questions