【问题标题】:Dagger with androidx namespace not working带有 androidx 命名空间的匕首不起作用
【发布时间】:2018-10-31 20:34:18
【问题描述】:

我无法让 dagger 与 androidx.fragment.app.Fragment 一起工作;

// My dependencies
implementation 'com.google.android.material:material:1.0.0'
...
implementation 'com.google.dagger:dagger-android:2.19'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.19'

// gradle.properties
android.useAndroidX=true
android.enableJetifier=true

// In my Fragment
...
import androidx.fragment.app.Fragment;
...

public abstract class BaseFragment extends Fragment {

   @Override
    public void onAttach(Context context) {
        AndroidInjection.inject(this); // cannot resolve method here, AndroidInjection wants to use AndroidInjection.inject(android.app.Fragment);
        super.onAttach(context);        
    }
}

因此,我的 Activity 类中存在相同的 Fragment 类型错误

public class MainActivity extends BaseActivity implements HasFragmentInjector{

    @Inject
    DispatchingAndroidInjector<androidx.fragment.app.Fragment> fragmentInjector;

// incompatible  return type here, AndroidInjector wants to use AndroidInjector<android.app.Fragment>
    @Override
    public AndroidInjector<Fragment> fragmentInjector() { 
        return fragmentInjector;
    }

}

不适用 1.Material Design Components 说我不应该碰任何支持库。 (AndroidInjection.inject&lt;android.support.v4.app.Fragment&gt; 被消灭了, 虽然我的 Dagger 组件无法生成)。

2.Google says android.app.Fragment is depreciated.

我如何连接所有东西以使 dagger 与 androidx 命名空间一起工作。

问候, 乔治

【问题讨论】:

    标签: android android-fragments dagger-2 dagger androidx


    【解决方案1】:

    该问题可能会在 Dagger 的下一个版本 (2.20) 中得到解决。 (见:https://github.com/google/dagger/releases/tag/dagger-2.19

    目前,我认为解决问题的最佳方法是使用旧版本 (2.16)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 1970-01-01
      相关资源
      最近更新 更多