如何使的.jar从一个Android Studio项目 [英] How to make a .jar out from an Android Studio project

查看:165
本文介绍了如何使的.jar从一个Android Studio项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AndroidStudio,我有这个项目,如下所示:

I'm using AndroidStudio and I have this project as shown:

什么是蓝色的圆圈内是MYLIB。 MYLIB还需要使用外部的lib是红色的圆圈内,和一个Apache包(绿圈)。

What is inside the blue circle is myLib. myLib also needs to use an external lib that is inside the red circle, and an apache package (green circle).

所以,我要让这件事成为一个单一的.jar,这样我就可以在其他项目中使用。

So I want to make this whole thing become a single .jar, so I can use it in another projects.

一个一步一步的指导,将是非常美联社preciated,我在开发者世界的初学者。

A step-by-step guide would be really appreciated, I'm a beginner in the developer world.

谢谢!

推荐答案

如果您设置code在摇篮一个普通的Java模块,那么它真的很容易有摇篮给你的内容的jar文件。该JAR文件将只有你的code,不是依赖于其他Apache库。我建议你​​分配这样说;这是一个有点怪异到您的图书馆内捆绑的依赖,这是比较正常的这些库的用户必须包括对自己的依赖关系(否则有这些项目的冲突已链接库的副本,也许是不同版本的)。更重要的是,可以避免周围的重新分配其他人的code,如果你发布你的库的潜在许可问题。

If you set up the code as a plain Java module in Gradle, then it's really easy to have Gradle give you a jar file with the contents. That jar file will have only your code, not the other Apache libraries it depends on. I'd recommend distributing it this way; it's a little weird to bundle dependencies inside your library, and it's more normal for users of those libraries to have to include those dependencies on their own (because otherwise there are collisions of those projects are already linking copies of the library, perhaps of different versions). What's more, you avoid potential licensing problems around redistributing other people's code if you were to publish your library.

取code,它也需要被编译成一个罐子,并将其移动到一个单独的普通的Java模块机器人工作室:

Take the code that also needs to be compiled to a jar, and move it to a separate plain Java module in Android Studio:

  1. 文件菜单> 新模块... > Java库
  2. 设置了图书馆,Java包的名称,并在向导的类名。 (如果你不希望它来创建一个类你,你可以直接删除它,一旦模块创建)
  3. 在你的Andr​​oid code,成立了相关的新模块上,这样就可以用code。在新的库:
  4. 文件> 项目结构> 模块> (你的Andr​​oid模块)> 依赖> + > 模块依赖。见下面的截图:
  5. 选择从列表中出现的对话框中你的模块:
  1. File menu > New Module... > Java Library
  2. Set up the library, Java package name, and class names in the wizard. (If you don't want it to create a class for you, you can just delete it once the module is created)
  3. In your Android code, set up a dependency on the new module so it can use the code in your new library:
  4. File > Project Structure > Modules > (your Android Module) > Dependencies > + > Module dependency. See the screenshot below:
  5. Choose your module from the list in the dialog that comes up:

希望您的项目现在应该建立正常。当你做一个构建,为你的Java库jar文件将被放置在建立/库目录中的模块的目录。如果你想建立手工jar文件,您可以运行它建立从摇篮窗口文件的任务:

Hopefully your project should be building normally now. After you do a build, a jar file for your Java library will be placed in the build/libs directory in your module's directory. If you want to build the jar file by hand, you can run its jar build file task from the Gradle window:

这篇关于如何使的.jar从一个Android Studio项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