我正在努力在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)
看看你在Android Studio中的'Gradle Console'面板。通常在那裏有更多的錯誤日誌 – StuStirling
@StuStirling我在我的問題做了更新,也許現在你可以幫忙? – Konrad
將'annotationProcessor'更改爲'kapt' – StuStirling