在kotlin项目中使用空间持久性库时编译错误

时间:2018-06-07 09:56:05

标签: android kotlin android-room

我好几天都遇到了问题。 我正在使用Kotlin和房间持久性库开发一个应用程序。我的数据库由4个实体组成。问题是每当我添加 kapt "android.arch.persistence.room:compiler:$room_version"无论房间版本如何,我都会收到以下编译错误和警告:

C:\Users\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib-jre7\1.2.0\ec8b969e26fbcf2265a4d1a1539c4d1d4c5af380\kotlin-stdlib-jre7-1.2.0.jar: kotlin-stdlib-jre7 is deprecated. Please use kotlin-stdlib-jdk7 instead	

C:\Users\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib-jre8\1.2.0\505f55b9619bbc5f5e26c77427dd24a6a441eef1\kotlin-stdlib-jre8-1.2.0.jar: kotlin-stdlib-jre8 is deprecated. Please use kotlin-stdlib-jdk8 instead	

C:\Users\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib-jre7\1.2.0\ec8b969e26fbcf2265a4d1a1539c4d1d4c5af380\kotlin-stdlib-jre7-1.2.0.jar: kotlin-stdlib-jre7 is deprecated. Please use kotlin-stdlib-jdk7 instead	

C:\Users\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib-jre8\1.2.0\505f55b9619bbc5f5e26c77427dd24a6a441eef1\kotlin-stdlib-jre8-1.2.0.jar: kotlin-stdlib-jre8 is deprecated. Please use kotlin-stdlib-jdk8 instead	

C:/Users/Desktop/Meals_App	
app/build/tmp/kapt3/stubs/debug/appDatabase/appDao.java	
error: cannot find symbol
    public abstract java.util.List<appDatabase.recipesEntity> getAll();
                                              ^
  symbol:   class recipesEntity
  location: class appDatabase	

error: cannot find symbol
    appDatabase.recipesEntity recipes);
               ^
  symbol:   class recipesEntity
  location: class appDatabase	

error: cannot find symbol
    appDatabase.chefEntity chefs);
               ^
  symbol:   class chefEntity
  location: class appDatabase	

error: cannot find symbol
    appDatabase.recipesEntity recipes);
               ^
  symbol:   class recipesEntity
  location: class appDatabase	
app/build/tmp/kapt3/stubs/debug/appDatabase/appDatabase.java	

error: cannot find symbol
    public abstract appDatabase.appDao appDao();
                               ^
  symbol:   class appDao
  location: class appDatabase	

error: cannot find symbol
@android.arch.persistence.room.Database(entities = {appDatabase.recipesEntity.class, appDatabase.chefEntity.class, appDatabase.stepsEntity.class, appDatabase.ingredientEntity.class}, version = 1, exportSchema = false)
                                                                                                ^
  symbol:   class chefEntity
  location: class appDatabase	

error: cannot find symbol
@android.arch.persistence.room.Database(entities = {appDatabase.recipesEntity.class, appDatabase.chefEntity.class, appDatabase.stepsEntity.class, appDatabase.ingredientEntity.class}, version = 1, exportSchema = false)
                                                                                                                                                             ^
  symbol:   class ingredientEntity
  location: class appDatabase	

