简体   繁体   English

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

[英]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. main应用程序模块和我包含在应用程序模块中的另一个模块,我们将此模块称为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 . 我想在Mymodule中,包含在Android的地图-utils的使用SphericalUtil,所以我已经添加compile 'com.google.maps.android:android-maps-utils:0.5+'到的build.gradle文件属于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. 奇怪的是,我无法导入属于android-maps-utils库的任何类,即使我没有gradle错误,它似乎也不存在于myModule中。 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. 当我在属于myModule的任何类中键入SphericalUtil时,都会得到“ Add library android-maps-utils to classpath ”的建议,但似乎没有做任何事情。

When I add the exact same line in the app module gradle file, it does seem to work. 当我在应用程序模块gradle文件中添加完全相同的行时,它似乎确实起作用。 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. 我可以在属于app模块的任何类中键入SphericalUtil ,Android Studio可以识别该类并允许我自动导入它。

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 . 我也尝试过使用其他库,例如okhttp,但是那里没有问题,我可以自动导入并在myModule中使用它,因此问题似乎与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: 如果有帮助,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"

You need to use Android module plugin instead of java plugin. 您需要使用Android模块插件而不是Java插件。 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. 在出现的“创建新模块”窗口中,单击“ Android库”,然后单击“下一步”。 There's also an option to create a Java Library, which builds a traditional JAR file. 还有一个创建Java库的选项,该库将构建传统的JAR文件。 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. 尽管JAR文件对许多项目有用(尤其是当您想与其他平台共享代码时),但它不允许您包含Android资源或清单文件,这对于Android项目中的代码复用非常有用。 So this guide focuses on creating Android libraries. 因此,本指南重点介绍创建Android库。

3.Give your library a name and select a minimum SDK version for the code in the library, then click Finish. 3.给您的库命名,并为库中的代码选择一个最低的SDK版本,然后单击“完成”。

To tackle the AndroidManifest.xml problem, you can use something like this: 要解决AndroidManifest.xml问题,您可以使用如下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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