繁体   English   中英

Dagger 2“Dagger”前缀组件无法编译?自动生成的类

[英]Dagger 2 “Dagger” prefix component not able to compile? auto generated class

我试图在Android上使用Dagger 2。 我以前有它工作,我有一个appModule注入依赖于应用程序中的特定类。 我的问题是我得到了错误

Error:(14, 55) error: cannot find symbol class DaggerAppComponent

试图导入。 这是一个自动生成的类

下面是我的build.gradle文件中的Dagger特定依赖项

 compile 'com.google.dagger:dagger-compiler:2.0.2'
 compile 'com.google.dagger:dagger:2.0.2'
 provided 'javax.annotation:jsr250-api:1.0'

我曾多次尝试清理和重建应用程序,但课程不会产生。 我也试过用

 compile 'org.glassfish:javax.annotation:10.0-b28'

对于我的注释,但我仍然没有运气? 如果有人可以帮助我id欣赏。 很难确切地看到我目前到底发生了什么? 谢谢

编辑:这是以前工作的组件代码,我只是添加了1个额外的类注入?

@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {

    void inject(RegHelper reghelper);
    void inject(headerFooterRecViewAdapter headadapter);
    void inject(SectionListExampleActivity seclistactivity);

}

请加

apt 'com.google.dagger:dagger-compiler:2.x'

到您的app build.gradle文件

在Android Studio 2.3中设置独立项目,我更新了默认的gradle文件,如下所示,以获取生成的Component文件。 添加的行有注释// dagger2 addition

PROJECT build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.1'

        // dagger2 addition
        classpath 'com.android.tools.build:gradle:1.0.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.+' 

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()

        // dagger2 addition
        mavenCentral()
        maven{
            url 'https://oss.sonatype.org/content/repositories/snapshots/'
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

APP MODULE build.gradle

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'   // dagger2 addition

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.demo.dagger2demo"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'

    // dagger2 addition
    compile 'com.google.dagger:dagger:2.+'
    apt "com.google.dagger:dagger-compiler:2.+"
}

对于我(当前)最新的匕首依赖,这对我来说很有用。

`dependencies{
...
compile 'com.google.dagger:dagger:2.11'
compile 'com.google.dagger:dagger-android-support:2.11'
annotationProcessor "com.google.dagger:dagger-compiler:2.11"
}`

您需要安装此插件https://bitbucket.org/hvisser/android-apt ,以便Android Studio查看Dagger组件。

我和Dagger 2有类似的问题。我有一个AppComponent和一个ActivityComponent(作为一个子组件)。 一旦我在AppComponent中添加一个新的inject()函数,我就会得到上述错误。

除了“无法找到符号”错误之外还有更多错误,但它们非常模糊,我无法调试我的问题。 在挖掘和研究stackoverflow和不同的教程之后,我意识到我正在使用Dagger。 特别是我的AppComponent和ActivityComponent的设置方式。

我假设我可以用我的AppComponent和ActivityComponent注入我的'Activity'或'Fragment'。 事实证明这是错误的,至少我发现这不是使用Dagger的正确方法。

我的解决方案

AppComponent

@Singleton
@Component(modules = {AppModule.class})
public interface AppComponent {

    void inject(MyApp application);
    void inject(ContextHelper contextHelper);

    // for exports
    MyApp application();
    PrefsHelper prefsHelper();
}

应用模块

@Module
public class AppModule {

    private final MyApp application;

    public AppModule(MyApp application) {

        this.application = application;
    }

    @Provides @Singleton
    public MyApp application() {

        return this.application;
    }

    @Provides @Singleton
    public PrefsHelper providePrefsHelper() {

        PrefsHelper prefsHelper = new PrefsHelper(application);
        return prefsHelper;
    }
}

ActivityComponent

@ActivityScope
@Component (dependencies = {AppComponent.class}, modules = {ActivityModule.class})
public interface ActivityComponent {

