简体   繁体   English

调用dagger接口需要哪个依赖

[英]Which dependency required to call dagger interface

I am trying to use dagger to in my Android project.我正在尝试在我的 Android 项目中使用 dagger。 after importing the below posted dependencies, I am able to use the annotations but I can not use the Dagger Interface which is the @Component .导入以下发布的依赖项后,我可以使用注释,但不能使用 Dagger 接口,即@Component for example, if my interface is called "MyComponent", and when I want to use it as follows:例如,如果我的界面被称为“MyComponent”,当我想按如下方式使用它时:

DaggerMyComponent.build()

I found that DaggerMyComponent is not defined and I can't use it.我发现 DaggerMyComponent 没有定义,我不能使用它。 Please have a look at my Gradle files and let me know if there is any thin missing请看一下我的Gradle文件,如果有任何遗漏,请告诉我

gradle:project毕业:项目

// 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.3'
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' //added apt for source code generation

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

allprojects {
repositories {
    jcenter()

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

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

gradle.App : gradle.App

apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "26.0.1"
defaultConfig {
    applicationId "com.example.pc_amr.dagger_1"
    minSdkVersion 16
    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.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'

compile 'com.google.dagger:dagger-android:2.11'
compile 'com.google.dagger:dagger-android-support:2.11' // if you use the support libraries
annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
}

interface component接口组件

@Module
public class VehicleModule {

@Singleton
@Provides
Motor provideMotor(){
    return new Motor();
}

@Singleton
@Provides
Vehicle provideVehicle(){
    return new Vehicle(new Motor());
}

} }

main :主要

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    VehicleComponent component = DaggerVehicleComponent//Not recognised
}
}

I found it, I had to modify the build.gradle(App) and and the following dependencies:我找到了,我不得不修改 build.gradle(App) 和以下依赖项:

compile "com.google.dagger:dagger:2.11"
annotationProcessor "com.google.dagger:dagger-compiler:2.11"
provided 'javax.annotation:jsr250-api:1.0'
compile 'javax.inject:javax.inject:1'

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

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