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.
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:
ImmutableList
. Don't do that. Rename yours to something else.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.