dagger android support to androidx.fragment

2019-03-24 18:04发布

How to inject a fragment from the package androidx.fragment.app.Fragment ?

I'm using the dagger-android framework to inject my dependencies in my code.

As the documentation says I do this to inject my fragment

@Override
public void onAttach(Activity activity) {
    AndroidInjection.inject(this);
    super.onAttach(activity);
    // ...
}

the problem is that AndroidSupportInjection class accept only fragments of the package android.support.v4.app.Fragment or if I use AndroidInjection class only accept fragments of the package android.app.Fragment and I want to use fragments of the androidx.fragment.app.Fragment package.

Also DaggerFrament extend from android.support.v4.app.Fragment and want to use a fragment from androidx

And If I try to implement HasSupportFragmentInjector also this interface use a fragment from android.support

6条回答
淡お忘
2楼-- · 2019-03-24 18:28

I had a similar error and it was due to the Dagger version. On version 2.17 there is an strange issue, but if you roll back to version 2.16 it compiles perfectly (apart from the flags on gradle.properties that Paul posted).

From there using the tutorials you won't have trouble. Forgot to mention that on my project I had the non-androidX version of everything, then I ran the androidX migration that android studio offers, and after that I had to switch the Dagger version, but I suppose that if you do it from the start it's the same.

Hope this helps, if you switch and it doesn't work, post a little bit of your dagger implementation and plugins versions and I will try to help more!

查看更多
【Aperson】
3楼-- · 2019-03-24 18:30

Add the following to your gradle.properties file

android.useAndroidX = true
android.enableJetifier = true
查看更多
We Are One
4楼-- · 2019-03-24 18:39

add the below code to your gradle.properties

android.useAndroidX=true
android.enableJetifier=true

And if you are trying to inject into a Fragment you have to replace AndroidInjection.inject(this) with AndroidSupportInjection.inject(this)

查看更多
Explosion°爆炸
5楼-- · 2019-03-24 18:39

This is what I did to work with androidx namespace for Dagger 2.21

  1. Downloaded the jetifier tool from here: https://developer.android.com/studio/command-line/jetifier

  2. Unzip into a folder and open a terminal pointing into the extracted bin folder

  3. From Android Studio, open a class like DaggerFragment to check the path where the file is stored, for me (in MacOS) is something like this: enter image description here

  4. From terminal execute this command (replacing with the correct variables and path)

    ./jetifier-standalone -i /Users/{YOUR_USER}/.gradle/caches/{PATH_TO_DAGGER_ANDROID_SUPPORT_FOLDER}/dagger-android-support-2.21.aar -o dagger-android-support-2.21.aar

    The converted dagger-android-support-2.21.aar will appear in your bin folder

  5. Now open your app build.gradle file and change this line
    implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
    adding the , '*.aar' part in the include array

  6. Move the generated dagger-android-support-2.21.aar from bin to libs folder inside your project.

  7. Remove (or comment) this line from the dependencies implementation "com.google.dagger:dagger-android-support:2.21

    Now you can proceed invalidating the cache and rebuild the project and now DaggerFragment will point to your converted version which uses androidx.fragment.app.Fragment

NOTE: Obviously this is a temporary workaround and you should move to the official version as soon this is fixed in Dagger

查看更多
倾城 Initia
6楼-- · 2019-03-24 18:42

I had the same problem in case of HasFragmentInjector. You need to use HasSupportFragmentInjector for fragment injection. This is because, HasFragmentInjector uses android.app.Fragment which is not effected by jetifier. You need to add android-dagger-support library, jetifier converts all the support packages to androidx in Studio 3.3 (if jetifier is enabled).

If jetifier does not change support packages to androidx packages. You can download jetifier tool from here and convert the android-dagger-support.aar file manually by using the following command.

./jetifier-standalone -i dagger-android-support-<version>.aar -o <output-name>

Then add the library to your project. This is the HasSupportFragment class after conversion

import androidx.fragment.app.Fragment;
import dagger.android.AndroidInjector;

public interface HasSupportFragmentInjector {
    AndroidInjector<Fragment> supportFragmentInjector();
}

Somehow, jetifier tool was not converting libraries in AndroidStudio. I had to do it manually.

查看更多
等我变得足够好
7楼-- · 2019-03-24 18:45

Just for reference. i had the same problem. It was Jetifier issue. please upgrade your gradle build tools plugin to 3.3.0

        classpath 'com.android.tools.build:gradle:3.3.0' 

Sample code: https://github.com/jega-ms/android-dagger2-mvp-rx

查看更多
登录 后发表回答