Reputation: 53
I unfortunately have big issues with getting an instance of a Room-Database for my android project. I followed the instructions on https://developer.android.com/training/data-storage/room/index.html But the last step won't work for me:
AppDatabase db = Room.databaseBuilder(getApplicationContext(),
AppDatabase.class, "database-name").build();
Their AppDatabase.java looks like this
@Database(entities = {User.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
public abstract UserDao userDao();
}
My Problem: At "AppDatabase db" I get the error 'cannot resolve symbol "AppDatabase" '. This is quite senseful, because AppDatabase is just an abstract class. But how is this supposed to work in the tutorial? I tried to get the instance in the onCreate method of my MainActivity. Here are my gradlefiles: buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
ext {
buildToolsVersion = "25.0.2"
supportLibVersion = "25.3.1"
archRoomVersion = "1.0.0-alpha1"
}
allprojects {
repositories {
google()
jcenter()
maven { url 'https://maven.google.com' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
and the second one:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.example.fabian.kohlmannkonstruktionen"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-
core:3.0.1'
compile 'android.arch.persistence.room:runtime:' +
rootProject.archRoomVersion
annotationProcessor 'android.arch.persistence.room:compiler:' +
rootProject.archRoomVersion
}
MainActivity.java
package com.example.fabian.kohlmannkonstruktionen;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.arch.persistence.db.SupportSQLiteDatabase;
import android.arch.persistence.room.Database;
import android.arch.persistence.room.Room;
import android.arch.persistence.room.RoomDatabase;
import android.arch.persistence.room.migration.Migration;
import android.content.Context;
public class MainActivity extends AppCompatActivity {
AppDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = AppDatabase.getInMemoryDatabase(getApplicationContext());
}
...
AppDatabase.java
package com.example.fabian.kohlmannkonstruktionen;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.arch.persistence.db.SupportSQLiteDatabase;
import android.arch.persistence.room.Database;
import android.arch.persistence.room.Room;
import android.arch.persistence.room.RoomDatabase;
import android.arch.persistence.room.migration.Migration;
import android.content.Context;
public class MainActivity extends AppCompatActivity {
AppDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = AppDatabase.getInMemoryDatabase(getApplicationContext());
}
Projekt.java
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;
/**
* Created by Fabian on 14.11.2017.
*/
@Entity
public class Projekt {
@PrimaryKey(autoGenerate = true)
Integer id;
Integer projektnummer;
Integer lohn,arbeitszeit;
String projektname, firma;
Boolean abgeschlossen;
public Projekt(String projektname,String firma,Integer projektnummer){
this.abgeschlossen=false;
this.projektname=projektname;
this.projektnummer=projektnummer;
this.firma=firma;
}
...getters and setters...
ProjektDao.java
import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Delete;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.Query;
import java.util.List;
/**
* Created by Fabian on 15.11.2017.
*/
@Dao
public interface ProjektDao {
@Query("SELECT * FROM Projekt")
List<Projekt> getAll();
@Query("SELECT * FROM Projekt where id LIKE :id")
Projekt findByID(String id);
@Query("SELECT COUNT(*) from Projekt")
int countUsers();
@Insert
void insertAll(Projekt... projekts);
@Delete
void delete(Projekt projekt);
}
Termin and TerminDao looks the same
This is the text from the gradle console:
Executing tasks: [assemble]
Configuration on demand is an incubating feature.
Configuration 'compile' in project ':app' is deprecated. Use 'implementation' instead.
:app:compileDebugJavaWithJavac
C:\Users\Fabian\AndroidStudioProjects\KohlmannKonstruktionen\app\src\main\java\TerminDao.java:14: warning: The query returns some columns [pausenzeit, startzeit, endzeit, lohnproh, mitarbeiter, standort] which are not use by Projekt. You can use @ColumnInfo annotation on the fields to specify the mapping. Projekt has some fields [projektnummer, lohn, arbeitszeit, projektname, firma, abgeschlossen] which are not returned by the query. If they are not supposed to be read from the result, you can mark them with @Ignore annotation. You can suppress this warning by annotating the method with @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH). Columns returned by the query: id, pausenzeit, startzeit, endzeit, lohnproh, mitarbeiter, standort. Fields in Projekt: id, projektnummer, lohn, arbeitszeit, projektname, firma, abgeschlossen.
List<Projekt> getAll();
^
C:\Users\Fabian\AndroidStudioProjects\KohlmannKonstruktionen\app\src\main\java\AppDatabase.java:13: warning: Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide `room.schemaLocation` annotation processor argument OR set exportSchema to false.
public abstract class AppDatabase extends RoomDatabase {
^
C:\Users\Fabian\AndroidStudioProjects\KohlmannKonstruktionen\app\src\main\java\com\example\fabian\kohlmannkonstruktionen\MainActivity.java:13: error: cannot find symbol
AppDatabase db = AppDatabase.getInMemoryDatabase(getApplicationContext());
^
symbol: class AppDatabase
location: class MainActivity
1 error
2 warnings
FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
* Get more help at https://help.gradle.org
BUILD FAILED in 5s
14 actionable tasks: 1 executed, 13 up-to-date
Upvotes: 5
Views: 13233
Reputation: 242
For me it was a case of copy/pasting.
Re-type @PrimaryKey etc so Android Studio knows you're using them, or the IDE just treats it as text
Upvotes: 1
Reputation: 310
My AppDatabase.class
is a little bit different to yours:
...
INSTANCE = Room.databaseBuilder(context, AppDatabase.class, "xxx")
// To simplify the codelab, allow queries on the main thread.
// Don't do this on a real app! See PersistenceBasicSample for an example.
.allowMainThreadQueries()
.build();
...
And my build.gradle
also includes lifecycle
:
// for room
compile 'android.arch.lifecycle:extensions:1.0.0';
compile 'android.arch.persistence.room:runtime:1.0.0';
annotationProcessor 'android.arch.lifecycle:compiler:1.0.0';
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0';
Upvotes: 5
Reputation: 3261
Checkout my code that might help you.
AppDatabase.java
@Database(entities = {Product.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
public abstract ProductDao productDao();
public static AppDatabase getInMemoryDatabase(Context context) {
if (INSTANCE == null) {
INSTANCE =
Room.inMemoryDatabaseBuilder(context.getApplicationContext(), AppDatabase.class).allowMainThreadQueries().build();
}
return INSTANCE;
}
public static void destroyInstance() {
INSTANCE = null;
}
}
Mainactivity.java
AppDatabase db = AppDatabase.getInMemoryDatabase(getApplicationContext());
Build.Gradle(Project)
allprojects {
repositories {
jcenter()
maven { url 'https://maven.google.com' }
}
}
Build.Gradle(App)
// Room
compile 'android.arch.persistence.room:runtime:1.0.0'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
This will create only one instance. this works well in my code.
Upvotes: 3