question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Remove references to deprecated android.app.Fragment

See original GitHub issue

Description:

Using my project here: https://github.com/jaredsburrows/android-gif-example, setting the compileSdk and targetSdk to 28 and the compiling you will see that android.app.Fragment is now deprecated. It seems that the DaggerAppComponent relies on android.app.Fragment instead of the support Fragments.

How can I prevent these errors without turning off warnings as errors?

DaggerAppComponent:

// Generated by Dagger (https://google.github.io/dagger).
package burrows.apps.example.gif.di.component;

import android.app.Activity;
import android.app.Application;
import android.app.Fragment;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ContentProvider;
import burrows.apps.example.gif.App;
import burrows.apps.example.gif.SchedulerProvider;
import burrows.apps.example.gif.data.ImageService;
import burrows.apps.example.gif.data.RiffsyApiClient;
import burrows.apps.example.gif.di.module.ActivityBuilderModule_ProvidesMainActivity;
import burrows.apps.example.gif.di.module.LeakCanaryModule;
import burrows.apps.example.gif.di.module.LeakCanaryModule_ProvidesRefWatcherFactory;
import burrows.apps.example.gif.di.module.NetModule;
import burrows.apps.example.gif.di.module.NetModule_ProvidesCacheFactory;
import burrows.apps.example.gif.di.module.NetModule_ProvidesMoshiFactory;
import burrows.apps.example.gif.di.module.NetModule_ProvidesOkHttpClientFactory;
import burrows.apps.example.gif.di.module.NetModule_ProvidesRetrofitFactory;
import burrows.apps.example.gif.di.module.RiffsyModule;
import burrows.apps.example.gif.di.module.RiffsyModule_ProvidesRiffsyApiFactory;
import burrows.apps.example.gif.giflist.GifActivity;
import burrows.apps.example.gif.giflist.GifActivity_MembersInjector;
import burrows.apps.example.gif.giflist.GifPresenter;
import com.squareup.leakcanary.RefWatcher;
import dagger.android.AndroidInjector;
import dagger.android.DaggerApplication_MembersInjector;
import dagger.android.DispatchingAndroidInjector;
import dagger.android.DispatchingAndroidInjector_Factory;
import dagger.android.support.DaggerAppCompatActivity_MembersInjector;
import dagger.internal.Preconditions;
import java.util.Collections;
import java.util.Map;
import javax.inject.Provider;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;

public final class DaggerAppComponent implements AppComponent {
  private Application application;

  private LeakCanaryModule leakCanaryModule;

  private Provider<ActivityBuilderModule_ProvidesMainActivity.GifActivitySubcomponent.Builder>
      gifActivitySubcomponentBuilderProvider;

  private DaggerAppComponent(Builder builder) {
    initialize(builder);
  }

  public static AppComponent.Builder builder() {
    return new Builder();
  }

  private Map<Class<? extends Activity>, Provider<AndroidInjector.Factory<? extends Activity>>>
      getMapOfClassOfAndProviderOfFactoryOf() {
    return Collections
        .<Class<? extends Activity>, Provider<AndroidInjector.Factory<? extends Activity>>>
            singletonMap(GifActivity.class, (Provider) gifActivitySubcomponentBuilderProvider);
  }

  private DispatchingAndroidInjector<Activity> getDispatchingAndroidInjectorOfActivity() {
    return DispatchingAndroidInjector_Factory.newDispatchingAndroidInjector(
        getMapOfClassOfAndProviderOfFactoryOf(),
        Collections.<String, Provider<AndroidInjector.Factory<? extends Activity>>>emptyMap());
  }

  private DispatchingAndroidInjector<BroadcastReceiver>
      getDispatchingAndroidInjectorOfBroadcastReceiver() {
    return DispatchingAndroidInjector_Factory.newDispatchingAndroidInjector(
        Collections
            .<Class<? extends BroadcastReceiver>,
                Provider<AndroidInjector.Factory<? extends BroadcastReceiver>>>
                emptyMap(),
        Collections
            .<String, Provider<AndroidInjector.Factory<? extends BroadcastReceiver>>>emptyMap());
  }

