0

After I launched the application, UserDao_impl and AppDatabase_impl generated by kapt automatically under builder folder reported the error. It says that Cannot resolve symbol 'Generated'

Although it doesn't affect the effect of running because I can normally insert and query data, I'm wondering whether it will cause deep error in the future.

The android docs of Room 2.2.4 says that:

Fixed an issue with the @Generated being wrongly used when building with JDK 9. (b/146538330)

It seemed that it was once a bug to wrongly generate code with @Generate annotation on Java 9 or above. However, I don't know why @Generated annotation occurs again if the bug is fixed perfectly.

My Gradle JDK uses version 17, and my data with config files are as follows:

// User_.kt

@Entity(tableName = "users")
data class User_(
    @ColumnInfo(name = "username") val username: String,
    @ColumnInfo(name = "password") val password: String,
) {
    @PrimaryKey(autoGenerate = true) var id: Int = 0

    override fun toString(): String {
        return "${this::class.simpleName}(id: $id, username: $username, password, $password)"
    }
}

// UserDao.kt

@Dao
interface UserDao {
    @Query("SELECT * FROM users")
    fun getAll(): List<User_>

    @Query("SELECT * FROM users WHERE id = :id")
    fun get(id: Int): User_

    @Query("SELECT * FROM users WHERE username = :username LIMIT 1")
    fun findByName(username: String): User_?
    //

    @Insert
    fun insert(user: User_)

    @Delete
    fun delete(user: User_)
}

// AppDatabase.kt

@Database(entities = [User_::class], version = 1, exportSchema = false)
abstract class AppDatabase: RoomDatabase() {
    abstract fun userDao(): UserDao

    companion object {
        @Volatile
        private var INSTANCE: AppDatabase? = null


        fun getInstance(context: Context): AppDatabase {
            return INSTANCE?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    AppDatabase::class.java,
                    "AppDatabase"
                ).allowMainThreadQueries()
                    .build()
                INSTANCE = instance
                instance
            }
        }
    }
}

// project level build.gradle
plugins {
    id 'com.android.application' version '8.0.2' apply false
    id 'com.android.library' version '8.0.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.20' apply false
}

// build.gradle(Module: app)
plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'
    id 'kotlin-android'
}

android {
    namespace 'com.example.home'
    compileSdk 33

    //
    defaultConfig {
        applicationId "com.example.home"
        minSdk 23
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    android.applicationVariants.all {
        variant ->
            variant.outputs.all {
                outputFileName = "happyDog.apk"
            }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = '17'
    }
}

dependencies {
    def nav_version = "2.5.3"
    def room_version = "2.4.3"
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

    implementation "androidx.room:room-runtime:$room_version"
//    annotationProcessor "androidx.room:room-compiler:$room_version"
    // To use Kotlin annotation processing tool (kapt)
    kapt "androidx.room:room-compiler:$room_version"

    implementation 'org.bouncycastle:bcpkix-jdk15on:1.68'
    implementation 'org.bouncycastle:bcprov-jdk15on:1.68'

    implementation "androidx.media3:media3-exoplayer:1.1.0"
    implementation "androidx.media3:media3-exoplayer-dash:1.1.0"
    implementation "androidx.media3:media3-ui:1.1.0"

    implementation 'com.google.code.gson:gson:2.10.1'

    implementation 'androidx.fragment:fragment-ktx:1.5.7'
    implementation 'androidx.core:core-ktx:1.8.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'com.github.bumptech.glide:glide:3.7.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

Kartone
  • 31
  • 5

1 Answers1

0

Oh, well, it suddenly occurs to me that I had modified targetCompatibility JavaVersion in build.gradle(Module: app) to 17 before. After I changed java version in build.gradle(Module: app):

...
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = '1.8'
}
...

The annoying @Generated annotation then disappeared.

Kartone
  • 31
  • 5
  • Sorry, the answer is wrong, because when i run the app, it will report `compileDebugJavaWithJavac' task (current target is 1.8) and 'kaptGenerateStubsDebugKotlin' task (current target is 17) jvm target compatibility should be set to the same Java version.` I have to change java version to 17 again. – Kartone Aug 01 '23 at 13:56