Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hilt] Lint flags generated DialogFragment class with "Error: Use of LayoutInflater.from... detected. Consider using getLayoutInflater() instead" #3222

Closed
alvindizon opened this issue Feb 7, 2022 · 5 comments

Comments

@alvindizon
Copy link

@alvindizon alvindizon commented Feb 7, 2022

I have a class that extends from DialogFragment that is annotated with @AndroidEntryPoint, something like this:

@AndroidEntryPoint
class OurFragment : DialogFragment() {

I upgraded my project's Android Gradle Plugin to 7.2.0-alpha06 and Gradle to 7.3.3, and running lintRelease produces this error:

Error: Use of LayoutInflater.from(FragmentComponentManager.createContextWrapper(inflater, this)) detected. Consider using getLayoutInflater() instead [UseGetLayoutInflater from fragment-1.4.1]
    return LayoutInflater.from(FragmentComponentManager.createContextWrapper(inflater, this));
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

   Explanation for issues of type "UseGetLayoutInflater":
   Using LayoutInflater.from(Context) can return a LayoutInflater             
       that does not have the correct theme.

   Vendor: Android Open Source Project (fragment-1.4.1)
   Identifier: fragment-1.4.1
   Feedback: https://issuetracker.google.com/issues/new?component=192731

Checking the generated class, I can see this:

/**
 * A generated base class to be extended by the @dagger.hilt.android.AndroidEntryPoint annotated class. If using the Gradle plugin, this is swapped as the base class via bytecode transformation.
 */
public abstract class Hilt_OurCardFragment extends DialogFragment implements GeneratedComponentManagerHolder {
   ...
  @Override
  public LayoutInflater onGetLayoutInflater(Bundle savedInstanceState) {
    LayoutInflater inflater = super.onGetLayoutInflater(savedInstanceState);
    return LayoutInflater.from(FragmentComponentManager.createContextWrapper(inflater, this));
  }

I'm using version 2.40.5 of Dagger btw. I also have other classes that extend from Fragment, but they do not get flagged by lint, even if their generated classes also use LayoutInflater.from(FragmentComponentManager.createContextWrapper(inflater, this)). Now I'm not sure this is the correct place to bring this up, feel free to close this if this is not appropriate, but I'd like to bring this to your attention as I'm not sure how to handle this. I'm thinking of temporarily silencing this error via adding this issue to lint.xml.

edit: apologies, other classes extending from Fragment also trigger this lint error. I have silenced this error by adding the UseGetLayoutInflater issue in our lint.xml.

@Chang-Eric
Copy link
Member

@Chang-Eric Chang-Eric commented Feb 8, 2022

Thanks for the report on this. I think we can suppress this in the generated code by adding a @SuppressLint annotation. In this case, unless you are seeing bugs with the theme, suppressing seems like the right call since we are implementing getLayoutInflater() to sneak our Hilt context wrapper in so we can't do what the lint suggests and call it.

@alvindizon
Copy link
Author

@alvindizon alvindizon commented Feb 9, 2022

Hi @Chang-Eric , thanks for your response. Apologies but I don't quite understand what you're saying about adding the annotation--we can't add it to generated code, or are you talking about annotating my Fragment both with @SuppressLint and @AndroidEntryPoint?

@Chang-Eric
Copy link
Member

@Chang-Eric Chang-Eric commented Feb 9, 2022

I meant I will submit a change to Hilt to add it =)

@alvindizon
Copy link
Author

@alvindizon alvindizon commented Feb 9, 2022

Ah got it, @Chang-Eric thanks :)

copybara-service bot pushed a commit that referenced this issue Feb 28, 2022
…t(). This is more correct and removes the need for overriding getSystemService() in our ContextWrapper, however, since users may be depending on this if they used LayoutInflater.from() leaving in that getSystemService() override.

Fixes #3222.

RELNOTES=Fix #3222. Generated fragment code previously would cause a Lint issue around LayoutInflater.from().
PiperOrigin-RevId: 430493436
copybara-service bot pushed a commit that referenced this issue Feb 28, 2022
…t(). This is more correct and removes the need for overriding getSystemService() in our ContextWrapper, however, since users may be depending on this if they used LayoutInflater.from() leaving in that getSystemService() override.

Fixes #3222.

RELNOTES=Fix #3222. Generated fragment code previously would cause a Lint issue around LayoutInflater.from().
PiperOrigin-RevId: 430493436
@Chang-Eric
Copy link
Member

@Chang-Eric Chang-Eric commented Feb 28, 2022

This will be fixed when that is change is released. It ended up that instead of suppressing the warning, we should have been using cloneInContext(). Thanks for bringing this to our attention!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment