Search code examples
androidandroid-room

Android Room Database auto-genereated code does not compile


I cannot compile the project because the auto-generated code does not compile. Here is my code:

I'm using version 2.4.2 of Room

My database class:

RecipeDatabase.java

@Database(entities = {Person.class, Recipe.class}, version = 1)
public abstract class RecipeDatabase extends RoomDatabase {
    public abstract PersonDao personDao();
    public abstract RecipeDao recipeDao();
}

Entity classes:

Person.java

@Entity(tableName = "people")
public class Person {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "person_id")
    public int id;

    public String name;

    public Person(String name) {
        this.name = name;
    }
}

Recipe.java

@Entity(tableName = "recipes")
public class Recipe {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "recipe_id")
    public int id;

    public String name;
    public FoodType type; //it is an enum

    public Recipe(String name, FoodType type) {
        this.name = name;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public String getTypeName() {
        return type.getName();
    }

    public int getImageResource() {
        return type.getImgResource();
    }
}

DAO classes:

PersonDao.java

@Dao
public interface PersonDao {

    @Query("SELECT * FROM people")
    List<Person> getAll();

    @Query("SELECT name FROM people")
    List<String> getNames();
}

RecipeDao.java

@Dao
public interface RecipeDao {

    @Query("SELECT * FROM recipes")
    List<Recipe> getAll();

    @Query("SELECT name FROM recipes")
    List<String> getNames();
}

And here is the code which does not compile (under java (generated)):

RecipeDatabase_Impl.java

      @Override
      protected void onCreate(SupportSQLiteDatabase _db) {
        if (mCallbacks != null) {
          for (int _i = 0, _size = mCallbacks.size(); _i < _size; _i++) {
            mCallbacks.get(_i).onCreate(_db);
          }
        }
      }

...

      @Override
      protected RoomOpenHelper.ValidationResult onValidateSchema(SupportSQLiteDatabase _db) {
        final HashMap<String, TableInfo.Column> _columnsPeople = new HashMap<String, TableInfo.Column>(2);
        _columnsPeople.put("person_id", new TableInfo.Column("person_id", "INTEGER", true, 1, null, TableInfo.CREATED_FROM_ENTITY));
        _columnsPeople.put("name", new TableInfo.Column("name", "TEXT", false, 0, null, TableInfo.CREATED_FROM_ENTITY));
        final HashSet<TableInfo.ForeignKey> _foreignKeysPeople = new HashSet<TableInfo.ForeignKey>(0);
        final HashSet<TableInfo.Index> _indicesPeople = new HashSet<TableInfo.Index>(0);
        final TableInfo _infoPeople = new TableInfo("people", _columnsPeople, _foreignKeysPeople, _indicesPeople);
        final TableInfo _existingPeople = TableInfo.read(_db, "people");
        if (! _infoPeople.equals(_existingPeople)) {
          return new RoomOpenHelper.ValidationResult(false, "people(com.szabolcst.recipes.model.Person).\n"
                  + " Expected:\n" + _infoPeople + "\n"
                  + " Found:\n" + _existingPeople);
        }
        final HashMap<String, TableInfo.Column> _columnsRecipes = new HashMap<String, TableInfo.Column>(3);
        _columnsRecipes.put("recipe_id", new TableInfo.Column("recipe_id", "INTEGER", true, 1, null, TableInfo.CREATED_FROM_ENTITY));
        _columnsRecipes.put("name", new TableInfo.Column("name", "TEXT", false, 0, null, TableInfo.CREATED_FROM_ENTITY));
        _columnsRecipes.put("type", new TableInfo.Column("type", "TEXT", false, 0, null, TableInfo.CREATED_FROM_ENTITY));
        final HashSet<TableInfo.ForeignKey> _foreignKeysRecipes = new HashSet<TableInfo.ForeignKey>(0);
        final HashSet<TableInfo.Index> _indicesRecipes = new HashSet<TableInfo.Index>(0);
        final TableInfo _infoRecipes = new TableInfo("recipes", _columnsRecipes, _foreignKeysRecipes, _indicesRecipes);
        final TableInfo _existingRecipes = TableInfo.read(_db, "recipes");
        if (! _infoRecipes.equals(_existingRecipes)) {
          return new RoomOpenHelper.ValidationResult(false, "recipes(com.szabolcst.recipes.model.Recipe).\n"
                  + " Expected:\n" + _infoRecipes + "\n"
                  + " Found:\n" + _existingRecipes);
        }
        return new RoomOpenHelper.ValidationResult(true, null);
      }
    }, "1c493a2b22e38e23def00f0336257ee2", "6829c4aff16c254527acbdc5d6cecc85");
    final SupportSQLiteOpenHelper.Configuration _sqliteConfig = SupportSQLiteOpenHelper.Configuration.builder(configuration.context)
        .name(configuration.name)
        .callback(_openCallback)
        .build();
    final SupportSQLiteOpenHelper _helper = configuration.sqliteOpenHelperFactory.create(_sqliteConfig);
    return _helper;
  }

In both methods the problem is with the access modifer (protected) and the error goes: 'attempting to assign weaker access privileges; was public'

The methods that these override are:

RoomOpenHelper.kt

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX)
    abstract class Delegate(@JvmField val version: Int) {
        abstract fun dropAllTables(database: SupportSQLiteDatabase)
        abstract fun createAllTables(database: SupportSQLiteDatabase)
        abstract fun onOpen(database: SupportSQLiteDatabase)
        abstract fun onCreate(database: SupportSQLiteDatabase)

...

        @Suppress("DEPRECATION")
        open fun onValidateSchema(db: SupportSQLiteDatabase): ValidationResult {
            validateMigration(db)
            return ValidationResult(true, null)
        }

The full errors:

error: onCreate(SupportSQLiteDatabase) in <anonymous com.szabolcst.recipes.persistance.RecipeDatabase_Impl$1> cannot override onCreate(SupportSQLiteDatabase) in Delegate
      protected void onCreate(SupportSQLiteDatabase _db) {
                     ^
  attempting to assign weaker access privileges; was public
error: onValidateSchema(SupportSQLiteDatabase) in <anonymous com.szabolcst.recipes.persistance.RecipeDatabase_Impl$1> cannot override onValidateSchema(SupportSQLiteDatabase) in Delegate
      protected RoomOpenHelper.ValidationResult onValidateSchema(SupportSQLiteDatabase _db) {
                                                ^
  attempting to assign weaker access privileges; was public

Naturally editing anything in the generated files does not help, and I can't find anything like this on any forums


Solution

  • Changing implementation "androidx.room:room-runtime:2.4.2" back to implementation "androidx.room:room-runtime:2.3.0" seemed to help as now RoomOpenHelper.java is used instead of the kotlin version and in the java version the access modifer is protected.