Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio Java Library Module vs. Android Library Module

Tags:

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?

like image 216
BillHaggerty Avatar asked Jan 26 '15 16:01

BillHaggerty


People also ask

Can I use Android library in Java?

An Android library module can contain Java classes, Android components and resources. Only assets are not supported. The code and resources of the library project are compiled and packaged together with the application.

What is library module in Android Studio?

An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest.

What is the difference between AAR and jar?

The main difference is aar is splitted inside android to jar. If your app will be used only in user app only in android studio then aar is preferred. If you are planning for app to communicate with c/c++ compiled lib.so file jar is preferred.

What is Java library Android?

Like the small description of each project type says, a Android Library is just another Android Application project,often referred to as Module.And Java Library is another word for a java project.The only difference between a module and a project is the complexity.


2 Answers

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.

like image 197
Scott Barta Avatar answered Sep 18 '22 03:09

Scott Barta


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.
like image 20
Suragch Avatar answered Sep 18 '22 03:09

Suragch