Android Studio中的Kotlin Room设置问题

时间:2017-10-09 20:22:32

标签: android kotlin android-room

我正努力在Android Studio中设置Room + Kotlin的简单示例,但这样做确实很痛苦。

我有类似的课程:

@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
}

我的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(项目)

// 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"
}

我尝试在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);
  }
}

但是,项目无法编译,错误是:

  

警告:警告:支持的源版本&#39; RELEASE_7&#39;来自注释处理器&#39; android.arch.persistence.room.RoomProcessor&#39;少于-source&#39; 1.8&#39;

     

错误:任务&#39;:app:kaptDebugKotlin&#39;执行失败。   内部编译器错误。有关更多详细信息,请参阅日志

如何正确设置这个非常简单的示例?我被卡住了,此刻正在寻找正确的设置。

更新:

当我在gradle中更改我的房间设置时:

//    // 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"

应用程序现在启动,但在开始时崩溃,异常是:

  

java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.roomtest / com.example.roomtest.MainActivity}:java.lang.RuntimeException:找不到com.example.roomtest.AppDatabase的实现。 AppDatabase_Impl不存在                                                                           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)                                                                           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)                                                                           在android.app.ActivityThread.-wrap11(ActivityThread.java)                                                                           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)                                                                           在android.os.Handler.dispatchMessage(Handler.java:102)                                                                           在android.os.Looper.loop(Looper.java:148)                                                                           在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                           at java.lang.reflect.Method.invoke(Native Method)                                                                           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)                                                                        引起:java.lang.RuntimeException:找不到com.example.roomtest.AppDatabase的实现。 AppDatabase_Impl不存在                                                                           在android.arch.persistence.room.Room.getGeneratedImplementation(Room.java:90)                                                                           在android.arch.persistence.room.RoomDatabase $ Builder.build(RoomDatabase.java:440)                                                                           在com.example.roomtest.MainActivity.onCreate(MainActivity.java:17)                                                                           在android.app.Activity.performCreate(Activity.java:6237)                                                                           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)                                                                           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)

2 个答案:

答案 0 :(得分:0)

发生此RuntimeError是因为会议室编译器无法生成AppDatabase实现。

我建议您在gradle文件中将“compile”指令更改为“implementation”。我认为这可能有所帮助。

我正在再次阅读你的gradle文件,并将其与我的比较,我没有看到这一行implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

答案 1 :(得分:0)

行。

所以答案是将这一行放入gradle文件中:

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

整个设置(Kotlin)保持不变,并且gradle看起来像这样:

   // 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"

希望这对某人有所帮助,我为此浪费了太多时间