Cannot resolve method 'of' in 'ImmutableList

Viewed 708

Following a tutorial for Android Billing 5.0. The 'of' in ImmutableList is flagged red and the error in Android Studio is

Cannot resolve method 'of' in 'ImmutableList'"

How can I resolve?

QueryProductDetailsParams queryProductDetailsParams =
        QueryProductDetailsParams.newBuilder()
                .setProductList(
                        ImmutableList.of(
                                QueryProductDetailsParams.Product.newBuilder()
                                        .setProductId(PREMIUM_MONTHLY_VERSION_ID)
                                        .setProductType(BillingClient.ProductType.SUBS)
                                        .build()))
                .build();
1 Answers

ImmutableList, in context of 'a tutorial for Android Billing 5.0', is clearly referring to guava's ImmutableList - the full name of this type is com.google.common.collect.ImmutableList.

This class is baked into android as far as I remember (but not in plain java). It has always had an of method. Thus:

  • Most likely you have some type in your package or source file called ImmutableList. Don't do that. Rename yours to something else.
  • Alternatively, check your imports - you imported something else also named ImmutableList. You should have an import com.google.common.collect.ImmutableList; at the top, or possibly import com.google.common.collect.*; - if that is missing, consider adding it.

Android doesn't "have" java 9 or java 11 - you just need java1 for this, the point is: It is not a java core class at all - only stuff that starts with java.* is. However, android is not (quite) java.

I'm just not sure: I thought guava (the library that contains ImmutableList) is available by default on android, which means your IDE"s setup is broken. Or perhaps it is not, in which case you need to add the 'guava' dependency. How? Well, that depends - I think android builds are based on gradle, which would mean: Look it up on search.maven.org and follow the gradle instructions.

Related