【问题标题】:Dagger for Android - cant define injection for classDagger for Android - 无法为类定义注入
【发布时间】:2014-12-19 04:52:52
【问题描述】:

在尝试定义将使用我的注入的类时,我似乎无法使用注入这个词。

这是我的 MainModule.java 类:

import dagger.Module;
import dagger.Provides;


@Module()
public class MainModule {

    private Context context;

    public MainModule(Context context) {
        this.context = context;
    }


    @Provides
    Object provideSomething(Context context) {
        return new Object();
    }
}



@Module()
 class SubModule1 {

    private Context context;

    public SubModule1(Context context) {
        this.context = context;
    }

    @Provides
    Object provideSomethingElse(Context context) {
        return new Object();
    }
}

这是我在 android 中扩展应用程序的 MainApplication 类:

public class MainApplication extends Application {

    private ObjectGraph objectGraph;


    @Override
    public void onCreate() {
        super.onCreate();
        objectGraph = ObjectGraph.create(new MainModule(this));
        objectGraph = ObjectGraph.create(new SubModule1(this));
        objectGraph = ObjectGraph.create(new ActivityModule());
        objectGraph.inject(this);
}
    }

这是我在下面遇到的问题,android studio 看不到 injects 关键字,所以我无法使用它。

ActivityModule.java 类如下:

@Module(
        injects=
                ListPageActivity.class

) 
public class ActivityModule { }

ActiveModule 中的 injections 关键字在我的 IDE 中无法识别。这是我的 gradle 构建依赖项:

dependencies {
    compile files('libs/volley.jar')
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.0'
    compile 'com.android.support:recyclerview-v7:21.0.0'
    compile 'com.android.support:cardview-v7:21.0.0'
    compile 'com.jakewharton:butterknife:6.0.0'
    compile 'com.squareup:dagger:0.9.1'

}

【问题讨论】:

  • 只是好奇,你为什么使用 0.9.1 而不是最新的?
  • 我认为这是我在 J center 和 maven central 上检查的最新版本。你能给我最新的吗

标签: android dependency-injection dagger butterknife


【解决方案1】:

如果您需要使用 0.9.1 版本,请改用 entryPoints 关键字。那是因为injects 被重命名为entryPoints 并且存在于较新的版本中。

我建议你使用 Square 的最新 1.2.2 版本,它有 injects:
http://square.github.io/dagger/

apt "com.squareup.dagger:dagger-compiler:1.2.2"
compile "com.squareup.dagger:dagger:1.2.2"

或来自 Google 的最新 Dagger 2。它仍然是快照,但我希望不会太久。:
http://google.github.io/dagger/

apt "com.google.dagger:dagger-compiler:2.0-SNAPSHOT"
compile "com.google.dagger:dagger:2.0-SNAPSHOT"

【讨论】:

  • 嘿,谢谢 - 加一。你能给我在 android studio 中编译的 gradle 依赖吗?我找不到它。我指的是谷歌包中的匕首2。
  • 你对项目网站有 maven 依赖。将其转换为 Gradle 很容易,但我已经编辑了答案。
  • 错误:“未找到 Gradle DSL 方法:apt()”
  • 你需要在你的项目中使用android-apt插件:bitbucket.org/hvisser/android-apt
【解决方案2】:

改变你的匕首版本。使用发布的代码对我来说一切似乎都很好。

compile 'com.squareup.dagger:dagger:1.2.2'

组 id 已更改,因此您可以在 maven Central here 上找到该项目

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多