skip to Main Content

App Background:

This is a simple kotlin MVVM app created in android studio that uses a room database to store the user’s data locally that is not fully complete I’m currently trying to run the app just to make sure when the app is ran, the app functions correctly however, following stack trace appears when the home activity is started:

2023-01-09 20:34:34.298 18579-18579/com.example.fridgeit2 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.fridgeit2, PID: 18579
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fridgeit2/com.example.fridgeit2.ui.HomeActivity}: java.lang.RuntimeException: cannot find implementation for com.example.fridgeit2.data.ItemDatabase. ItemDatabase_Impl does not exist
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.RuntimeException: cannot find implementation for com.example.fridgeit2.data.ItemDatabase. ItemDatabase_Impl does not exist
        at androidx.room.Room.getGeneratedImplementation(Room.java:100)
        at androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:1486)
        at com.example.fridgeit2.data.ItemDatabase$Companion.getInstance(ItemDatabase.kt:27)
        at com.example.fridgeit2.ui.HomeActivity.onCreate(HomeActivity.kt:23)
        at android.app.Activity.performCreate(Activity.java:7994)
        at android.app.Activity.performCreate(Activity.java:7978)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Not sure why its saying that the ItemDataBase does not exist when it clearly does:
https://imgur.com/a/BqZaD0a

ItemDatabase

package com.example.fridgeit2.data

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

@Database(
    entities = [Item::class],
    version = 1
)
abstract class ItemDatabase : RoomDatabase() {

    abstract val itemDAO : ItemDAO

    companion object{
        @Volatile
        private var INSTANCE : ItemDatabase? = null
        fun getInstance(context: Context):ItemDatabase{
            synchronized(this){
                var instance = INSTANCE
                if(instance==null){
                    instance = Room.databaseBuilder(
                        context.applicationContext,
                        ItemDatabase::class.java,
                        "item_database"
                    ).build()
                    INSTANCE = instance
                }
                return instance
            }
        }
    }
}

Android Manifest File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.fridgeit2">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.FridgeIt2"
        tools:targetApi="31">
        <activity
            android:name=".ui.HomeActivity"
            android:label="Home"
            android:exported="false" />
        <activity
            android:name=".ui.SplashScreenActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Build.gradle(Project) File

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.2.0' apply false
    id 'com.android.library' version '7.2.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Build.gradle(Module) File


plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

apply plugin: "kotlin-kapt"

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.example.fridgeit2"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildFeatures{
        viewBinding true
        dataBinding true
    }

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

dependencies {
    //ROOM
    def room_version = "2.4.3"

    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"

    // Architectural Components
    def lifecycle_version = "2.5.1"

    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"

    //Coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1'

    // Coroutine Lifecycle Scopes
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.5.1"

    // Retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.6.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.6.0'
    implementation "com.squareup.okhttp3:logging-interceptor:4.5.0"

    // Navigation Components
    implementation "androidx.navigation:navigation-fragment-ktx:2.5.3"
    implementation "androidx.navigation:navigation-ui-ktx:2.5.3"

    implementation 'androidx.core:core-ktx:1.8.0'
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

Home Activity

package com.example.fridgeit2.ui

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import com.example.fridgeit2.R
import com.example.fridgeit2.data.ItemDatabase


import com.example.fridgeit2.databinding.ActivityHomeBinding
import com.example.fridgeit2.repository.ItemRepository

class HomeActivity : AppCompatActivity() {
    private lateinit var binding : ActivityHomeBinding
    lateinit var itemViewModel: ItemViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this,R.layout.activity_home)

        val dao = ItemDatabase.getInstance(application).itemDAO
        val repository = ItemRepository(dao)
        val factory = ItemViewModelProviderFactory(repository)
        //itemViewModel = ViewModelProvider(this,factory).get(ItemViewModelProviderFactory::class.java))
        binding.itemViewModel = itemViewModel
        binding.lifecycleOwner = this
        displayItemList()

    }

    private fun displayItemList() {
        itemViewModel.items.observe(this, Observer {
            Log.i("MYTAG", it.toString())
        })
    }
}

Home Activity XML

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <data>
        <variable
            name="itemViewModel"
            type="com.example.fridgeit2.ui.ItemViewModel" />
    </data>
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.HomeActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvItems"
        android:layout_width="409dp"
        android:layout_height="729dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

If you need anything else please let me know, thanks in advance!

3

Answers


  1. Chosen as BEST ANSWER

    Solved issue by rewriting the build.gradle(app) file:

    plugins {
        id 'com.android.application'
        id 'org.jetbrains.kotlin.android'
        id 'kotlin-kapt'
    }
    
    
    
    android {
        compileSdk 32
    
        defaultConfig {
            applicationId "com.example.fridgeit2"
            minSdk 21
            targetSdk 32
            versionCode 1
            versionName "1.0"
    
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
    
        buildFeatures{
            dataBinding true
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
        kotlinOptions {
            jvmTarget = '1.8'
        }
    }
    
    dependencies {
    
        //ROOM
    
        def room_version = "2.4.3"
        implementation "androidx.room:room-runtime:$room_version"
        kapt "androidx.room:room-compiler:$room_version"
        implementation "androidx.room:room-ktx:$room_version"
    
        //Coroutines
        implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2'
        implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1'
    
        def lifecycle_version = "2.5.1"
        // ViewModel
        implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
        // LiveData
        implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
    
        //annotation processor
        implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
    
        // Navigation Components
        implementation "androidx.navigation:navigation-fragment-ktx:2.5.3"
        implementation "androidx.navigation:navigation-ui-ktx:2.5.3"
    
    
        implementation 'androidx.core:core-ktx:1.8.0'
        implementation 'androidx.appcompat:appcompat:1.5.1'
        implementation 'com.google.android.material:material:1.7.0'
        implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
        testImplementation 'junit:junit:4.13.2'
        androidTestImplementation 'androidx.test.ext:junit:1.1.5'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    }
    

    Also Rebuild your project first before running


  2. Use kapt instead of annotation processor .
    To use kapt , you need to add kapt plugin

    apply plugin: 'kotlin-kapt'
    
    Login or Signup to reply.
  3. Update this

    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
    

    to

    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search