    void inject(MainActivity activity);
    void inject(OtherActivity activity);
    void inject(SomeFragment fragment);
}

ActivityModule

@Module
public class ActivityModule {

    private final MyActivity activity;

    public ActivityModule(MyActivity activity) {

        this.activity = activity;
    }

    @Provides @ActivityScope
    public ContextHelper provideContextHelper(MyApp application) {

        // My ContextHelper depends on certain things from AppModule 
        // So I call appComponent.inject(contextHelper)

        AppComponent appComponent = application.getAppComponent();
        ContextHelper contextHelper = new ContextHelper(activity);
        appComponent.inject(contextHelper);
        return contextHelper;
    }
}

应用

public class MyApp extends Application {

    private AppComponent appComponent;

    @Override
    public void onCreate() {

        super.onCreate();
        initializeDepInj();
    }

    private void initializeDepInj() {

        appComponent = DaggerAppComponent.builder()
                .appModule(new AppModule(this))
                .build();
        appComponent.inject(this);
    }

    public LockAppComponent getAppComponent() {

        return appComponent;
    }
}

活动

public class MainActivity extends AppCompatActivity {

    // I get it from ActivityModule
    @Inject
    ContextHelper contextHelper;

    // I get it from AppModule
    @Inject
    PrefsHelper prefsHelper;

    ActivityComponent component;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setupInjection();
    }

    protected void setupInjection() {

        MyApp application = (MyApp) getApplication();
        component = DaggerActivityComponent.builder()
                .appComponent(application.getAppComponent())
                .activityModule(new ActivityModule(this))
                .build();
        component.inject(this);

        // I was also doing the following snippet
        // but it's not the correct way since the ActivityComponent depends
        // on AppComponent and therefore ActivityComponent is the only 
        // component that I should inject() with and I'll still get classes
        // that are provided by the AppComponent/AppModule

        // application.getAppComponent().inject(this); // this is unneccessary
    }

    public ContextHelper getContextHelper() {

        return contextHelper;
    }
}

我不知道它是否直接解决了你的问题,但它至少应该说明如何正确使用Dagger。

希望能帮助到你。

我在以下应用程序类中的设置Android Studio 2.2上遇到了同样的问题:

public class NetApp extends Application {

    private NetComponent mNetComponent;

    @Override
    public void onCreate() {
        super.onCreate();

        // Dagger%COMPONENT_NAME%
        mNetComponent = DaggerNetComponent.builder()
                // list of modules that are part of this component need to be created here too
                .appModule(new AppModule(this)) // This also corresponds to the name of your module: %component_name%Module
                .netModule(new NetModule("https://api.github.com"))
                .build();

        // If a Dagger 2 component does not have any constructor arguments for any of its modules,
        // then we can use .create() as a shortcut instead:
        //  mNetComponent = com.codepath.dagger.components.DaggerNetComponent.create();
    }

    public NetComponent getNetComponent() {
        return mNetComponent;
    }
}

我正在使用以下gradle声明用于匕首2:

//Dagger 2
// apt command comes from the android-apt plugin
apt 'com.google.dagger:dagger-compiler:2.7'
compile 'com.google.dagger:dagger:2.7'
provided 'javax.annotation:jsr250-api:1.0'

that was missing before. 我可以通过重建整个项目(有错误)然后添加之前丢失的的导入来解决问题。

只需将@Module添加到Api类并重建项目即可。

希望你们都做得很好。 我遇到了同样的问题,并花了很多时间在堆栈溢出。 最后我通过这个并找到了解决方案。 简而言之,您必须在模块级Gradle文件中进行一些更改。 请删除

apply plugin: 'com.neenbedankt.android-apt'

在文件的顶部。 并替换

apt 'com.google.dagger:dagger-compiler:2.11'

annotationProcessor 'com.google.dagger:dagger-compiler:2.11'

在重建项目之后,您将能够导入Dagger前缀类。 它会帮助你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM