在同一项目,不同模块中引用 class

Refer to a class in same project, different module

我有来自 github 的 osmdroid 代码,并将其放在 Android Studio 项目中,顶层结构如下:

+OpenStreetMapViewer  
+osmdroid-android  
+osmdrod-packager  
+osmdroid-third-party  
+sample  
+Gradle Scripts  

然后我添加了自己的模块:

+OpenStreetMapViewer
+MyModule
+osmdroid-android
+osmdrod-packager
+osmdroid-third-party
+sample
+Gradle Scripts

MyModule 有一个 Activity(一个 AppCompatActivity)并且它运行得很好。现在我想在 MyModule 中使用 OpenStreetMapViewer 中的 class MapActivity 所以我将依赖项添加到 build.gradle for MyModule

dependencies {
    ...
    compile project(':OpenStreetMapViewer')
}

这似乎行不通,因为:
- 当试图在 MyModule 中使用 MapActivity 时,它不知道在哪里可以找到它
- 我收到此警告:

Warning:Dependency org.osmdroid:OpenStreetMapViewer:5.0-SNAPSHOT on project emnrdstickylistheaders resolves to an APK archive which is not supported as a compilation dependency. 
File: D:\Users\myusername\AndroidstudioProjects\osmdroid-stickylist\OpenStreetMapViewer\build\outputs\apk\OpenStreetMapViewer-release-unsigned.apk

我该怎么做?

我会为此受到抨击,但如果从另一个 APK 导入 class 是您 需要 做的,那么请不要使用 gradle. Maven 可以通过 maven-android-plugin https://github.com/simpligility/android-maven-plugin. In fact, that's what osmdroid does for the integration tests. It's an APK project that depends on an APK. Of course, the downside to this is that the support for using maven as a build tool with android studio is terrible. It's slightly better with Intellij, but still has a number of open issues, such as this one https://youtrack.jetbrains.com/issue/IDEA-145825

真正的问题应该是,"is this the right approach?"(导入 APK 作为依赖项)。对此,我会回答,考虑到 gradle 当前构建工具支持的状态,不。您最好将 MapActivity 的相关部分复制到您自己的项目中,或者从头开始,只导入您需要的库作为依赖项。换句话说,你的 gradle 文件应该包含这个(在这里:https://github.com/osmdroid/osmdroid/wiki/How-to-add-the-osmdroid-library-via-Gradle):

dependencies { compile 'org.osmdroid:osmdroid-android:5.0.1@aar' }

然后按照位于 osmdroid wiki 上的其余操作指南:https://github.com/osmdroid/osmdroid/wiki/How-to-use-the-osmdroid-library 了解如何使用它来满足您的需求。

有多种方法可以让 osmdroid 满足您的需求。如果你不能让它做你想做的事,请在 github 上提出一个问题。我是一名贡献者,拥有写入权限,可以提供帮助。