2017-10-09 66 views
0

我正在努力在Android Studio中設置房間+ Kotlin的簡單示例,但這確實很痛苦。Android Studio中的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); 
    } 
} 

然而,項目不能被編譯和錯誤是:

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

如何正確設置這個非常簡單的例子?我被卡住了,此刻正在尋找正確的設置。

UPDATE:

當我改變了我的房間設置在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: 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)

+0

看看你在Android Studio中的'Gradle Console'面板。通常在那裏有更多的錯誤日誌 – StuStirling

+0

@StuStirling我在我的問題做了更新,也許現在你可以幫忙? – Konrad

+0

將'annotationProcessor'更改爲'kapt' – StuStirling

回答

0

發生RuntimeError是因爲房間編譯器無法生成AppDatabase實現。

我建議你在gradle文件中將「compile」指令改爲「implementation」。我認爲這可能會有所幫助。

我再次閱讀您的gradle這個文件並把它與我相比,我沒有看到任何地方這條線implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

+0

謝謝,但沒有奏效 – Konrad

+0

更改AppDatabase類科特林而不是Java,然後再試一次 –

+0

我做了一個更新我的問題,也許現在你可以幫助?我也將AppDatabase轉換爲kotlin – Konrad

0

確定。

所以答案是,把此行到gradle這個文件:

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

整個安裝(科特林)保持不變,和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" 

希望這幫助別人,我失去了太多的時間