ApiModule.kt
#Module
class ApiModule {
private val BASE_URL = "https://raw.githubusercontent.com"
#Provides
fun providesCountriesApi() : CountriesApi{
return Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
.create(CountriesApi::class.java)
}
#Provides
fun provideCountriesService(): CountriesService {
return CountriesService()
}
}
ApiComponent.kt
#Component(modules = [ApiModule::class])
interface ApiComponent {
fun inject(service: CountriesService)
fun inject(viewModel: ListViewModel)
}
CountriesService.kt
class CountriesService {
#Inject
lateinit var api: CountriesApi
init {
DaggerApiComponent.create().inject(this)
}
fun getCountries(): Single<List<Country>> {
return api.getCountries()
}
}
CountriesApi.kt
interface CountriesApi {
#GET("DevTides/countries/master/countriesV2.json")
fun getCountries(): Single<List<Country>>
}
gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.0"
defaultConfig {
applicationId "com.demo.kotlinandroidmaster"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation "androidx.recyclerview:recyclerview:1.0.0"
implementation "androidx.cardview:cardview:1.0.0"
implementation 'com.google.android.material:material:1.0.0-rc01'
implementation 'com.github.bumptech.glide:glide:3.7.0'
implementation 'info.androidramp:loading-gear:1.0.4'
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'com.google.dagger:dagger:2.21'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'io.reactivex.rxjava2:rxjava:2.1.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
implementation "androidx.room:room-runtime:2.0.0-rc01"
implementation "androidx.room:room-rxjava2:2.0.0-rc01"
implementation "androidx.lifecycle:lifecycle-extensions:2.0.0-rc01"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.0.0-rc01"
annotationProcessor "androidx.room:room-compiler:2.0.0-rc01"
annotationProcessor 'com.google.dagger:dagger-compiler:2.21'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Dagger component is not getting generated
try doing a build of your project, before running the app.
Dagger components are generated when a Build is done and removed when a Clean is done.
Edit
#Singleton
#Component(
modules = [AppModule::class, AndroidInjectionModule::class,
InjectionBinder::class,
AndroidSupportInjectionModule::class, InjectionBinder::class]
)
interface AppComponent {
#Component.Builder
interface Builder {
#BindsInstance
fun application(app: YourApplication): Builder
fun build(): AppComponent
}
fun inject(app: YourApplication)
}
here's a working example of an AppComponent
You are missing the annotation processor and the dagger compiler dependency.
In your build.gradle add after the other plugins:
apply plugin: 'kotlin-kapt'
and then, as dependency:
kapt 'com.google.dagger:dagger-compiler:2.21'
Side note: you are not using the latest version of Dagger.
Related
I have spent the hole week trying to add hilt dependency injection to my sample note application, android studio have been throwing on me error after an error.It got me mad, any way, in AppModule i have been trying to inject my room database to app repository and then my app repo to my use cases class and at the end injecting use cases class to my sharedViewModel
so this is my AppModule object:
#Module
#InstallIn(SingletonComponent::class)
object AppModule {
#Provides
#Singleton
fun provideNoteDatabase(app: Application): NoteDatabase {
return Room.databaseBuilder(
app,
NoteDatabase::class.java,
NoteDatabase.DATABASE_NAME
).build()
}
#Provides
#Singleton
fun provideNoteRepository(db: NoteDatabase): NotesRepo {
return RepoImplementation(db.noteDao())
}
#Provides
#Singleton
fun provideNoteUseCase(repo: NotesRepo): NoteUseCase {
return NoteUseCase(
getNotesUseCase = GetNotesUseCase(repo),
deleteNoteUseCase = DeleteNoteUseCase(repo),
updateNoteUseCase = UpdateNoteUseCase(repo),
insertNoteUseCase = InsertNoteUseCase(repo)
)
}
}
and this my Application class:
#HiltAndroidApp
class Application : Application()
my edit fragment:
#AndroidEntryPoint
class EditFragment : Fragment() {
private var _binding: FragmentEditBinding? = null
private val binding get() = _binding!!
private val viewModel: SharedViewModel by activityViewModels()
//...
}
my other fragment:
#AndroidEntryPoint
class MainFragment : Fragment() {
private var _binding: FragmentMainBinding? = null
private val binding get() = _binding!!
private val viewModel: SharedViewModel by activityViewModels()
//...
}
by the way also my MainActivity is annotated with #AndroidEntryPoint
my famous viewModel :
#HiltViewModel
class SharedViewModel #Inject constructor(private val noteUseCase: NoteUseCase) :
ViewModel() {...}
this is project level build.gradle:
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
def nav_version = "2.5.2"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.44'
}
}
plugins {
id 'com.android.application' version '7.3.0' apply false
id 'com.android.library' version '7.3.0' apply false
id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
}
and module level build.gradle:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-android'
id 'kotlin-kapt'
id "androidx.navigation.safeargs"
id 'com.google.dagger.hilt.android'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.stayin"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
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 {
dataBinding true
viewBinding true
}
namespace 'com.example.stayin'
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
def lifecycle_version = "2.4.1"
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
// coroutines for getting off the UI thread
def coroutines = "1.6.0"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines"
//shared preferences dependency
implementation 'androidx.preference:preference-ktx:1.2.0'
// Room dependency
def room_version = "2.4.3"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
// Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$room_version"
//navigation component dependency
implementation "androidx.navigation:navigation-fragment-ktx:2.5.2"
implementation "androidx.navigation:navigation-ui-ktx:2.5.2"
//Dagger - Hilt
implementation 'com.google.dagger:hilt-android:2.44'
kapt 'com.google.dagger:hilt-compiler:2.44'
// For instrumentation tests
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.44'
kaptAndroidTest 'com.google.dagger:hilt-compiler:2.44'
// For local unit tests
testImplementation 'com.google.dagger:hilt-android-testing:2.44'
kaptTest 'com.google.dagger:hilt-compiler:2.44'
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
}
if can any one help me to find what is wrong and explained why, i will be so thankful towards him. i rally need to pass this so i can level up in my career.
Remove below deprecated dependency:
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
(It was deprecated since dagger-2.34 version)
proof:
https://github.com/google/dagger/releases/tag/dagger-2.34
Also Try to upgrade your lifecycle version as below:
def lifecycle_version = "2.5.1"
add below lines after dependency section in build.gradle(app):
kapt { correctErrorTypes true }
follow official documentation:
https://developer.android.com/training/dependency-injection/hilt-android
https://dagger.dev/hilt/view-model.html
Please help. I use room. But I have such a mistake. If you remove this code
#Database(
entities = [Current::class],
version = 1
)
But I definitely need it to be in my code otherwise everything will be wrong.
This my ForecastDatabase.kt
package com.ggenius.whattowearkotlin.data.db
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.ggenius.whattowearkotlin.data.db.entity.Current
#Database(
entities = [Current::class],
version = 1
)
abstract class ForecastDatabase : RoomDatabase() {
abstract fun currentWeatherDao(): CurrentWeatherDao
companion object {
#Volatile private var instance: ForecastDatabase? = null
private val LOCK = Any()
operator fun invoke(context: Context) = instance ?: synchronized(LOCK) {
instance ?: buildDatabase(context).also { instance = it }
}
private fun buildDatabase(context: Context) =
Room.databaseBuilder(context.applicationContext,
ForecastDatabase::class.java, "forecast.db")
.build()
}
}
Its my gradle. Check Room
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
id 'kotlin-kapt'
id 'androidx.navigation.safeargs'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.ggenius.whattowearkotlin"
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
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 {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleDependency
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.4.1'
// Navigation
implementation "android.arch.navigation:navigation-fragment:$navigation_version"
implementation "android.arch.navigation:navigation-ui:$navigation_version"
implementation "android.arch.navigation:navigation-fragment-ktx:$navigation_version"
implementation "android.arch.navigation:navigation-ui-ktx:$navigation_version"
implementation "androidx.core:core-ktx:1.7.0"
implementation "androidx.constraintlayout:constraintlayout:2.1.3"
// Room
//noinspection GradleDependency
implementation "androidx.room:room-runtime:2.4.0-beta01"
implementation "androidx.legacy:legacy-support-v4:1.0.0"
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
kapt "androidx.room:room-compiler:$room_version"
// Gson
implementation "com.google.code.gson:gson:2.8.6"
// Kotlin Android Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0"
// Retrofit
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2'
// ViewModel
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
//noinspection GradleDependency
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
//noinspection LifecycleAnnotationProcessorWithJava8
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
// Kodein
implementation "org.kodein.di:kodein-di-generic-jvm:$kodein_version"
implementation "org.kodein.di:kodein-di-framework-android-x:$kodein_version"
// Better dateTime-time support even on older Android versions
implementation "com.jakewharton.threetenabp:threetenabp:1.1.0"
// Glide
implementation 'com.github.bumptech.glide:glide:4.11.0'
kapt 'com.github.bumptech.glide:compiler:4.11.0'
// Groupie RecyclerView
implementation 'com.xwray:groupie:2.7.0'
implementation 'com.xwray:groupie-kotlin-android-extensions:2.7.0'
// Preference
implementation "androidx.preference:preference:1.1.1"
// WeatherLocation
implementation "com.google.android.gms:play-services-location:19.0.1"
// New Material Design
implementation "com.google.android.material:material:1.5.0"
//test
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
In this screenshot, there is an error due to which my application does not start
enter image description here
You don't appear to have implementation 'androidx.room:room-ktx:2.4.0' in the build gradle.
I have watched and read many tutorial on How to use Room persistence library with coroutines but whenever I use coroutine in my file it forces me to annotate my code with #InternalCoroutineApi but in the tutorial they don't need to annotate anything.
Now I'm wanted to know
1. What does this annotation means?
2. Why it is necessary?
3. How can I avoid this?
Even this answer doesn't help me either.
below is the how I created my data base and my build.gradle file
import android.content.Context
import androidx.room.Database
import androidx.room.Entity
import androidx.room.Room
import androidx.room.RoomDatabase
import kotlinx.coroutines.InternalCoroutinesApi
import kotlinx.coroutines.internal.synchronized
#Database(entities = [Post::class], version = 1, exportSchema = false)
public abstract class PostRoomDatabase: RoomDatabase() {
abstract fun getDao(): PostDao
companion object{
//Companion object provide the same functionality as the Static keyword in java
#Volatile
private var DATABASE_INSTANCE :PostRoomDatabase? = null
#InternalCoroutinesApi
fun getDatabase(context: Context) : PostRoomDatabase{
val tempInstance = DATABASE_INSTANCE
if(tempInstance != null){
return tempInstance
}
synchronized(this){
val instance = Room.databaseBuilder(
context.applicationContext,
PostRoomDatabase::class.java,
"post_database"
).build()
DATABASE_INSTANCE = instance;
return instance
}
}
}
}
Build.gradle file
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 28
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "vijay.bhadolia.seed"
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
// To inline the bytecode built with JVM target 1.8 into
// bytecode that is being built with JVM target 1.6. (e.g. navArgs)
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
def room_version = "2.2.5"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.navigation:navigation-fragment:2.2.2'
implementation 'androidx.navigation:navigation-ui:2.2.2'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.2.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.2.2'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//Circular images
implementation 'de.hdodenhof:circleimageview:3.1.0'
//Room database
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
def coroutines_version = "1.3.0"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
}
Your imports say:
import kotlinx.coroutines.internal.synchronized
Which is the internal API that it is complaining about. You should not be importing anything for the synchronized keyword, so simply remove that import line.
I want to inject dependencies in android tests using dagger.
I've created a simple App with an AppComponent extending AndroidInjector.
That works for the App itself (the code is built in /build by dagger).
Then, for /test I've also created the AppComponentTest and AppTest.
It took me some time to realised that I need to create a naive test and run it to let gradle create the /build for test.
Now the code is there, but there is some error with that generated code, as you can see here:
AppComponent.kt
#Singleton
#Component(
modules = [
AndroidSupportInjectionModule::class
]
)
interface AppComponent : AndroidInjector<App> {
#Component.Factory
interface Factory {
fun create(#BindsInstance application: Application): AppComponent
}
}
App.kt
open class App : Application(), HasAndroidInjector {
#Inject
lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Any>
override fun androidInjector(): AndroidInjector<Any> = dispatchingAndroidInjector
override fun onCreate() {
super.onCreate()
setupDagger()
}
#VisibleForTesting
open fun setupDagger() {
DaggerAppComponent
.factory()
.create(this)
.inject(this)
}
}
Then under the same structure but in /test I've created this:
AppComponentTest.kt
#Singleton
#Component(
modules = [
AndroidSupportInjectionModule::class
]
)
interface AppComponentTest : AppComponent {
#Component.Factory
interface Factory {
fun create(
#BindsInstance application: Application
): AppComponentTest
}
}
AppTest.kt
class AppTest : App() {
override fun setupDagger() {
DaggerTestAppComponent
.factory()
.create(this)
.inject(this)
}
}
And this is the build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.dagger"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
testOptions {
unitTests {
includeAndroidResources = true
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
def dagger_version = "2.27"
implementation "com.google.dagger:dagger-android:$dagger_version"
implementation "com.google.dagger:dagger-android-support:$dagger_version"
kapt "com.google.dagger:dagger-android-processor:$dagger_version"
kapt "com.google.dagger:dagger-compiler:$dagger_version"
testImplementation 'junit:junit:4.13'
kaptTest "com.google.dagger:dagger-compiler:$dagger_version"
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Also the naive test:
ExampleUnitTest.kt
#RunWith(JUnit4::class)
class ExampleUnitTest {
#Test
fun test() {
assert(true)
}
}
I've also tried with other approach using this:
AppComponentTest2.kt
#Singleton
#Component(modules = [])
interface AppComponentTest2 {
fun inject(test: ExampleUnitTest2)
}
ExampleUnitTest2.kt
#RunWith(JUnit4::class)
class ExampleUnitTest2 {
#Test
fun test() {
DaggerAppComponentTest2
.builder()
.build()
.inject(this)
assert(true)
}
}
And also the same error:
Btw, this is a repo with that code:
https://gitlab.com/fitu/dagger
There are two branches there first_approach and second_approach
EDIT 1:
I had other repo where I was able to do this injection and now I've tried and it doesn't work.
Comparing dependencies I have:
New project:
classpath 'com.android.tools.build:gradle:3.6.1'
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.61'
implementation 'com.google.dagger:dagger-android:2.27'
implementation 'com.google.dagger:dagger-android-support:2.27'
kapt 'com.google.dagger:dagger-android-processor:2.27'
kapt 'com.google.dagger:dagger-compiler:2.27'
kaptTest 'com.google.dagger:dagger-compiler:2.27'
Old project:
classpath 'com.android.tools.build:gradle:3.2.0'
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.70'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.61'
implementation'com.google.dagger:dagger-android-support:2.14.1'
kapt 'com.google.dagger:dagger-compiler:2.14.1'
kaptTest 'com.google.dagger:dagger-compiler:2.14.1'
So it seems is not a dagger or kotlin version.
Maybe it's related with Android Studio?
I'm not sure about the version of AS when I'd created that old project, but my current AS version is: 3.6.1
EDIT 2:
I've deleted almost everything in the project, the main folder with App and everything:
The only reminding was two classes: AppComponentTest.kt and ExampleUnitTest.kt
#Singleton
#Component(modules = [])
interface AppComponentTest
class ExampleUnitTest {
#Test
fun test() {
DaggerAppComponentTest
.builder()
.build()
assert(true)
}
}
The error persist, so I'm guessing the problem could be a combinations of AS version, Kotlin version and/or Dagger version.
I am trying to use Room in my project. Gradle syncing files well, but I get RunitomeException when trying to get database instance.
"Caused by: java.lang.RuntimeException: cannot find implementation for com.fillooow.android.testtochka.BusinessLogic.database.GithubUserSearchDataBase. GithubUserSearchDataBase_Impl does not exist"
I searched this issue and find that solution is to add this lines into build.gradle file:
implementation "android.arch.persistence.room:runtime:1.1.1"
implementation "android.arch.persistence.room:rxjava2:1.1.1"
kapt "android.arch.persistence.room:compiler:1.1.1"
and also aply this plugin
apply plugin: 'kotlin-kapt'
But this is my build.gradle file, and I still have this issue:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.fillooow.android.testtochka"
minSdkVersion 21
targetSdkVersion 28
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 fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-v4:28.0.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.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.jakewharton.rxbinding2:rxbinding-kotlin:2.0.0'
implementation 'com.google.android.gms:play-services-auth:16.0.1'
implementation 'com.facebook.android:facebook-android-sdk:[4,5)'
implementation 'com.vk:androidsdk:1.6.9'
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation "android.arch.persistence.room:runtime:1.1.1"
implementation "android.arch.persistence.room:rxjava2:1.1.1"
kapt "android.arch.persistence.room:compiler:1.1.1"
}
And this is DataBase class
import android.arch.persistence.room.Room
import android.arch.persistence.room.RoomDatabase
import android.content.Context
abstract class GithubUserSearchDataBase : RoomDatabase(){
abstract fun githubUserSearchDataDao(): GithubUserSearchDataDao
companion object {
private var INSTANCE: GithubUserSearchDataBase? = null
fun getInstance(context: Context): GithubUserSearchDataBase?{
if (INSTANCE == null){
synchronized(GithubUserSearchDataBase::class){
INSTANCE = Room.databaseBuilder(context.applicationContext,
GithubUserSearchDataBase::class.java,
"github.db")
.build()
}
}
return INSTANCE
}
fun destroyInstance(){
INSTANCE = null
}
}
}
Project were cleared and rebuild a lot of times.
So, maybe I missed something?
Your gradle file looks fine. Just be sure to Sync it after you have added the proper imports.
What you are missing is the #Database annotation on top of your Database class.
#Database(entities = [Entity1::class, Entity2::class, Entity3::class, Entity4::class], version = 1)
abstract class GithubUserSearchDataBase : RoomDatabase(){
abstract fun githubUserSearchDataDao(): GithubUserSearchDataDao
companion object {
private var INSTANCE: GithubUserSearchDataBase? = null
fun getInstance(context: Context): GithubUserSearchDataBase?{
if (INSTANCE == null){
synchronized(GithubUserSearchDataBase::class){
INSTANCE = Room.databaseBuilder(context.applicationContext,
GithubUserSearchDataBase::class.java,
"github.db")
.build()
}
}
return INSTANCE
}
fun destroyInstance(){
INSTANCE = null
}
}
}
In the entities attribute of the #Database annotation you must put an array with all the classes of your model annotated with the #Entity annotation. I put there fake names, you should put the proper ones.