error: android.arch.persistence.room.RoomProcessor was unable to process this class because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
public abstract class appDatabase extends android.arch.persistence.room.RoomDatabase {
                ^	
app/build/tmp/kapt3/stubs/debug/appDatabase/ingredientEntity.java	

error: cannot find symbol
    public final appDatabase.ingredientEntity copy(int ingID, int recipeID, @org.jetbrains.annotations.NotNull()
                            ^
  symbol:   class ingredientEntity
  location: class appDatabase	
app/build/tmp/kapt3/stubs/debug/appDatabase/stepsEntity.java	

error: cannot find symbol
    public final appDatabase.stepsEntity copy(int stepID, int recipeID, @org.jetbrains.annotations.NotNull()
                            ^
  symbol:   class stepsEntity
  location: class appDatabase	
app/build/tmp/kapt3/stubs/debug/appDatabase/recipesEntity.java	

error: cannot find symbol
@android.arch.persistence.room.Entity(tableName = "recipesEntity", foreignKeys = {@android.arch.persistence.room.ForeignKey(entity = appDatabase.chefEntity.class, childColumns = {"chefID"}, onDelete = 5, parentColumns = {"chefID"})})
                                                                                                                                                ^
  symbol:   class chefEntity
  location: class appDatabase	

error: android.arch.persistence.room.RoomProcessor was unable to process this class because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
public final class recipesEntity {
             ^	
app/build/tmp/kapt3/stubs/debug/appDatabase/chefEntity.java	

error: android.arch.persistence.room.RoomProcessor was unable to process this class because not all of its dependencies could be resolved. Check for compilation errors or a circular dependency with generated code.
public final class chefEntity {
             ^	

以下是我的数据库类: ChefEntity.kt

@Entity(tableName = "chefEntity", indices = [(Index(value = ("username"), unique = true))])

data class chefEntity(
        @PrimaryKey(autoGenerate = true)
        @ColumnInfo(name = "chef_id")
        var chefID: Int,

        @ColumnInfo(name = "firstname")
        var firstname: String,

        @ColumnInfo(name = "lastname")
        var lastname: String,

        @ColumnInfo(name = "username")
        var username: String,

        @ColumnInfo(name = "password")
        var password: String,

        @ColumnInfo(name = "address")
        var address: String,

        @ColumnInfo(name = "mobile")
        var mobile: String,

        @ColumnInfo(name = "bg_color")
        var bg_color: Int
 )

recipesEntity.kt

@Entity(tableName = "recipesEntity",
        foreignKeys = [(ForeignKey(entity = chefEntity::class,
                parentColumns = [("chefID")],
                childColumns = [("chefID")],
                onDelete = ForeignKey.CASCADE))])

data class recipesEntity(
        @PrimaryKey(autoGenerate = true)
        @ColumnInfo(name = "recipeID")
        val recipeID: Int,

        @ColumnInfo(name = "chef_id")
        val chefID: Int,

        @ColumnInfo(name = "name")
        val name: String,

        @ColumnInfo(name = "prep_time")
        val pTime: String,

        @ColumnInfo(name = "image_name")
        val imageName: String
)

ingredientEntity.kt

@Entity(tableName = "ingredientEntity",
        foreignKeys = [(ForeignKey(entity = recipesEntity::class,
                parentColumns = [("recipeID")],
                childColumns = [("recipeID")],
                onDelete = ForeignKey.CASCADE))])

data class ingredientEntity(
        @PrimaryKey(autoGenerate = true)
        @ColumnInfo(name = "ingredient_id")
        var ingID: Int,

        @ColumnInfo(name = "recipeID")
        var recipeID: Int,

        @ColumnInfo(name = "ingredient_name")
        var name: String
)

stepsEntity.kt

@Entity(tableName = "stepsEntity",
        foreignKeys = [(ForeignKey(entity = recipesEntity::class,
                parentColumns = [("recipeID")],
                childColumns = [("recipeID")],
                onDelete = ForeignKey.CASCADE))])

data class stepsEntity(
        @PrimaryKey()
        @ColumnInfo(name = "step_id")
        val stepID: Int,

        @ColumnInfo(name = "recipeID")
        val recipeID: Int,

        @ColumnInfo(name = "desciption")
        val desciption: String,

        @ColumnInfo(name = "order")
        val order: Int
)

appDatabase

@Database(entities = [(recipesEntity::class), (chefEntity::class), (stepsEntity::class), (ingredientEntity::class)],
        version = 1, exportSchema = false)

abstract class appDatabase : RoomDatabase() {
    abstract fun appDao(): appDao
}

appDao

@Dao
interface appDao {

    @Query("SELECT * from recipesEntity")
    fun getAll(): List<recipesEntity>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(recipes: recipesEntity)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(chefs: chefEntity)

    @Update
    fun update(recipes: recipesEntity)

    @Query("DELETE from recipesEntity")
    fun deleteAll()

}

模块:应用

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: "kotlin-kapt"

apply plugin: 'kotlin-android-extensions'


android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.sawsan.mealsapp"
        minSdkVersion 17
        targetSdkVersion 27
        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 'com.android.tools.build:gradle:3.1.2'
    implementation "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.41"
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    implementation 'com.pes.materialcolorpicker:library:1.2.0'
    implementation 'com.afollestad.material-dialogs:core:0.9.6.0'
    implementation 'com.afollestad.material-dialogs:commons:0.9.6.0'

    //Room

    def room_version = "1.1.1-rc1" // or, for latest rc, use "1.1.1-rc1"

    implementation "android.arch.persistence.room:runtime:$room_version"
    annotationProcessor "android.arch.persistence.room:compiler:$room_version"
    kapt "android.arch.persistence.room:compiler:$room_version"

    // Extensions = ViewModel + LiveData
    implementation "android.arch.lifecycle:extensions:1.1.1"
    kapt "android.arch.lifecycle:compiler:1.1.1"

    //RxJava support for Room
    implementation "android.arch.persistence.room:rxjava2:$room_version"

    //RxJava
    implementation "io.reactivex.rxjava2:rxkotlin:2.1.0"
    implementation "io.reactivex.rxjava2:rxjava:2.1.3"
    implementation "io.reactivex.rxjava2:rxandroid:2.0.1"
}

我希望有人可以帮我解决这个问题,因为我在网上找不到任何类似的案例。 抱歉这么久,谢谢你。

1 个答案:

答案 0 :(得分:0)

我遇到了这个问题,请确保您的文件扩展名正确,例如,我有一个名为AccreditationLogo而不是AccreditationLogo.kt的文件。这似乎使编译器有些奇怪。