java 如何将 Android 库项目转换为外部 JAR?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7973822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:16:47  来源:igfitidea点击:

How to Convert an Android Library Project to an External JAR?

javaandroidjarlibrary-project

提问by uTubeFan

I have an Android library project that has been working for me pretty well, but now I am interested in "converting" it to an external JAR.

我有一个 Android 库项目,对我来说效果很好,但现在我有兴趣将它“转换”为外部 JAR。

How do I do that?

我怎么做?

Can any Android library project be converted to an external JAR? If not, what are the restrictions or limitations?

任何 Android 库项目都可以转换为外部 JAR 吗?如果不是,有什么限制或限制?

回答by CommonsWare

Can any Android library project be converted to an external JAR?

任何 Android 库项目都可以转换为外部 JAR 吗?

Not right now.

不是现在。

If not, what are the restrictions or limitations?

如果不是,有什么限制或限制?

If your library is purely Java code, with no resources, you can create a JAR out of the .classfiles, just as you would with regular Java.

如果您的库是纯 Java 代码,没有资源,您可以从.class文件中创建 JAR ,就像使用常规 Java 一样。

If your library uses resources, you can still create a JAR as above. However, you will need to look up the Rvalues (e.g., R.layout.main) via reflection or getIdentifier(), and your resources will not be packaged in the JAR but would have to be distributed separately.

如果您的库使用资源,您仍然可以像上面一样创建 JAR。但是,您需要通过反射或查找R值(例如,R.layout.maingetIdentifier(),并且您的资源不会打包在 JAR 中,而必须单独分发。

In the future, the build tools should support creating distributable JAR files out of library projects, complete with resources. The current build tools do create JARs, but they are not designed to be distributed but are rather internal build artifacts at the moment.

将来,构建工具应该支持从库项目中创建可分发的 JAR 文件,并附带资源。当前的构建工具确实会创建 JAR,但它们并不是为分布式而设计的,而是目前的内部构建工件。

回答by Sinjo

http://developer.android.com/guide/developing/projects/index.html#LibraryProjectsclaims that you can't export these to an external JAR. Specific quote:

http://developer.android.com/guide/developing/projects/index.html#LibraryProjects声称您无法将这些导出到外部 JAR。具体报价:

Similarly, you cannot export the library project to a self-contained JAR file, as you would do for a true library. Instead, you must compile the library indirectly, by referencing the library in the dependent application and building that application.

同样,您不能将库项目导出到自包含的 JAR 文件,就像您对真正的库所做的那样。相反,您必须通过引用依赖应用程序中的库并构建该应用程序来间接编译库。