  private DispatchingAndroidInjector<Fragment> getDispatchingAndroidInjectorOfFragment() {
    return DispatchingAndroidInjector_Factory.newDispatchingAndroidInjector(
        Collections
            .<Class<? extends Fragment>, Provider<AndroidInjector.Factory<? extends Fragment>>>
                emptyMap(),
        Collections.<String, Provider<AndroidInjector.Factory<? extends Fragment>>>emptyMap());
  }

  private DispatchingAndroidInjector<Service> getDispatchingAndroidInjectorOfService() {
    return DispatchingAndroidInjector_Factory.newDispatchingAndroidInjector(
        Collections
            .<Class<? extends Service>, Provider<AndroidInjector.Factory<? extends Service>>>
                emptyMap(),
        Collections.<String, Provider<AndroidInjector.Factory<? extends Service>>>emptyMap());
  }

  private DispatchingAndroidInjector<ContentProvider>
      getDispatchingAndroidInjectorOfContentProvider() {
    return DispatchingAndroidInjector_Factory.newDispatchingAndroidInjector(
        Collections
            .<Class<? extends ContentProvider>,
                Provider<AndroidInjector.Factory<? extends ContentProvider>>>
                emptyMap(),
        Collections
            .<String, Provider<AndroidInjector.Factory<? extends ContentProvider>>>emptyMap());
  }

  @SuppressWarnings("unchecked")
  private void initialize(final Builder builder) {
    this.gifActivitySubcomponentBuilderProvider =
        new Provider<ActivityBuilderModule_ProvidesMainActivity.GifActivitySubcomponent.Builder>() {
          @Override
          public ActivityBuilderModule_ProvidesMainActivity.GifActivitySubcomponent.Builder get() {
            return new GifActivitySubcomponentBuilder();
          }
        };
    this.application = builder.application;
    this.leakCanaryModule = builder.leakCanaryModule;
  }

  @Override
  public void inject(App arg0) {
    injectApp(arg0);
  }

  private App injectApp(App instance) {
    DaggerApplication_MembersInjector.injectActivityInjector(
        instance, getDispatchingAndroidInjectorOfActivity());
    DaggerApplication_MembersInjector.injectBroadcastReceiverInjector(
        instance, getDispatchingAndroidInjectorOfBroadcastReceiver());
    DaggerApplication_MembersInjector.injectFragmentInjector(
        instance, getDispatchingAndroidInjectorOfFragment());
    DaggerApplication_MembersInjector.injectServiceInjector(
        instance, getDispatchingAndroidInjectorOfService());
    DaggerApplication_MembersInjector.injectContentProviderInjector(
        instance, getDispatchingAndroidInjectorOfContentProvider());
    DaggerApplication_MembersInjector.injectSetInjected(instance);
    return instance;
  }

  private static final class Builder implements AppComponent.Builder {
    private LeakCanaryModule leakCanaryModule;

    private Application application;

    @Override
    public AppComponent build() {
      if (leakCanaryModule == null) {
        this.leakCanaryModule = new LeakCanaryModule();
      }
      if (application == null) {
        throw new IllegalStateException(Application.class.getCanonicalName() + " must be set");
      }
      return new DaggerAppComponent(this);
    }

    @Override
    public Builder application(Application application) {
      this.application = Preconditions.checkNotNull(application);
      return this;
    }
  }

  private final class GifActivitySubcomponentBuilder
      extends ActivityBuilderModule_ProvidesMainActivity.GifActivitySubcomponent.Builder {
    private RiffsyModule riffsyModule;

    private NetModule netModule;

    private GifActivity seedInstance;

    @Override
    public ActivityBuilderModule_ProvidesMainActivity.GifActivitySubcomponent build() {
      if (riffsyModule == null) {
        this.riffsyModule = new RiffsyModule();
      }
      if (netModule == null) {
        this.netModule = new NetModule();
      }
      if (seedInstance == null) {
        throw new IllegalStateException(GifActivity.class.getCanonicalName() + " must be set");
      }
      return new GifActivitySubcomponentImpl(this);
    }

    @Override
    public void seedInstance(GifActivity arg0) {
      this.seedInstance = Preconditions.checkNotNull(arg0);
    }
  }

