Issue
I have looked around here and other sites, but I can't figure out the solution.
everything worked fine till I initialized the RoomViewModel.kt in MainActivity.kt.
I keep getting this error
java.lang.RuntimeException: cannot find implementation for com.caller.fakecaller.data.DataBase. DataBase_Impl does not exist
at androidx.room.Room.getGeneratedImplementation(Room.java:97)
at androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:1358)
at com.caller.fakecaller.data.DataBase$Companion.getInstance(DataBase.kt:31)
at com.caller.fakecaller.data.RoomViewModel.<init>(RoomViewModel.kt:18)
at com.caller.fakecaller.util.RoomViewModelFactory.create(RoomViewModelFactory.kt:14)...
I have this gradle
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.caller.fakecaller"
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary 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'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.0.2'
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.6.0'
implementation "androidx.compose.ui:ui:1.0.2"
implementation "androidx.compose.material:material:1.0.2"
implementation "androidx.compose.ui:ui-tooling-preview:1.0.2"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.3.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:1.0.2"
debugImplementation "androidx.compose.ui:ui-tooling:1.0.2"
//Room
implementation "androidx.room:room-runtime:2.3.0"
annotationProcessor "androidx.room:room-compiler:2.3.0"
// optional - Kotlin Extensions and Coroutines support for Room
implementation("androidx.room:room-ktx:2.3.0")
// Lifecycle components
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.1"
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation "androidx.lifecycle:lifecycle-common-java8:2.3.1"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
//Navigation with Compose
implementation("androidx.navigation:navigation-compose:2.4.0-alpha08")
//Preferences
implementation("androidx.preference:preference-ktx:1.1.1")
//Activity
implementation 'androidx.activity:activity-ktx:1.4.0-alpha01'
//Glide Accompanist
implementation "dev.chrisbanes.accompanist:accompanist-glide:0.4.2"
}
when i added the plugins kotlin-android and kotlin-kapt it said that they weren't found.
therefore i had to change the app gradle to be like this (based on other posts)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
task clean(type: Delete) {
delete rootProject.buildDir
}
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30"
}
}
allprojects {
configurations.all {
resolutionStrategy {
cacheChangingModulesFor 0, 'seconds'
}
}
}
but that gave me these errors
com/android/build/gradle/BaseExtension
> com.android.build.gradle.BaseExtension
Unable to load class 'com.android.build.gradle.BaseExtension'.
in the database file I did as follows:
private const val DATABASE_NAME = "FakeCaller"
@Database(entities = [Contact::class, RingTone::class], version = 1, exportSchema = false)
abstract class DataBase:RoomDatabase() {
abstract fun UserDao(): UserDao
abstract fun RingDao():RingDao
//Implementing the DataBase
companion object {
@Volatile
private var INSTANCE: DataBase? = null
fun getInstance(context: Context): DataBase {
synchronized(this) {
var instance = INSTANCE
if (instance == null) {
instance = Room.databaseBuilder(
context.applicationContext,
DataBase::class.java,
DATABASE_NAME
).build()
INSTANCE = instance
}
return instance
}
}
}
}
and connected everything to the RoomViewModel (I created a factory for it also)
class RoomViewModel(application: Application) : AndroidViewModel(application) {
val readContactList : LiveData<List<Contact>>
val readFileUri: LiveData<Uri>
private val repository : Repository
init {
val dbInstance = DataBase.getInstance(application)
val userDao = dbInstance.UserDao()
val ringDao = dbInstance.RingDao()
repository = Repository(userDao, ringDao)
readContactList = repository.getContacts
readFileUri = repository.getUri()
}
suspend fun getContact(id:Long): LiveData<Contact>{
return repository.getContact(id)
}
...
}
in MainActivity I made this
setContent {
FakeCallerTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
val context = LocalContext.current
val roomViewModel: RoomViewModel = viewModel(
factory = RoomViewModelFactory(context.applicationContext as Application)
)
Greeting("Android")
}
i think there is an issue with the context but i don't know really. still learning...
please help
Solution
This should help.
Modern day approach:
inside the plugins block,
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt' //Add this
}
Then, withing dependencies,
kapt "androidx.room:room-compiler:$rootProject.roomVersion"
Well based on the comment below,
These are all that I have needed to build fine.
implementation "androidx.room:room-ktx:$rootProject.roomVersion"
kapt "androidx.room:room-compiler:$rootProject.roomVersion"
androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"
Hey wait, are you connected to the internet? These may need to be downloaded from the web by the studio (downloads automatically). If download does not start, please check whether gradle is configured for offline work. If it is, toggle the state and it will download.
Per the comment below, this seems to have solved the issue
project-level-build{
dependencies{
classpath 'com.android.tools.build:gradle:7.1.0-alpha04'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Answered By - MARSK
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.