java - 无法在模块中使用 android-maps-utils

标签 java android gradle android-maps-utils

我有一个包含两个模块的项目; main 应用程序模块和我包含在应用程序模块中的另一个模块,我们将此模块称为 myModule。

我想在 myModule 中使用 SphericalUtil,它包含在 android-maps-utils 中,因此我添加了 compile 'com.google.maps.android:android-maps-utils:0.5+' 到属于 myModule 的 build.gradle 文件。

奇怪的是,我无法导入属于 android-maps-utils 库的任何类,它似乎不存在于 myModule 中,即使我没有收到 gradle 错误。当我在属于 myModule 的任何类中输入 SphericalUtil 时,我收到“将库 android-maps-utils 添加到类路径”的建议,但它似乎没有执行任何操作。

当我在应用程序模块 gradle 文件中添加完全相同的行时,它似乎确实有效。我可以在属于应用模块的任何类中输入 SphericalUtil,Android Studio 会识别该类并允许我自动导入它。

我也尝试过使用不同的库,例如okhttp,但没有问题,我可以自动导入并在 myModule 中使用它,因此该问题似乎与 android-maps-utils 相关。也许按照将库添加到类路径的建议会破坏事情?我真的无法猜测,所以非常感谢任何帮助。

如果有帮助,myModule gradle 文件如下所示:

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"

最佳答案

您需要使用Android模块插件而不是java插件。作为following documentation说:

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.

要解决 AndroidManifest.xml 问题,您可以使用如下内容:

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

关于java - 无法在模块中使用 android-maps-utils,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48882146/

相关文章:

java - isInitialized - lateinit var 的支持字段此时不可访问

java - 我称之为 glOrtho 的矩阵模式重要吗?

java - Gradle 排除 VS 包含

Android - 如何排除内部两个级别的组?

gradle - 在子项目中找不到参数的方法实现

java - 在 Java 中,如何将 String 值与 String 类型的 ArrayList 进行比较?

Java将原始字节编码为图像简单图像格式/文件

Android 多次使用相同的 fragment - 如何有不同的点击 Action

android - adb 设备返回错误 : protocol fault (no status)

android - 膨胀类 android.support.design.widget.FloatingActionButton 时出错