27

I'm using a class that I serialize in my Android Phone app, and deserialize in my Android Wear app. They are both in the same Android Studio Project, and are deployed as one.

How can I share the class between the two without having a copy of the class in each package? Right now I'm copy/pasting it, but is there any way for me to include it in both apps?

The directory structure being:

./
    mobile/
           ...src/etc
    wear/
           ...src/etc

How do I handle common classes?

Robin Eisenberg
  • 1,836
  • 18
  • 26

2 Answers2

41

You can create a new "Android Library" Module in the project, and place your common classes there.

Then you simply add it as a dependency of both the Mobile and Wear modules (in Project Structure -> Dependencies -> Add -> Module dependency). That way you can create/use instances of these classes from both modules.

matiash
  • 54,791
  • 16
  • 125
  • 154
  • Yep, that did it :) That's a lot of files created just to include a few classes between two projects. I'm just comparing to a C++ solution on Visual Studio, where two projects can share a class with a simple #include. Anyways thank you, that works perfectly. – Robin Eisenberg Jul 06 '14 at 02:30
  • 2
    @RobinEisenberg heh, you're right. In Java there is a much tighter coupling between files/directories and compilation units. As a bonus, though, the class should be compiled only once with this approach (it's like, say, a lib in C/C++). – matiash Jul 06 '14 at 02:32
  • Shall we delete AndroidManifest.xml from this library module? – rudakovsky Feb 23 '16 at 16:00
  • 4
    @rudakovsky Not necessarily. The manifests are merged, so it may be better to leave the declarations where appropriate. For example, if a class in the library requires a certain permission, then it's fine (I would even say better) if the library's manifest declares the permission. That way it will be automatically included in any application module that references it. – matiash Mar 29 '16 at 19:43
  • Is there any way to use this solution to share the Module Library between two distinct AndroidStudio projects? – GozzoMan May 28 '18 at 09:23
  • How about if you have json data rhat you want to share between multiple apps? – Chosen Sep 04 '18 at 03:22
  • 2
    @MujtabaMahmood The same way, basically -- you could include it either as an asset or raw resource of the Library module, and use it from the app module. – matiash Sep 05 '18 at 08:36
-1

Usually for share a code base the modularization is used. AndroidStudio uses module naming.

A module is a collection of source files and build settings that allow you to divide your project into discrete units of functionality.

It can be Android Library(.aar) or Java Library(.jar). In both cases a new module will be created with apply plugin: 'com.android.library' line in build.gradle file. You can use it to put a shared code.

To include/add this module you should use

File -> New -> Import Module...

This command add all necessary information into settings.gradle file and you will be able use it via

implementation project(':<module_name>')
yoAlex5
  • 29,217
  • 8
  • 193
  • 205