10

Debugging Details: Following tutorial to migrate from Android Billing 4.0 to 5.0 https://developer.android.com/google/play/billing/migrate-gpblv5, specifically in the section "Showing products available to buy" The 'of' in ImmutableList is flagged red and the error in Android Studio is

Cannot resolve method 'of' in 'ImmutableList'"

How can I resolve?

Minimal reproducible code to get the Compilation Error:

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

Here are the specific details:

Desired Behaviour: Code compiles fine.

Specific Problem or Error: Compilation error.

TomV
  • 1,157
  • 1
  • 13
  • 25
  • 1
    What is this `ImmutableList` class? If this is part of core Java, then I'm not familiar with it. – Hovercraft Full Of Eels Jun 14 '22 at 10:34
  • 2
    @HovercraftFullOfEels Likely from Guava. – Michael Jun 14 '22 at 10:40
  • 1
    @Michael: thanks, then this begs the question, does Guava's ImmutableList class have an `.of(...)` method that takes the parameters that are currently being passed into it? And has it been properly imported, or is it conflicting with another similarly named class? – Hovercraft Full Of Eels Jun 14 '22 at 10:45
  • Maybe there is Method Confliction from a different library. like import from another library and method use of other library – Vikas Jun 14 '22 at 11:30
  • Thanks for your help so far. I'm now fully qualifying it as com.google.common.collect.ImmutableList but doesn't recognised the 'collect' in the package. I'm led to believe it needs java 9. But Android Studio shows I'm using JDK 11 – TomV Jun 14 '22 at 11:36

1 Answers1

11

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.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • 14
    Solved: Need to add implementation 'com.google.guava:guava:24.1-jre' in build.gradle dependencies. Thanks to Michael, Hovercraft Full of Eels and @rzwitserloot for leading me down the right path! – TomV Jun 14 '22 at 12:23
  • 1
    @TomV nice! Apologies for the vagueness in this answer - I didn't have an android environment ready to check a few things, and gradle is not my usual build system. – rzwitserloot Jun 14 '22 at 12:30
  • 2
    import com.google.firebase.crashlytics.buildtools.reloc.com.google.common.collect.ImmutableList; implementation 'com.google.firebase:firebase-crashlytics-buildtools:2.9.1' Will work. – tanni tanna Sep 07 '22 at 13:51
  • 13
    'com.google.guava:guava:24.1-jre' may cause Duplicate class issues when building, so it is better to add this to your gradle: implementation 'com.google.guava:guava:27.0.1-android' // For Billing – TomV Sep 09 '22 at 07:36