K.Os
K.Os

Reputation: 5496

Kotlin Room setup problems in Android Studio

I'm trying hard to setup simple example of Room+Kotlin in Android Studio, but it is really a pain to do it.

I have classes like:

@Entity(tableName = "test")
data class Test(val name: String, val lastname: String)

DAO

@Dao
interface TestDao {
@Query("SELECT * FROM test")
fun loadAll(): List<Test>

@Insert
fun insertAll(vararg tests: Test)

@Delete
fun delete(test: Test)
}

AppDatabase:

@Database(entities = arrayOf(Test::class), version = 1)
abstract class AppDatabase : RoomDatabase() {
   abstract fun testDao(): TestDao
}

My build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
    applicationId "com.example.roomtest"
    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'
    }
 }
}

ext.roomVersion = '1.0.0-alpha9'


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.espresso:espresso-
core:3.0.1', {
    exclude group: 'com.android.support', module: 'support-annotations'
})

// Room
compile "android.arch.persistence.room:runtime:$roomVersion"
compile "android.arch.persistence.room:rxjava2:$roomVersion"
kapt "android.arch.persistence.room:compiler:$roomVersion"

}
repositories {
mavenCentral()
}

Build.gradle(project)

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.1.51'
repositories {
    google()
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.0.0-beta7'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
    }
}

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

task clean(type: Delete) {
delete rootProject.buildDir
}

ext {
archRoomVersion = "1.0.0-alpha9"
}

And i try to do simple insert to my database in MainActivity:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AppDatabase db = Room.databaseBuilder(getApplicationContext(),
            AppDatabase.class, "database-name").build();

    Test test = new Test("test1", "test2");

    db.testDao().insertAll(test);
  }
}

However, project cannot be compiled and the error is:

Warning:warning: Supported source version 'RELEASE_7' from annotation processor 'android.arch.persistence.room.RoomProcessor' less than -source '1.8'

Error:Execution failed for task ':app:kaptDebugKotlin'. Internal compiler error. See log for more details

How can i setup this really simple example properly? I am stuck, looking for correct setup at this moment.

UPDATE:

When i change my Room setup in gradle like this:

//    // Room
//    implementation "android.arch.persistence.room:runtime:$roomVersion"
//    implementation "android.arch.persistence.room:rxjava2:$roomVersion"
//    kapt "android.arch.persistence.room:compiler:$roomVersion"

//room database
implementation "android.arch.persistence.room:runtime:1.0.0-alpha9"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha9"

The app now starts, but crash in the beginning and the exception is:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.roomtest/com.example.roomtest.MainActivity}: java.lang.RuntimeException: cannot find implementation for com.example.roomtest.AppDatabase. AppDatabase_Impl does not exist at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.RuntimeException: cannot find implementation for com.example.roomtest.AppDatabase. AppDatabase_Impl does not exist at android.arch.persistence.room.Room.getGeneratedImplementation(Room.java:90) at android.arch.persistence.room.RoomDatabase$Builder.build(RoomDatabase.java:440) at com.example.roomtest.MainActivity.onCreate(MainActivity.java:17) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)

Upvotes: 0

Views: 1911

Answers (2)

K.Os
K.Os

Reputation: 5496

The answer is to put this line into the gradle file:

compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.51"

The whole setup(Kotlin) stays the same, and the gradle look like this:

// Room
implementation "android.arch.persistence.room:runtime:$roomVersion"
implementation "android.arch.persistence.room:rxjava2:$roomVersion"
kapt "android.arch.persistence.room:compiler:$roomVersion"
compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.51"

Upvotes: 0

Fredy Mederos
Fredy Mederos

Reputation: 2636

This RuntimeError happens because room compiler could not generate the AppDatabase implementation.

I recommend you to change "compile" instruction to "implementation" in your gradle file. I think it could help.

I was reading again your gradle file and comparing it with mine and I don't see anywhere this line implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

Upvotes: 1

Related Questions