2017-09-26 68 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'