25

My desired end result is to have a Project that can output different productflavors of Mobile, Tv, and Wear apps, that share the same code-base. So I would like the codebase to be a dependency for all three app-types, where each app module would only contain activities and interaction with the codebase.

My first idea was to add a "Android Library Module" to my newly created project, but I noticed that this is really just a new app module with its own resources and everything. I would like the codebase to function more like a "Java Library", but it needs access to packages like "android.graphics.Color".

So in short, is the correct way of achieving this result to use a java library that has a reference to an android sdk or am i just going about this the wrong way?

Continuation of this question at:Does an Android Library need a manifest ,app_name,Icon?

Community
  • 1
  • 1
BillHaggerty
  • 6,157
  • 10
  • 35
  • 68

3 Answers3

27

There's no in-between. If you want access to Android APIs, then the library needs to be an Android library so that the build system can properly link it in to dependent projects. It's true that Android Libraries have resources and other things you may not need, but you can ignore those bits and treat it essentially as a plain Java library if you wish. Even if you're not using resources, you may find useful the ability to specify AndroidManifest.xml attributes to be merged into the dependent app.

The Android Library project doesn't build a fully-fledged APK as its output; it generates an AAR, which is conceptually similar to a JAR archive, but has resources and meta-information useful to Android projects.

Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • So this AAR will be built into the dependents apk structure, and the same device will be capable of having two different "productFlavor apks" that are using the same AAR or different versions of the AAR, is that correct? – BillHaggerty Jan 26 '15 at 18:53
  • Yes, different flavors can depend on the same AAR. If you need different versions of the library, you'll need different AARs, and will need to have flavor-specific dependencies on the different AARs. That scenario is pretty complicated, though, so if you think you need to do that, you should look at it really carefully and make sure there's not a simpler way. – Scott Barta Jan 26 '15 at 18:55
  • My company makes apps that are branded for other companies. Right now we have a pretty lame build system that programmably switches resources,java files, and renames packages. The problem is that its pretty complicated and people are afraid to make changes like updating android build tools or anything that could effect the build process. I will definitely look into finding the simplest way. Thanks. – BillHaggerty Jan 26 '15 at 19:00
  • Yours is a fairly common use case. If you do some searching around you'll find some SO questions about people doing exactly what you're doing, and you'll find some guidance about how to use flavors to set that up, which is definitely what you need. If you have questions after doing your research, post a new question. – Scott Barta Jan 26 '15 at 19:02
  • Ok, I have seen a lot of related stuff but I was under the impression that my use-case was different then many of the examples. I will look into this again. – BillHaggerty Jan 26 '15 at 19:07
  • If you're trying to do your customer-specific flavors in a library project and not your main project (not sure if this is what you're doing or not), then it's going to make things harder for sure. You can do flavors in libraries and choose which one to depend on in the main build, but the support for it is kind of clunky. – Scott Barta Jan 26 '15 at 19:09
  • I am trying to have the Library module be completely generic(everything is in one project). The flavors will(I hope) be defined in the App modules like Mobile,tv,wear. I think this will make it easier. Right now I am trying to get the code-base to be more like a library rather then an app. – BillHaggerty Jan 26 '15 at 19:16
  • I'm pretty sure you can just add android.jar as a dependency for the modules without needing to make it an Android Library Module as well. That should only be required if you need to access the resources or define a manifest. – corsair992 Jun 21 '15 at 04:18
  • @corsair992 Would you mind editing your answer to briefly explain how to add android.jar as a dependency to a module? – Shailen Aug 23 '15 at 14:30
  • @shailenTJ: It's not _my_ answer. I imagine you can declare a dependency on android.jar just like any other dependency. – corsair992 Aug 23 '15 at 15:01
  • @corsair992 I meant your comment :) In AS, in the project view, I can see `android.jar` in the External Libraries section. However, if I just have a Java Module (not an Android Library module), I cannot find a way to add android.jar as a dependency. Even in the module settings, I cannot find a way to do this. How did you do this? – Shailen Aug 23 '15 at 15:03
  • @shailenTJ: I guess copy it in your "libs" folder? I have never created a library myself. – corsair992 Aug 23 '15 at 15:26
  • @corsair992, yes it was tempting to do so, but the Android *app* module already relies on the External Libraries which link directly to `.jar` files from my Android SDK folder. The SDK Manager updates them frequently and I quasi do not maintain them at all. I want to access the same libraries without having a duplicate copy. See what I mean? – Shailen Aug 23 '15 at 15:38
  • @shailenTJ: The [Android Gradle plugin](https://developer.android.com/tools/building/plugin-for-gradle.html) takes care of importing the correct version of android.jar from the SDK according to the `compileSdkVersion` value. However, since they don't maintain it in a Maven repository, you can't reference it directly in a Java project without manually importing it or writing a custom plugin yourself. The "External Libraries" section merely list the external dependencies that are imported by Gradle. – corsair992 Aug 23 '15 at 15:44
  • 1
    @shailenTJ If you have a Java library that depends on android.jar and you try to include it into an Android app project, you're likely to have all sorts of build problems because the build system will be seeing two copies of the Android API classes, one of which it doesn't like. You might want to reframe what you're trying to do as a new question. – Scott Barta Aug 24 '15 at 15:27
  • @ScottBarta: It should not cause issues if you're using a proper build system (Gradle, Maven, etc.). Lots of libraries have dependencies on various versions of android.jar (e.g. Retrofit). – corsair992 Sep 04 '15 at 23:22
  • It would be useful to move any useful information from the comments into the answer and then clean up the comments. – Suragch Jan 25 '17 at 05:49
15

Supplemental answer defining terms

The Android Studio documentation defines a module as follows:

A module is a collection of source files and build settings that allow you to divide your project into discrete units of functionality. Your project can have one or many modules and one module may use another module as a dependency. Each module can be independently built, tested, and debugged.

So basically a module is a subproject in your bigger project.

Types of modules

  • Android app module - The app module is usually what you are working with in a normal project. When you compile it you get an APK file that will run on a device. Here are the different types of app modules that exist:
    • Phone & Tablet Module
    • Android Wear Module
    • Android TV Module
    • Glass Module
  • Library module - The purpose of a library is to share code. For example, you could have a project with a couple different app modules. The common code that they both use could be located in the library.
    • Android Library - In addition to Java code, this allows you to also include Android resource files and a manifest. If you are making an Android project and are wondering what kind of library to use, then choose the Android Library. When compiled it creates an AAR (Android Archive) file.
    • Java Library - This only allows you to include Java code files, no Android resource files. This is useful for cross-platform code sharing. When compiled it creates a JAR (Java Archive) file.
  • Google Cloud module - This type of module is the Google Cloud backend for communication with your client side app.
Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
9

One additional point that I've not seen well documented: An android library module can have a dependency on another android library module or java library module, but a java library module cannot have a dependency on an android library module.

tom
  • 131
  • 1
  • 1