Google Firebase 和 Dagger-2 不工作

Google Firebase and Dagger-2 does not working

我想使用 Dagger2 和 Firebase。 很遗憾,我收到以下错误消息:

background_crash E/FA: Task exception on worker thread: java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist.
W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.

如果我不使用 Dagger2,一切正常。 这里有什么问题吗?我必须手动初始化 FirebaseApp 吗?谢谢! 这是我的代码:

// App extends Application
@Override
public void onCreate() {
    super.onCreate();
    appComponent = DaggerAppComponent
            .builder()
            .appModule(new AppModule(this))
            .build();
    firebaseAnalytics = FirebaseAnalytics.getInstance(this);
}

...

// AppComponent.class
@Provides
@Singleton
public FirebaseAuth provideFirebaseAuth() {
    return FirebaseAuth.getInstance();
}

...

@ActivityScope
@Component(dependencies = AppComponent.class)
public interface SignupComponent {
    void inject(SignupActivity signupActivity);
}

...

@Inject
public FirebaseAuth firebaseAuth;

private SignupComponent signupComponent;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_signup);
    ButterKnife.bind(this);
    signupComponent = DaggerSignupComponent
            .builder()
            .appComponent(((App)getApplication()).getAppComponent())
            .build();
    signupComponent.inject(this);
}

@OnClick(R.id.sign_up_btn_sign_up)
public void clickOnSignUp() {
    String email = emailInput.getText().toString();
    String pass = passwordInput.getText().toString();

    if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(pass)) {
        progressBar.setVisibility(View.VISIBLE);
        firebaseAuth.createUserWithEmailAndPassword(email, pass)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        progressBar.setVisibility(View.GONE);
                        if (task.isSuccessful()) {
                            Toast.makeText(SignupActivity.this, "yeppppa", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }
}

Aaa 和我的构建文件

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.1.1'
    compile 'com.android.support:design:24.1.1'
    compile 'com.android.support:cardview-v7:24.1.1'
    compile 'com.android.support:recyclerview-v7:24.1.1'

    compile 'com.google.firebase:firebase-core:9.4.0'
    compile 'com.google.firebase:firebase-database:9.4.0'
    compile 'com.google.firebase:firebase-storage:9.4.0'
    compile 'com.google.firebase:firebase-crash:9.4.0'
    compile 'com.google.firebase:firebase-auth:9.4.0'

    compile 'com.google.dagger:dagger:2.6'
    apt 'com.google.dagger:dagger-compiler:2.6'

    compile 'com.jakewharton:butterknife:8.2.1'
    apt 'com.jakewharton:butterknife-compiler:8.2.1'
}

apply plugin: 'com.google.gms.google-services'
 wrong configured dagger-compiler:

compile 'com.google.dagger:dagger-compiler:2.1'

please modify project level ' build.gradle:

buildscript 
{
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}
And then app level build.gradle:

First, add plugin:

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

And then modify the dependency to

apt "com.google.dagger:dagger-compiler:2.1"

这其实和Dagger没有任何关系。它与 Firebase 崩溃报告的一个已知问题有关,同时使用 Application subclass 来处理最终在主进程和崩溃报告创建的进程之间共享的文件或其他资源。 Android 为您的应用程序使用的每个进程创建一个新的应用程序 class 并不经常被讨论,在您的情况下,您实际上有两个相互冲突的 Dagger 图。

阅读本页末尾的已知问题: https://firebase.google.com/docs/crash/android

最简单的解决方案是删除 firebase-crash 依赖项,或者将您的初始化逻辑移至内容提供程序而不是应用程序。无论如何,Android 平台团队通常不赞成使用应用程序。

您也可以等到 Firebase 崩溃报告离开测试版,届时它将不再创建新进程。