3

I am trying out the new Room ORM from googles Android Architecture Components, I followed the documentation but when i run my code i get java.lang.RuntimeException: cannot find implementation for com.zeyad.usecases.app.UserDatabase. UserDatabase_Impl does not exist

Here is my code:

Gradle project:

allprojects {
repositories {
    jcenter()
    maven { url 'https://jitpack.io' }
    maven { url 'https://maven.google.com' }
}
}

Gradle module:

compile "android.arch.lifecycle:runtime:1.0.0-alpha1"
compile "android.arch.lifecycle:extensions:1.0.0-alpha1"
annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha1"
compile "android.arch.persistence.room:runtime:1.0.0-alpha1"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha1"

User:

@Entity(tableName = "User")
public class User {

    static final String LOGIN = "login";
    private static final String ID = "id", AVATAR_URL = "avatar_url";
    @PrimaryKey
    @SerializedName(LOGIN)
    @ColumnInfo(name = LOGIN)
    String login;

    @SerializedName(ID)
    @ColumnInfo(name = ID)
    int id;

    @SerializedName(AVATAR_URL)
    @ColumnInfo(name = AVATAR_URL)
    String avatarUrl;

    public User() {
    }

    public User(String login, int id, String avatarUrl) {
        this.login = login;
        this.id = id;
        this.avatarUrl = avatarUrl;
    }

    public static boolean isEmpty(User user) {
        return user == null || (user.login == null && user.avatarUrl == null);
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getAvatarUrl() {
        return avatarUrl;
    }

    public void setAvatarUrl(String avatarUrl) {
        this.avatarUrl = avatarUrl;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        return id == user.id && (login != null ? login.equals(user.login) : user.login == null
                && (avatarUrl != null ? avatarUrl.equals(user.avatarUrl) : user.avatarUrl == null));
    }

    @Override
    public int hashCode() {
        int result = login != null ? login.hashCode() : 0;
        result = 31 * result + id;
        result = 31 * result + (avatarUrl != null ? avatarUrl.hashCode() : 0);
        return result;
    }
}

UserDatabase:

@Database(entities = {User.class}, version = 1)
public abstract class UserDatabase extends RoomDatabase {
    public abstract RoomBaseDao roomBaseDao();
}

RoomBaseDao:

@Dao
public interface RoomBaseDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertItemsReplace(User... objects);

    @Update(onConflict = OnConflictStrategy.REPLACE)
    void updateItemsReplace(User... objects);

    @Delete
    void deleteItem(User object);
}

Application onCreate:

UserDatabase gDb = Room.databaseBuilder(this, UserDatabase.class, "test-db").build(); // throws the exception above

Thanks in advance.

Zeyad Gasser
  • 1,516
  • 21
  • 39
  • It feels like the code is not getting generated as expected. Did you include both dependencies in your `build.gradle` file? – CommonsWare May 24 '17 at 15:51
  • i updated the question with the gradle dependencies – Zeyad Gasser May 24 '17 at 16:33
  • OK, that looks fine. If you go into your module's `build/generated/source/` directory, do you see an `apt/debug/` directory with your `UserDatabase_Impl.java` and `RoomBaseDao_Impl.java` classes (in the appropriate Java package)? – CommonsWare May 24 '17 at 17:43
  • i have this directory but its empty – Zeyad Gasser May 24 '17 at 18:45
  • That's the crux of the issue, then. Assuming that cleaning and rebuilding the project does not help, check your Gradle Console and see if there are any messages from the annotation processor that might explain why your code is not being generated. – CommonsWare May 24 '17 at 18:59
  • yea cleaning the project and rebuilding doesnt solve, so what i can do now ? – Zeyad Gasser May 24 '17 at 19:32
  • As I wrote, check your Gradle Console and see if there are any messages from the annotation processor that might explain why your code is not being generated. – CommonsWare May 24 '17 at 19:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145064/discussion-between-zeyad-gasser-and-commonsware). – Zeyad Gasser May 24 '17 at 19:37

2 Answers2

5

In my case I was using old version of ButterKnife which required android-apt plugin. So I updated ButterKnife (which now uses annotation processor) and removed plugin declaration in Gradle file.

Probably that was not exactly your problem, but maybe it will help others.

Nominalista
  • 4,632
  • 11
  • 43
  • 102
0

Another thing which may cause the problem:

configurations.all {
  resolutionStrategy {
    force "com.android.support:support-annotations:$SUPPORT_LIBRARY_VERSION"
  }
}
IvBaranov
  • 540
  • 2
  • 10
  • 24