I am eager to study Dagger 2, which in theory does not seem so difficult, but in practice I have already spent 2 days to launch the project, but to no avail.
@Module class ActivityModule { @Provides @NonNull @Singleton fun getActivitys() : TestClass = TestClass() } @Module class AppModule(val context: Context) { @Provides @Singleton fun getContexts() : Context = context } @Component(modules = [(ActivityModule::class), (AppModule::class)]) @Singleton interface AppComponent { fun inject(mainActivity: MainActivity) } App
class App : Application() { lateinit var appComponent: AppComponent override fun onCreate() { super.onCreate() appComponent = buildComponent() } fun getComponent() : AppComponent = appComponent private fun buildComponent(): AppComponent { return DaggerAppComponent.builder() .appModule(AppModule(this)) .activityBindgsModule(ActivityModule()) .build() } } MainActivity
class MainActivity : AppCompatActivity() { @Inject lateinit var testClass: TestClass override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) App().getComponent().inject(this) Log.d("MAIN", testClass.getString()) } } Registered in the manifesto
<application android:name=".App" ... In Build dependencies prescribed such, tried to update, does not help.
compile "com.google.dagger:dagger:2.11" compile "com.google.dagger:dagger-android:2.11" compile "com.google.dagger:dagger-android-support:2.11" kapt "com.google.dagger:dagger-compiler:2.11" kapt "com.google.dagger:dagger-android-processor:2.11" When running the project on the emulator, during the build I get an error in the Messanger section
Error:(25, 17) Unresolved reference: DaggerAppComponent Error:Execution failed for task ':app:compileDebugKotlin'. > Compilation error. See log for more details But if I download a ready-made example with GitHub Link to this project, everything compiles, but if I start a new project, I get errors. This example I wrote will do another article.
Updated
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 26 defaultConfig { applicationId "info.android_developer_community.dagerss" minSdkVersion 21 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' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' implementation "com.google.dagger:dagger:2.14" implementation "com.google.dagger:dagger-android:2.14" implementation "com.google.dagger:dagger-android-support:2.14" kapt "com.google.dagger:dagger-android-processor:2.14" kapt "com.google.dagger:dagger-compiler:2.14" kapt "com.google.dagger:dagger-android-support:2.14" }
rebuildproject. - Yuriy SPb ♦apply plugin: 'kotlin-kapt'- Yuriy SPb ♦