![](/img/trans.png)
[英]How can I get Dagger 2 to auto-generate the code I need in this case?
[英]Troubleshooting gradle file when dagger doesn't auto-generate component code
更新 :鏈接到項目-> https://drive.google.com/open?id=1D8mYZL3Pb8FezPp5FOKA20-Um5kTRYxH
我一直從Dagger開始,然后按照本教程進行操作,直到第5.4步: https : //www.vogella.com/tutorials/Dagger/article.html#special-treatment-of-fields-in-dagger
我添加了以下依賴項:
api 'com.google.dagger:dagger:2.20'
api 'com.google.dagger:dagger-android:2.20'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.20'
annotationProcessor 'com.google.dagger:dagger-compiler:2.20'
compileOnly 'javax.annotation:jsr250-api:1.0'
但是IDE無法解析“ DaggerMyApplicationComponent”(在onCreate()中)。 似乎Dagger無法生成代碼。
MyApplication.java
import android.app.Activity;
import android.app.Application;
import javax.inject.Inject;
import dagger.android.DispatchingAndroidInjector;
import dagger.android.HasActivityInjector;
public class MyApplication extends Application implements HasActivityInjector {
@Inject
DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;
@Override
public void onCreate() {
super.onCreate();
DaggerMyApplicationComponent.create().inject(this);
}
@Override
public DispatchingAndroidInjector<Activity> activityInjector() {
return dispatchingAndroidInjector;
}
}
AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.parassidhu.daggerkumar">
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MyApplicationModule.java :
import dagger.Module;
import dagger.android.ContributesAndroidInjector;
@Module
public abstract class MyApplicationModule {
@ContributesAndroidInjector
abstract MainActivity contributeActivityInjector();
}
MyApplicationComponent.java :
import javax.inject.Singleton;
import dagger.Component;
import dagger.android.AndroidInjectionModule;
import dagger.android.AndroidInjector;
@Singleton
@Component(modules = {AndroidInjectionModule.class, MyApplicationModule.class})
public interface MyApplicationComponent extends AndroidInjector<MyApplication> {
}
請提出可能出了什么問題。 我試過幾次使項目,清理,重建,失效和重新啟動,但沒有幫助。
如評論中所述,從gradle中刪除所有不必要的kotlin依賴項和插件。 如果您的項目中沒有kotlin代碼,則將模塊稱為kotlin模塊是沒有意義的。 Dagger認為它應該生成kotlin代碼,但它沒有kotlin編譯器來執行。 因此,要么將整個代碼更改為kotlin並使用kotlin依賴項,要么堅持使用Java並刪除所有kotlin依賴項。
我已經解決了這個問題。 解決方案很奇怪。 我將依賴項更改為:
api 'com.google.dagger:dagger:2.20'
api 'com.google.dagger:dagger-android:2.20'
kapt 'com.google.dagger:dagger-android-processor:2.20'
kapt 'com.google.dagger:dagger-compiler:2.20'
總體來說,我用kapt
代替annotationProcessor
然后清理項目和使項目和繁榮! 有效。
現在來了怪異的部分。 盡管在項目中配置了Kotlin,但我的項目中沒有一個Kotlin文件 (默認的測試除外)。 如果任何人都能對它有所了解,將不勝感激。
希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.