Search code examples
androidandroid-gradle-pluginosmdroidandroid-build

Refer to a class in same project, different module


I have the osmdroid code from github and have it in an Android Studio project, with top-level structure like so:

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

Then I added my own module:

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

MyModule has an Activity (an AppCompatActivity) and it runs just fine. Now I want to use the class MapActivity from OpenStreetMapViewer in MyModule So I add the dependency to the build.gradle for MyModule

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

This doesn't' seem to work because:
- When trying to use MapActivity in MyModule, it doesn't know where to find it
- I get this warning:

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

How do I do this?


Solution

  • I'll get flak for this, but if importing a class from another APK is what you need to do, then don't use gradle. Maven can do it via the 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

    The real question should be, "is this the right approach?" (importing an APK as a dependency). To which, I'd answer, considering the state of the current build tool support with gradle, no. You are probably better off either copying the relevant portions of the MapActivity into your own project or starting from scratch and only import the libraries you need as dependencies. In other words, your gradle file should contain this (which is here: https://github.com/osmdroid/osmdroid/wiki/How-to-add-the-osmdroid-library-via-Gradle) :

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

    and then follow the rest of the how to guides located on the osmdroid wiki here: https://github.com/osmdroid/osmdroid/wiki/How-to-use-the-osmdroid-library for how to use it to suite your needs.

    There's a number of ways to go about making osmdroid bend to your needs. If you can't make it do what you want, open an issue on github. I am a contributor and have write access and can help out.