  private final class GifActivitySubcomponentImpl
      implements ActivityBuilderModule_ProvidesMainActivity.GifActivitySubcomponent {
    private RiffsyModule riffsyModule;

    private NetModule netModule;

    private GifActivitySubcomponentImpl(GifActivitySubcomponentBuilder builder) {
      initialize(builder);
    }

    private DispatchingAndroidInjector<android.support.v4.app.Fragment>
        getDispatchingAndroidInjectorOfFragment() {
      return DispatchingAndroidInjector_Factory.newDispatchingAndroidInjector(
          Collections
              .<Class<? extends android.support.v4.app.Fragment>,
                  Provider<AndroidInjector.Factory<? extends android.support.v4.app.Fragment>>>
                  emptyMap(),
          Collections
              .<String,
                  Provider<AndroidInjector.Factory<? extends android.support.v4.app.Fragment>>>
                  emptyMap());
    }

    private Cache getCache() {
      return NetModule_ProvidesCacheFactory.proxyProvidesCache(
          netModule, DaggerAppComponent.this.application);
    }

    private OkHttpClient getOkHttpClient() {
      return NetModule_ProvidesOkHttpClientFactory.proxyProvidesOkHttpClient(netModule, getCache());
    }

    private Retrofit.Builder getBuilder() {
      return NetModule_ProvidesRetrofitFactory.proxyProvidesRetrofit(
          netModule,
          NetModule_ProvidesMoshiFactory.proxyProvidesMoshi(netModule),
          getOkHttpClient());
    }

    private RiffsyApiClient getRiffsyApiClient() {
      return RiffsyModule_ProvidesRiffsyApiFactory.proxyProvidesRiffsyApi(
          riffsyModule, getBuilder());
    }

    private GifPresenter getGifPresenter() {
      return new GifPresenter(getRiffsyApiClient(), new SchedulerProvider());
    }

    private RefWatcher getRefWatcher() {
      return LeakCanaryModule_ProvidesRefWatcherFactory.proxyProvidesRefWatcher(
          DaggerAppComponent.this.leakCanaryModule, DaggerAppComponent.this.application);
    }

    private ImageService getImageService() {
      return new ImageService(DaggerAppComponent.this.application);
    }

    @SuppressWarnings("unchecked")
    private void initialize(final GifActivitySubcomponentBuilder builder) {
      this.riffsyModule = builder.riffsyModule;
      this.netModule = builder.netModule;
    }

    @Override
    public void inject(GifActivity arg0) {
      injectGifActivity(arg0);
    }

    private GifActivity injectGifActivity(GifActivity instance) {
      DaggerAppCompatActivity_MembersInjector.injectSupportFragmentInjector(
          instance, getDispatchingAndroidInjectorOfFragment());
      DaggerAppCompatActivity_MembersInjector.injectFrameworkFragmentInjector(
          instance, DaggerAppComponent.this.getDispatchingAndroidInjectorOfFragment());
      GifActivity_MembersInjector.injectPresenter(instance, getGifPresenter());
      GifActivity_MembersInjector.injectRefWatcher(instance, getRefWatcher());
      GifActivity_MembersInjector.injectRepository(instance, getImageService());
      return instance;
    }
  }
}

Error output(warnings as errors):

$ gradlew connectedDebugAndroidTest testDebug

> Task :compileDebugJavaWithJavac FAILED
/<>/debug/burrows/apps/example/gif/di/component/DaggerAppComponent.java:6: warning: [deprecation] Fragment in android.app has been deprecated
import android.app.Fragment;
                  ^
/<>/debug/burrows/apps/example/gif/App_MembersInjector.java:5: warning: [deprecation] Fragment in android.app has been deprecated
import android.app.Fragment;
                  ^
