简体   繁体   English

如何从库项目中调用主项目中的类/方法?

[英]How can I call a class/method in main project from library project?

I have created two projects. 我创建了两个项目。 One is a main application and second is just for the player.I am using player project as a library for the main project. 一个是主要应用程序,第二个仅用于播放器。我正在使用播放器项目作为主要项目的库。 Now, I have a feature in both project which allows users to share the video. 现在,我在两个项目中都有一个功能,该功能允许用户共享视频。 I have done all sharing implementation and methods are in main project in a particular class called ShareActivity.class. 我已经完成了所有共享实现,并且方法都在名为ShareActivity.class的特定类的主项目中。 Is there any way, I can use ShareActivity.class in Player library project? 有什么办法可以在Player库项目中使用ShareActivity.class?

I am thinking below solutions. 我在想以下解决方案。

1) Should I use my library project as a jar file? 1)我应该将我的图书馆项目用作jar文件吗? Will it allow me to use a class from main project in library project? 是否可以在图书馆项目中使用主项目中的类?

2) Should I give reference of main project in a library project? 2)我应该在图书馆项目中引用主要项目吗? I tried that but It is going in to infinite loop while building. 我试过了,但是在构建时会陷入无限循环。

Please Advise! 请指教!

You can use classes that are in a library project in your main project, but not the other way round. 您可以在主项目中使用库项目中的类,但反之则不能。 If both apps need a particular feature, move it to the library. 如果两个应用程序都需要特定功能,请将其移至库中。

Instead of having an explicit Intent in your library project (with a reference to ShareActivity ), you could use an implicit Intent in your library project. 可以在库项目中使用隐式Intent ,而不是在库项目中具有显式Intent (通过引用ShareActivity )。 Then your ShareActivity class would register to receive that implicit Intent . 然后,您的ShareActivity类将注册以接收ShareActivityIntent Reference 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM