简体   繁体   中英

Cannot use android-maps-utils in module

I have a project which contains two modules; the main app module and another module which I include in the app module, let's call this module myModule.

I want to use SphericalUtil in myModule, which is included in android-maps-utils, so I've added compile 'com.google.maps.android:android-maps-utils:0.5+' to the build.gradle file belonging to myModule .

The strange thing is that I cannot import any classes belonging to the android-maps-utils library, it seems to just not exist in myModule, even though I get no gradle errors. When I type SphericalUtil in any of the classes belonging to myModule I get the suggestion to ' Add library android-maps-utils to classpath ', but it doesn't seem to be doing anything.

When I add the exact same line in the app module gradle file, it does seem to work. I can type SphericalUtil in any class belonging to the app module and Android Studio recognizes the class and allows me to auto-import it.

I've also tried with a different library, eg okhttp, but no issues there, I can autoimport and use it in myModule, so the issue seems to be correlated to android-maps-utils . Maybe following the suggestion to add the library to the classpath broke things? I'm really out of guesses, so any help is really appriciated.

If it helps, myModule gradle file looks like this:

apply plugin: 'java-library'

repositories {
    maven { url "https://maven.google.com" }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    // https://mvnrepository.com/artifact/com.google.android/android
    compileOnly group: 'com.google.android', name: 'android', version: '4.1.1.4'

    compile 'com.google.maps.android:android-maps-utils:0.5+'

    compile 'com.squareup.okhttp3:okhttp:3.8.1'

    compile files('libs/experiment.resultlogger-0.0.2.jar')
}

sourceCompatibility = "1.7"
targetCompatibility = "1.7"

You need to use Android module plugin instead of java plugin. As the following documentation says:

To create a new library module in your project, proceed as follows:

  1. Click File > New > New Module.

  2. In the Create New Module window that appears, click Android Library, then click Next. There's also an option to create a Java Library, which builds a traditional JAR file. While a JAR file is useful for many projects—especially when you want to share code with other platforms—it does not allow you to include Android resources or manifest files, which is very useful for code reuse in Android projects. So this guide focuses on creating Android libraries.

3.Give your library a name and select a minimum SDK version for the code in the library, then click Finish.

To tackle the AndroidManifest.xml problem, you can use something like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.your.library"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM