简体   繁体   English

在Android Studio中生成Jar文件

[英]Generating Jar file in Android Studio

I have a library project in Android Studio. 我在Android Studio中有一个库项目。 I want to generate a .jar file for this library. 我想为这个库生成一个.jar文件。 I need the jar file contain/include all the dependencies this Library project has. 我需要jar file contain/include all the dependencies此库项目所具有的jar file contain/include all the dependencies

The problem is I couldn't find a way of generating a jar file. 问题是我找不到生成jar文件的方法。 Is there any way I can generate a jar file for my project? 有什么办法可以为我的项目生成一个jar文件吗?

Best Regards 最好的祝福

Here is an example of Gradle task which creates .jar from classes.jar . 以下Gradle任务的示例,该任务从classes.jar创建.jar

classes.jar file can be found in /build/intermediates/exploded-aar/ folder. classes.jar文件可以在/build/intermediates/exploded-aar/文件夹中找到。

Edit: Create jar file for every Android Librar y project and distribute them standalone. 编辑:为每个Android Librar项目创建jar文件并单独分发。 What about dependencies required by library projects - add those dependencies in final project, where your library will be used. 库项目所需的依赖项如何 - 在最终项目中添加这些依赖项,将使用您的库。

For example: you Android Library project MyLibProject , which has following dependencies in build.gradle 例如:您的Android Library项目MyLibProject ,它在build.gradle具有以下依赖build.gradle

dependencies {
    compile "com.android.support:support-v4:19.+"
}

When you'll build your project you'll get classes.jar for MyLibProject , and those classes.jar will contain just class from library project, but not from com.android.support:support-v4:19.+ . 当您构建项目时,您将获得MyLibProject classes.jar ,而那些classes.jar将只包含来自库项目的类,但不包含来自com.android.support:support-v4:19.+

After that in another project you add classes.jar as library dependency in build.gradle and also define same dependencies as in MyLibProject . 之后,在另一个项目中添加classes.jar在库相关build.gradle也定义相同的依赖在MyLibProject

And if you still need fatJar , look here how to create fatJar with Gradle . 如果你还需要fatJar ,请看这里如何使用Gradle创建fatJar

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. 如果您在Gradle中将代码设置为普通Java模块,那么让Gradle为您提供包含内容的jar文件非常容易。 That jar file will have only your code, not the other Apache libraries it depends on. 该jar文件只包含您的代码,而不是它依赖的其他Apache库。 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. 更重要的是,如果您要发布图书馆,可以避免重新分发其他人的代码时出现潜在的许可问题。

Take the code that also needs to be compiled to a jar, and move it to a separate plain Java module in Android Studio: 将需要编译的代码转换为jar,并将其移至Android Studio中的单独普通Java模块:

File menu > New Module... > Java Library
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)
In your Android code, set up a dependency on the new module so it can use the code in your new library:
File > Project Structure > Modules > (your Android Module) > Dependencies > + > Module dependency. See the screenshot below: 

在此输入图像描述

Choose your module from the list in the dialog that comes up: 从出现的对话框的列表中选择您的模块:

在此输入图像描述

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. 完成构建后,Java库的jar文件将放在模块目录的build / libs目录中。 If you want to build the jar file by hand, you can run its jar build file task from the Gradle window: 如果要手动构建jar文件,可以从Gradle窗口运行其jar构建文件任务:

在此输入图像描述

Oroginal article here : How to make a .jar out from an Android Studio project 这里的Oroginal文章: 如何从Android Studio项目中创建.jar

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

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