这是代码......
的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
}
MainActivity
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;
}
}
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);
}
SampleModule
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();
}
}
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;
}
}
Gradle Log
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
相同的代码似乎在我的办公室Mac上工作,但不在我的Windows中。为此付出了很多努力。 有人可以帮帮我!!!
提前致谢。
答案 0 :(得分:1)
当您的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架构蓝图回购here