[英]Dagger2 component is not created
这是代码...
build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
主要活动
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import javax.inject.Inject;
public class MainActivity extends AppCompatActivity {
@Inject
SampleModule sampleModule;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((SampleApp)getApplication()).getSampleComponent().inject(this);
sampleModule.simpleModel.setX(10);
}
}
SampleApp
import android.app.Application;
/**
* Created by pavan on 4/17/2017.
*/
public class SampleApp extends Application {
private SampleComponent sampleComponent;
@Override
public void onCreate() {
super.onCreate();
sampleComponent = DaggerSampleComponent.builder()
.sampleModule(new SampleModule(this))
.build();
}
public SampleComponent getSampleComponent(){
return sampleComponent;
}
}
样品成分
import javax.inject.Singleton;
import dagger.Component;
/**
* Created by pavan on 4/17/2017.
*/
@Singleton
@Component(modules = {SampleModule.class})
public interface SampleComponent {
void inject(MainActivity activity);
}
样品模块
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
/**
* Created by pavan on 4/17/2017.
*/
@Module
public class SampleModule {
SimpleModel simpleModel;
SampleApp sampleApp;
public SampleModule(SampleApp sampleApp){
this.sampleApp = sampleApp;
}
@Provides
@Singleton
public SampleApp provideApplication(){
return sampleApp;
}
@Provides
@Singleton
public SimpleModel provideSimpleModelObj() {
return new SimpleModel();
}
}
简单模型
public class SimpleModel {
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
摇篮日志
Error:(13, 10) error: com.uvr.organizer.myfilesorganizer.FileOrganizerModule cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
com.uvr.organizer.myfilesorganizer.FileOrganizerModule is injected at
com.uvr.organizer.myfilesorganizer.LoginActivity.appScope
com.uvr.organizer.myfilesorganizer.LoginActivity is injected at
com.uvr.organizer.myfilesorganizer.AppComponent.inject(loginActivity)
Error:(14, 10) error: com.uvr.organizer.myfilesorganizer.FileOrganizerModule cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
com.uvr.organizer.myfilesorganizer.FileOrganizerModule is injected at
com.uvr.organizer.myfilesorganizer.MainActivity.appScope
com.uvr.organizer.myfilesorganizer.MainActivity is injected at
com.uvr.organizer.myfilesorganizer.AppComponent.inject(mainActivity)
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
Information:Total time: 8.136 secs
Information:3 errors
Information:0 warnings
Information:See complete output in console
我所有的问题是关于文件“ DaggerSampleComponent”的,除非我删除接口中的所有注入,否则根本不会创建该文件。
Java版本:1.8
相同的代码似乎可以在我的Office Mac上运行,但不能在Windows上运行。 为此苦苦挣扎。 有人能帮我吗!!!
提前致谢。
当Dagger 2设置出现问题时,您尝试进行构建时会在Gradle控制台中收到一条编译时消息(位于Android Studio的右下角)。 该消息将告诉您出了什么问题,并为您提供了解决方法的线索。
就您而言,您的LoginActivity
内看起来像这样:
@Inject FileOrganiserModule fileOrganiserModule;
protected void onCreate(Bundle savedInstanceState) {
Dagger 2模块和组件就像脚手架一样,可以帮助您请求注入项目中的依赖项。 通常,您不应通过在其上放置@Inject
注释来请求注入它们。
如果必须使用对当前Activity的引用来创建模块,则通常只需使用构造函数创建模块的实例:
void injectMembers() {
DaggerLoginComponent.builder().loginModule(new LoginModule(this));
}
或者,您可以使用新的dagger.android
类为您完成此操作。 您可以遵循的一个很好的示例项目在此处的Google Android体系结构蓝图仓库中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.