Compilation error `error: cannot access LatLngBounds` when calling constructor with same arity from another module

Viewed 123

I have this class public class LatLngBounds in module A that has 2 constructors:

public LatLngBounds(com.google.android.gms.maps.model.LatLngBounds google, com.huawei.hms.maps.model.LatLngBounds huawei)

public LatLngBounds(LatLng latLng1, LatLng latLng2)

So when I call the second constructor from another module B, I get the following compilation error:

error: cannot access LatLngBounds
    private static final LatLngBounds ADELAIDE = new LatLngBounds(

However if I remove the first constructor, the error is gone. Note that marking it as private does not solve the problem.

I found out that I get this error only if there is another constructor with exactly same arity as the second constructor above. Other constructors that do not fulfil this condition do not trigger this error. It seems like this error is thrown only when the compiler needs to check overloaded constructors.

Note that module B does not have dependencies on com.google.android.gms.maps.model.LatLngBounds or com.huawei.hms.maps.model.LatLngBounds.

So my take on this is that it seems that when the compiler is trying to check this other constructor, it fails to find these classes and triggers the error.

You can check the problem in this repo, checkout tag 1.1.2.

So why is this error thrown? How can I keep both 2 constructors?

1 Answers

TLDR: Use api instead of implementation for maps dependency in module A, or add maps dependency to module B as well.

    api 'com.google.android.gms:play-services-maps:17.0.0'
    api 'com.huawei.hms:maps:5.0.1.301'

It is understandable from the aspect of transitive dependency. With implementation dependency in module A, the classes in that dependency is not visible to other module (module B). So you need to add that dependency in module B as well. implementation dependency help build be faster or module independency so this is recommended.

However, you can change this behavior by using api. With api, that dependency is transitively added to dependency of module B.

Detail: https://developer.android.com/studio/build/dependencies#dependency_configurations

Related