error: warnings found and -Werror specified
/<>/debug/burrows/apps/example/gif/di/component/DaggerAppComponent.java:81: warning: [deprecation] Fragment in android.app has been deprecated
  private DispatchingAndroidInjector<Fragment> getDispatchingAndroidInjectorOfFragment() {
                                     ^
/<>/debug/burrows/apps/example/gif/di/component/DaggerAppComponent.java:84: warning: [deprecation] Fragment in android.app has been deprecated
            .<Class<? extends Fragment>, Provider<AndroidInjector.Factory<? extends Fragment>>>
                              ^
/<>/debug/burrows/apps/example/gif/di/component/DaggerAppComponent.java:84: warning: [deprecation] Fragment in android.app has been deprecated
            .<Class<? extends Fragment>, Provider<AndroidInjector.Factory<? extends Fragment>>>
                                                                                    ^
/<>/debug/burrows/apps/example/gif/di/component/DaggerAppComponent.java:86: warning: [deprecation] Fragment in android.app has been deprecated
        Collections.<String, Provider<AndroidInjector.Factory<? extends Fragment>>>emptyMap());
                                                                        ^
/<>/debug/burrows/apps/example/gif/App_MembersInjector.java:20: warning: [deprecation] Fragment in android.app has been deprecated
  private final Provider<DispatchingAndroidInjector<Fragment>> fragmentInjectorProvider;
                                                    ^
/<>/debug/burrows/apps/example/gif/App_MembersInjector.java:30: warning: [deprecation] Fragment in android.app has been deprecated
      Provider<DispatchingAndroidInjector<Fragment>> fragmentInjectorProvider,
                                          ^
/<>/debug/burrows/apps/example/gif/App_MembersInjector.java:43: warning: [deprecation] Fragment in android.app has been deprecated
      Provider<DispatchingAndroidInjector<Fragment>> fragmentInjectorProvider,
                                          ^
/<>/debug/burrows/apps/example/gif/giflist/GifActivity_MembersInjector.java:15: warning: [deprecation] Fragment in android.app has been deprecated
  private final Provider<DispatchingAndroidInjector<android.app.Fragment>>
                                                               ^
/<>/debug/burrows/apps/example/gif/giflist/GifActivity_MembersInjector.java:26: warning: [deprecation] Fragment in android.app has been deprecated
      Provider<DispatchingAndroidInjector<android.app.Fragment>> frameworkFragmentInjectorProvider,
                                                     ^
/<>/debug/burrows/apps/example/gif/giflist/GifActivity_MembersInjector.java:39: warning: [deprecation] Fragment in android.app has been deprecated
      Provider<DispatchingAndroidInjector<android.app.Fragment>> frameworkFragmentInjectorProvider,
                                                     ^
Note: /<>/kapt/debug/burrows/apps/example/gif/di/component/DaggerAppComponent.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
12 warnings

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 6s
18 actionable tasks: 17 executed, 1 up-to-date

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
ronshapirocommented, Aug 29, 2018

Or, alternatively, we could copy any @SuppressWarnings that are on the @Component definition. This seems like the most flexible and least awkward approach. @netdpb @cgdecker @kluever what do you think?

1reaction
TWiStErRobcommented, Oct 17, 2019

Yes, please copy SuppressWarnings, Suppress and SuppressLint annotations.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fragment | Android Developers
Returns the Transition that will be used to move Views out of the scene when the fragment is removed, hidden, or detached when...
Read more >
Fragments deprecated in Android P - Stack Overflow
I think that's the reason why Android P doesn't adopt Lifecycle . Consequently Fragment is deprecated in Android P.
Read more >
Templates use deprecated classes if theme isn't appcompat ...
I think we should use android.support.v4.app.Fragment or androidx.fragment.app.Fragment in the imporrt statement. import ${getMaterialComponentName('android${ ...
Read more >
Android - onCreateOptionsMenu is deprecated - YouTube
Links :GitHub repo ▻ https://github.com/MrBean355/youtube- android /tree/ ... the demo app [0:50] Using MenuProvider in fragments [6:08] Using ...
Read more >
FragmentTransaction.Hide(Fragment) Method (Android.App)
Caution. deprecated. Hides an existing fragment. C# Copy. [Android.Runtime.Register("hide", "(Landroid/app/Fragment;)Landroid/app/FragmentTransaction;", ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found