Home > Back-end >  AndroidX Room generated class error: "Class is public, should be declared in a file named class
AndroidX Room generated class error: "Class is public, should be declared in a file named class

Time:05-09

Numerous similar questions on SO but they do not pertain to classes generated by the AndroidX Room library. Please do not mark this question a duplicate, because similar questions relate to traditional Java classes, not those generated by Room.

Following the documentation on Android Room, I have defined a database and table entity as follows:

Database:

@Database(entities = [User::class], version = 1, exportSchema = false)
abstract class UserDatabase : RoomDatabase() {
    ...
}

Entity:

@Entity
data class User(
    ...
)

But upon building, I receive an error traced to a generated file user.java:

error: class User is public, should be declared in a file named User.java

Has anyone run into this error when working with Room?

CodePudding user response:

Move the entity to its own file named User.kt.

  • Related