William Estrada
William Estrada

Reputation: 141

Supertypes of the following classes cannot be resolved error in multi module project

So I have 3 modules

now if I try to run MainActivity using the following code in onCreate

supportFragmentManager
    .beginTransaction()
    .add(R.id.fragment_container, HomeFragment())
    .commit()

I get the error

e: Supertypes of the following classes cannot be resolved. Please make sure you have the required 
dependencies in the classpath:
class tech.vrutal.home.HomeFragment, unresolved supertypes: tech.vrutal.ui.BaseFragment

But if I use Navigation Component as follows (removing fragmentManager code in MainActivity)

// activity_main.xml

<androidx.fragment.app.FragmentContainerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_container"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/nav_graph" />

// nav_graph.xml

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nav_graph"
    app:startDestination="@id/home_fragment">

    <fragment
        android:id="@+id/home_fragment"
        android:name="tech.vrutal.home.HomeFragment"
        android:label="HomeFragment" />

</navigation>

now it works as expected

so why creating HomeFragment() directly in MainActivity fails, while using Navigation Component just works fine

Upvotes: 0

Views: 406

Answers (2)

Nico
Nico

Reputation: 2862

I was facing this problem in a very similar module architecture.

I solved this issue by changing the dependency declaration in :features:home 's build.gradle file

from

dependencies {
    //...
    implementation project(":commons:ui")
    //...
}

to

dependencies {
    //...
    api project(":commons:ui")
    //...
}

Upvotes: 2

I got the same error many many times in a day. I don't know the cause or how to resolve it, but every time I try to run the app for the second time, it works.

Upvotes: 0

Related Questions