Converting an existing Application to a Library

ぐ巨炮叔叔 提交于 2019-12-11 22:49:00

问题


Converting an existing Android application to a library is incredibly simple: All I have to do is check the is Library checkbox in the project's properties.

However, once this easy step has been applied, a much more serious task lies ahead: How to retain the original application code unchanged (as a library!), while building different applications based on it.

That is, I don't really want to add another activity, but rather re-use the original activity (now in a library), where only a few methods (in the one-and-only activity) are different in each derived application.

Is the solution really is as simple as subclassing the library's activity in each application based on it?

What caveats should I watch for?

Is there a better approach?


回答1:


You will mostly have to derive new classes from the ones included in the librairy or call their methods. If your librairy has classes such as activities, applications, you can extends them has you said.

Another solution could be to isolate the code you want to reuse and plugit into other activities by dependency injection. But this framework seems an overkill to me where you can just derive new classes.

A more android specific problem could be : how to call my new activities from within my new applications. The answer here relies in intent and to use different action names to call for the extended activities.

Regards, Stéphane



来源:https://stackoverflow.com/questions/6324234/converting-an-existing-application-to-a-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!