簡體   English   中英

未創建Dagger2組件

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM