Java 如何从 Android Studio 项目中制作 .jar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21712714/
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-08-13 10:13:07  来源:igfitidea点击:

How to make a .jar out from an Android Studio project

javaandroidjarandroid-studio

提问by dum4ll3

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

我正在使用 AndroidStudio,我有这个项目,如图所示:

enter image description here

在此处输入图片说明

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).

蓝色圆圈内是 myLib。myLib 还需要使用红色圆圈内的外部库和一个 apache 包(绿色圆圈)。

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

所以我想把这整个东西变成一个单独的 .jar,这样我就可以在其他项目中使用它。

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

分步指南将不胜感激,我是开发人员世界的初学者。

Thanks!

谢谢!

回答by Scott Barta

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.

如果您在 Gradle 中将代码设置为一个普通的 Java 模块,那么让 Gradle 为您提供一个包含内容的 jar 文件真的很容易。该 jar 文件将只包含您的代码,而不包含它所依赖的其他 Apache 库。我建议以这种方式分发它;在您的库中捆绑依赖项有点奇怪,这些库的用户必须自己包含这些依赖项更为正常(因为否则这些项目的冲突已经链接了库的副本,可能是不同版本的) )。更重要的是,如果您要发布您的库,您可以避免在重新分发其他人的代码方面出现潜在的许可问题。

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模块中:

  1. Filemenu > 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: enter image description here
  5. Choose your module from the list in the dialog that comes up: enter image description here
  1. 文件菜单 >新建模块...> Java 库
  2. 在向导中设置库、Java 包名称和类名称。(如果您不希望它为您创建一个类,您可以在创建模块后将其删除)
  3. 在您的 Android 代码中,设置对新模块的依赖,以便它可以使用新库中的代码:
  4. 文件>项目结构>模块> (你的 Android 模块)>依赖> +>模块依赖。请看下面的截图: 在此处输入图片说明
  5. 从出现的对话框中的列表中选择您的模块: 在此处输入图片说明

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/libsdirectory in your module's directory. If you want to build the jar file by hand, you can run its jarbuild file task from the Gradle window: enter image description here

希望您的项目现在应该可以正常构建。构建完成后,Java 库的 jar 文件将放置在模块目录的build/libs目录中。如果您想手动构建 jar 文件,您可以从 Gradle 窗口运行其jar构建文件任务: 在此处输入图片说明

回答by Abhinav Tyagi

  • Open build.gradle for library project enter image description here

  • Write two tasks in build.gradle -- deleteJar and createJar and add rule createJar.dependsOn(deleteJar, build) enter image description here

  • 打开库项目的 build.gradle 在此处输入图片说明

  • 在 build.gradle 中写两个任务——deleteJar 和 createJar 并添加规则 createJar.dependsOn(deleteJar, build) 在此处输入图片说明

The code from above:

上面的代码:

task deleteJar(type: Delete) {
    delete 'libs/jars/logmanagementlib.jar'
}           

task createJar(type: Copy) {
    from('build/intermediates/bundles/release/')
    into('libs/jars/')
    include('classes.jar')
    rename('classes.jar', 'logmanagementlib.jar')
}

createJar.dependsOn(deleteJar, build)
  • Expand gradle panel from right and open all tasks under yourlibrary->others. You will see two new tasks there -- createJar and deleteJar enter image description here

  • Double click on createJar enter image description here

  • Once the task run successfully, get your generated jar from path mentioned in createJar task i.e. libs/xxxx.jar enter image description here

  • copy the newly generated jar into your required project's lib folder-->right click-->select "add as library"

  • 从右侧展开 gradle 面板并打开 yourlibrary->others 下的所有任务。您将在那里看到两个新任务——createJar 和 deleteJar 在此处输入图片说明

  • 双击 createJar 在此处输入图片说明

  • 任务成功运行后,从 createJar 任务中提到的路径获取生成的 jar,即 libs/xxxx.jar 在此处输入图片说明

  • 将新生成的jar复制到您需要的项目的lib文件夹中-->右键单击-->选择“添加为库”

回答by Bao Le

Simply add this to your java module's build.gradle. It will include dependent libraries in archive.

只需将其添加到您的 java 模块的 build.gradle 中即可。它将在存档中包含依赖库。

mainClassName = "com.company.application.Main"

jar {
  manifest { 
    attributes "Main-Class": "$mainClassName"
  }  

  from {
    configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
  }
}

This will result in [module_name]/build/libs/[module_name].jar file.

这将产生 [module_name]/build/libs/[module_name].jar 文件。

回答by yaniv

the way i found was to find the project compiler output (project structure > project). then find the complied folder of the module you wish to turn to a jar, compress it with zip and change the extension of the output from zip to jar.

我发现的方法是找到项目编译器输出(项目结构 > 项目)。然后找到您要转换为jar的模块的编译文件夹,用zip压缩并将输出的扩展名从zip更改为jar。

回答by Sonia John Kavery

.jar file will be automatically generate when u compile/run your application.

.jar 文件将在您编译/运行您的应用程序时自动生成。

You can find your class.jar file from root_folder/app/build/intermediates/bundles/debug

您可以从 root_folder/app/build/intermediates/bundles/debug 中找到您的 class.jar 文件

jar file location

jar 文件位置

回答by Haroun Hajem

In the Android Studio IDE, access the "Run Anything bar" by:

在 Android Studio IDE 中,通过以下方式访问“运行任何内容栏”:

CTRL+CTRL+gradle CreateFullJarRelease+ENTER

CTRL+ CTRL+ gradle CreateFullJarRelease+ENTER

After that you'll find your artefact in this folder in your project
Build > Intermediates > Full_jar > Release > CreateFullJarRelease > full.jar

之后,您将在项目的此文件夹中找到您的人工制品
Build > Intermediates > Full_jar > Release > CreateFullJarRelease > full.jar



OR

或者



Gradle has already a Taskfor that, in the gradle side-menu, under the otherfolder.

Gradle 已经Taskother文件夹下的 gradle 侧菜单中提供了一个。

gradle toolbar

渐变工具栏

Then scroll down to createFullJarReleaseand click it.

然后向下滚动createFullJarRelease并单击它。

Gradle Tasks

Gradle 任务

After that you'll find your artefact in this folder in your project

之后,您将在项目的此文件夹中找到您的人工制品

Build > Intermediates > Full_jar > Release > CreateFullJarRelease > full.jar

Build > Intermediates > Full_jar > Release > CreateFullJarRelease > full.jar

回答by Md Imran Hossain Ku

task deleteJar(type: Delete) {
    delete 'libs/mylibrary.jar'
}           

task exportjar(type: Copy) {
    from('build/intermediates/compile_library_classes/release/')
    into('libs/')
    include('classes.jar')
    rename('classes.jar', 'mylibrary.jar')
}

exportjar.dependsOn(deleteJar, build)

回答by yoAlex5

If you run a gradle task from AndroidStudio[More]

如果您从 AndroidStudio 运行 gradle 任务[更多]

assembleRelease 
//or
bundleReleaseAar

or via terminal

或通过终端

./gradlew <moduleName>:assembleRelease
//or
./gradlew <moduleName>:bundleReleaseAar

then you will able to find .aarin

那么你将能够找到.aar

<project_path>/build/outputs/aar/<module_name>.aar
//if you do not see it try to remove this folder and repeat the command

.aar(Android Library) file consists of .jar(Java Library) file and additional Android resources. .aarfile is a zip file with aar extension that is why you can replace .aarwith .zipor run

.aar(Android 库) 文件由.jar(Java 库) 文件和其他 Android 资源组成。.aar文件是AAR扩展名的zip文件,这就是为什么你可以替换.aar使用.zip或运行

unzip "<path_to/module_name>.aar"

回答by Damo Gurcinovski

  1. Go to Gradle tab in Android Studio , then select library project .

  2. Then go to Tasks

  3. Then go to Other

  4. Double click on bundleReleaseaar

  1. 转到 Android Studio 中的 Gradle 选项卡,然后选择 library project 。

  2. 然后转到任务

  3. 然后转到其他

  4. 双击 bundleReleaseaar

You can find your .aarfiles under your_module/build/outputs/aar/your-release.aar

您可以.aar在下面找到您的文件your_module/build/outputs/aar/your-release.aar

Here image

Here image