Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
544 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...