Android와 연결되지 않은 AndroidStudio에서 모듈을 만들려고하는데 아무런 활동이 없지만 방 데이터베이스와 같은 여러 가지 컨텍스트가 필요합니다. 여기 Kotlin : Dagger2 @Inject context var is always null
내 설정이다 :AppComponent
@Singleton
@Component(modules = arrayOf(AndroidSupportInjectionModule::class, AppModule::class))
interface AppComponent : AndroidInjector<NexoApplication> {
@Component.Builder
interface Builder {
@BindsInstance
fun application(application: NexoApplication): Builder
fun build(): AppComponent
}
override fun inject(app: NexoApplication)
}
AppModule
@Module
class AppModule {
@Singleton @Provides
fun provideLogger(application: NexoApplication) = LogNexoManager(application)
}
AppLifecycleCallbacks
interface AppLifecycleCallbacks {
fun onCreate(application: Application)
fun onTerminate(application: Application)
}
는 앱
class NexoApplication: DaggerApplication() {
@Inject lateinit var appLifecycleCallbacks: AppLifecycleCallbacks
override fun applicationInjector() = DaggerAppComponent.builder()
.application(this)
.build()
override fun onCreate() {
super.onCreate()
appLifecycleCallbacks.onCreate(this)
}
override fun onTerminate() {
appLifecycleCallbacks.onTerminate(this)
super.onTerminate()
}
}
의 AndroidManifest.xml
<?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.elstatgroup.elstat">
<application
android:name="com.elstatgroup.elstat.NexoApplication"
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true">
</application>
그리고 난 이렇게 내 주요 클래스에 컨텍스트를 주입하려고 :
class LogNexoManager(app: Application){
var logRepository: LogRepository
init {
logRepository = LogRepositoryImpl(app)
}
}
샘플 단위 테스트는 항상
@Test
fun proceedWithLogs(){
val logManager = LogManager()
}
거짓 그리고 예외입니다 :
kotlin.UninitializedPropertyAccessException: lateinit property app has not been initialized
UPDATE : 내가 @Emanuel S에 의해 제안 된 변경 한 지금은 오류 등이있다 :
Error:Execution failed for task ':nexo:kaptDebugKotlin'. Internal compiler error. See log for more details
내 Build.gradle 파일은 다음과 같습니다.
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 26
defaultConfig {
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
ext.daggerVersion = '2.11'
ext.roomVersion = '1.0.0-alpha9'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
// RxJava
implementation 'io.reactivex.rxjava2:rxjava:2.1.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
// Room
implementation "android.arch.persistence.room:runtime:$roomVersion"
implementation "android.arch.persistence.room:rxjava2:$roomVersion"
kapt "android.arch.persistence.room:compiler:$roomVersion"
compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.51"
androidTestImplementation "android.arch.persistence.room:testing:$roomVersion"
compile "com.google.dagger:dagger:$daggerVersion"
compile "com.google.dagger:dagger-android:$daggerVersion"
compile "com.google.dagger:dagger-android-support:$daggerVersion"
kapt "com.google.dagger:dagger-android-processor:$daggerVersion"
kapt "com.google.dagger:dagger-compiler:$daggerVersion"
implementation "com.google.dagger:dagger-android-support:2.11-rc2" // version may be not up 2 date later.
}
repositories {
mavenCentral()
}
이렇게하면 'var app : Application? = null' –
필수 종속성을 선언하는 데 생성자를 사용하지 않는 이유는 무엇입니까? –
하지만 어떻게 테스트 컨텍스트를 주입 할 수 있습니까? 같은 방법으로? – Konrad