简体   繁体   English

将Java文件导出为android中的外部jar

[英]Export java file as external jar in android

how can i export one of my classes (non activity) and use it as external jar in my application. 我如何导出我的一个类(非活动类)并将其用作应用程序中的外部jar。 I need to do this because of reasons. 由于种种原因,我需要这样做。

I have tried to remove my package name from top of my class. 我试图从班级顶部删除我的包裹名称。 Then via eclipse export as jar file and added to my projects. 然后通过eclipse导出为jar文件并添加到我的项目中。 I got no errors in the code but when I ran the application it crashed with: 我在代码中没有错误,但是当我运行应用程序时,它崩溃了:

E/AndroidRuntime(12762): FATAL EXCEPTION: main
E/AndroidRuntime(12762): java.lang.Error: Unresolved compilation problem: 
E/AndroidRuntime(12762): The declared package "" does not match the expected package "com.myapp"

The thing is I wish that this jar can be used in any other application and package 我希望这个jar可以在任何其他应用程序和程序包中使用

Some solutions: 一些解决方案:
1) You can write a little project with this class, and make it library (project Properties -> Andoid -> enable Is Library -> OK). 1)您可以使用此类编写一个小项目,并将其设置为库(项目属性-> Andoid->启用Is Library-> OK)。 Then you can include this lib-project in another project (project Properties -> Andoid -> Add -> pick this lib-projectt -> OK.) 然后,您可以将该lib-project包含在另一个项目中(项目Properties-> Andoid-> Add->选择此lib-projectt-> OK。)
2) You can create class with same name, and then copy past all code into. 2)您可以使用相同的名称创建类,然后将过去的所有代码复制到其中。
3) You can create jar file with this class, and then include it as library. 3)您可以使用此类创建jar文件,然后将其包含为库。 Use this tutorial to create jar file. 使用本教程创建jar文件。

Eclipse automatically generates .jar files of your library projects in the /bin folder. Eclipse会自动在/bin文件夹中生成您的项目的.jar文件。 You can copy this file and use it in other projects. 您可以复制此文件并在其他项目中使用它。

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

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