为什么dagger 2生成的组件会在项目重建时被删除?

时间:2017-02-27 04:01:54

标签: android gradle android-gradle build.gradle dagger-2

当没有注释掉在组件上使用生成的'Dagger'的代码时,问题就存在了。如果已注释掉,则会生成生成的Dagger 2文件。

BookModule

@Module
public class BookModule {

    @Provides
    public Book providesBook(){
        return new Book();
    }

}

BookComponent

@Component(modules = BookModule.class)
public interface BookComponent {

    void inject(MainActivity activity);

    public Book getBook();

}

MainApplication

public class MainApplication extends Application {

    public static BookComponent mBookComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        mBookComponent = DaggerBookComponent.builder().bookModule(new BookModule()).build();
    }
}

的build.gradle(应用)

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.3'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'

        // 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(项目)

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

android {
    compileSdkVersion 24
    buildToolsVersion "25.0.2"

    defaultConfig {
        applicationId "com.example.bryan.myapplication"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        jackOptions{
            enabled = true
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.android.support:support-v4:24.2.0'
    compile group: 'com.google.dagger', name: 'dagger', version: '2.10-rc1'
    apt group: 'com.google.dagger', name: 'dagger-compiler', version: '2.10-rc1'
    compile group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3'

}

错误Stacktrace

ERROR: C:\bin\AndroidProjects\MyApplication\app\src\main\java\com\example\lloyd\myapplication\MainApplication.java:12.26: DaggerBookComponent cannot be resolved

com.android.jack.api.v01.CompilationException: Failed to compile
    at com.android.jack.api.v01.impl.Api01ConfigImpl$Api01CompilationTaskImpl.run(Api01ConfigImpl.java:144)
    at com.android.builder.core.AndroidBuilder.convertByteCodeUsingJackApis(AndroidBuilder.java:1931)
    at com.android.build.gradle.tasks.JackTask.doMinification(JackTask.java:148)
    at com.android.build.gradle.tasks.JackTask.access$000(JackTask.java:73)
    at com.android.build.gradle.tasks.JackTask$1.run(JackTask.java:112)
    at com.android.builder.tasks.Job.runTask(Job.java:51)
    at com.android.build.gradle.tasks.SimpleWorkQueue$EmptyThreadContext.runTask(SimpleWorkQueue.java:41)
    at com.android.builder.tasks.WorkQueue.run(WorkQueue.java:223)
    at java.lang.Thread.run(Thread.java:745)
Caused by: com.android.jack.frontend.FrontendCompilationException: Failed to compile
    at com.android.jack.Jack.buildSession(Jack.java:1053)
    at com.android.jack.Jack.run(Jack.java:540)
    at com.android.jack.api.v01.impl.Api01ConfigImpl$Api01CompilationTaskImpl.run(Api01ConfigImpl.java:124)
    ... 8 more
mBookComponent = DaggerBookComponent.builder().bookModule(new BookModule()).build();

上面的代码在MainApplication中,这就是问题所在,如果我注释掉它,它会完全重建。但是当我按原样离开它时,它得到一个错误,说“DaggerBookComponent”无法解析,此时生成的DaggerBookComponent被删除,导致无法解析

1 个答案:

答案 0 :(得分:1)

当你进行重建时,构建系统会查看Dagger模块和组件类,并生成使所有dagger工作的实际类。当你进行正常的增量构建时,它没有(它假设旧版本仍然很好)。这意味着当您重建时,如果您的程序或匕首组件/模块中存在某些错误,则无法重建这些文件。

所以基本上,你的某个地方有一个bug,可能(虽然不一定)在你的匕首设置中。当你修复它时,你将不再遇到问题。