在两种不同口味之间共享Dagger 2组件 [英] Share a Dagger 2 component between two different flavors

查看:128
本文介绍了在两种不同口味之间共享Dagger 2组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种口味:专业版和免费版。在每种方法中,我都有一个MainActivity,但是我希望 MainComponent MainModule 都相同,所以 MainComponent MainModule 都在src / java / main中,但是由于没有,我的MainComponent出现了错误具有两种口味的MainActivity的导入。在这里,让我说清楚:

I have two flavors: pro and free. In each of these, I have a MainActivity, but I want the MainComponent and MainModule to be the same for both, so MainComponent and MainModule are both in src/java/main, but I get an error in my MainComponent due to not having an import of MainActivity of both flavors. Here, let me make it clear:

这是src / java / main中的 MainComponent 的样子:

Here's what the MainComponent in src/java/main looks like:

import com.xxx.myapp.di.modules.MainModule;
import com.xxx.myapp.free.MainActivity;
import com.xxx.myapp.presenters.MainPresenterImpl;

import javax.inject.Singleton;

import dagger.Component;


@Singleton
@Component(modules = {MainModule.class})
public interface MainComponent {
    void inject(MainActivity mainActivity);
    void inject(MainPresenterImpl mainPresenter);
}

在上面显示的代码中,它仅导入 MainActivity 来自免费版本,因此当我切换到专业版本时会出错。

In the code shown above, it only imports the MainActivity from the free flavor, hence giving an error when I switch to the pro flavor.

推荐答案

使用调味剂时,不同成分的包装应不同。

When using flavors you should not have different packages for the different components.

而不是 com.xxx.myapp.free.MainActivity
将MainActivity上移至 com.xxx.myapp.MainActivity 两个版本的MainActivity必须具有相同的完全限定名称。

Instead of com.xxx.myapp.free.MainActivity Move MainActivity up a level to com.xxx.myapp.MainActivity both versions of MainActivity must have the same fully qualified named.

更新

您有两个名为 MainActivity的类,但对于Java,它们具有不同的完全限定名称 com.xxx.myapp.free.MainActivity com.xxx.myapp .pro.MainActivity (只是一个猜测)。

You have two classes named MainActivity but to Java they have different fully qualified names com.xxx.myapp.free.MainActivity and com.xxx.myapp.pro.MainActivity (just a guess).

您真正需要的是一个类 com.xxx.myapp .MainActivity 具有特定的实现。您可以通过风味特定的文件夹路径向每个风味添加一个单独的MainActivity实现。

What you really need is one class com.xxx.myapp.MainActivity with flavor specific implementations. YOu can add a separate implementation of MainActivity to each flavor by the flavor specific folder paths.

app / src / free / java / com / xxx /myapp/MainActivity.java
app / src / pro / java / com / xxx / myapp / MainActivity.java

Gradle将仅编译所选风味的实现。

Gradle will only compile the implementation for the selected flavor.

这篇关于在两种不同口味之间共享Dagger 2组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