Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best practice for sharing classes between a Wear and Android app?

Tags:

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?

like image 665
Robin Eisenberg Avatar asked Jul 06 '14 01:07

Robin Eisenberg


People also ask

Can be used to share data between different applications on Android?

The Android intent resolver is best used when sending data to another app as part of a well-defined task flow. To use the Android intent resolver, create an intent and add extras as you would if you were to call the Android Sharesheet. However, do not call Intent.

What does application class do in Android?

The Application class in Android is the base class within an Android app that contains all other components such as activities and services. The Application class, or any subclass of the Application class, is instantiated before any other class when the process for your application/package is created.


1 Answers

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.

like image 99
matiash Avatar answered Sep 28 '22 07:09

matiash