Reputation: 141
So I have 3 modules
:commons:ui
which contains BaseFragment
:features:home
which contains HomeFragment
, implements :commons:ui
:app
which contains MainActivity
, implements :features:home
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
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
Reputation: 73395
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