2017-09-26 41 views
-1

我试图在我的Android项目中使用匕首。在导入下面发布的依赖项后,我可以使用注释 ,但我无法使用@Component的Dagger接口。例如,如果我的界面被称为“MyComponent的”,而当我 要使用它,如下所示:需要调用匕首接口的依赖关系

DaggerMyComponent.build() 

我发现DaggerMyComponent没有定义,我不能使用它。请看看我的Gradle文件,让我知道,如果有任何遗漏薄

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.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

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' 
} 

界面组件

@Module 
public class VehicleModule { 

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

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

}

主要

public class MainActivity extends AppCompatActivity { 

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

    VehicleComponent component = DaggerVehicleComponent//Not recognised 
} 
} 
+0

你能告诉你的模块和组件声明? – pdegand59

+0

@ pdegand59请找到张贴的组件信息。请让我知道不管Dagger所需的依赖关系是否正确? – LetsamrIt

+0

您忘记了VehicleComponent接口,使用'@ Component'注解的接口 – pdegand59

回答

0

我找到了,我不得不修改的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'