在 Wear 和 Android 应用之间共享课程的最佳做法是什么? [英] What's the best practice for sharing classes between a Wear and Android app?
问题描述
我正在使用一个在我的 Android 手机应用程序中序列化并在我的 Android Wear 应用程序中反序列化的类.它们都在同一个 Android Studio 项目中,并作为一个部署.
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?
目录结构为:
./
mobile/
...src/etc
wear/
...src/etc
我如何处理常见的类?
推荐答案
您可以在项目中创建一个新的Android 库"模块,并将您的常用类放在那里.
You can create a new "Android Library" Module in the project, and place your common classes there.
然后您只需将其添加为 Mobile 和 Wear 模块的依赖项(在项目结构 -> 依赖项 -> 添加 -> 模块依赖项中).这样您就可以从两个模块创建/使用这些类的实例.
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.
这篇关于在 Wear 和 Android 应用之间共享课程的最佳做法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!