Project with androidx.room packed into aar library causes java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/room/RoomDatabase

Viewed 768

This is the problem that I didn't find any solution on this resource, so please share some knowledge. I made a lib that works fine as a module. here's the gradle of it:

def room_version = "2.2.6"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-ktx:$room_version"
implementation "androidx.room:room-rxjava2:$room_version"

now, when I assemle it into AAR, program crashes with java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/room/RoomDatabase

When I googled the problem it turned out that it is a common problem and solution is to switch from androidx.room to android.arch.persistence.room. But that older version doesn't have room-rxjava2 I'm using.

So, maybe anyone knows solution of using androidx.room in AAR without having a crash?

2 Answers

We must also add the room dependency to the app module (where the aar is imported) to support androidx.room with room-rxJava2 :

def room_version = "2.3.0"

implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"

// To use Kotlin annotation processing tool
kapt("androidx.room:room-compiler:$room_version")

// RxJava2 support for Room
implementation "androidx.room:room-rxjava2:$room_version"

try add this line to app(not the module which assemble to aar)

runtimeOnly "androidx.room:room-ktx:$room_version"
Related