Java 如何将 AAR 转换为 JAR

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

How to convert AAR to JAR

javajaraar

提问by Nestor

Situation:

情况

I intend to use a Java library and I only have an AARfile from a Maven repository but I need the JARfile.

我打算使用 Java 库,并且我只有来自 Maven 存储库的AAR文件,但我需要JAR文件。

Background story:

背景故事:

I tried to compile a library, but its Gradle structure was unstable. So I asked for a compiled jar file, and its developer handed me an aar file from some Maven repository (the developer couldn't compile his own project). The Gradle configuration was a mess, it depended on multiple libraries and Gradle has thrown some exceptions.

我试图编译一个库,但它的 Gradle 结构不稳定。所以我要了一个编译好的jar文件,它的开发者给了我一个来自某个Maven存储库的aar文件(开发者无法编译他自己的项目)。Gradle 配置一团糟,它依赖于多个库并且 Gradle 抛出了一些异常。

I tried to use it in my IDEA IDE, but it couldn't see it. A library project should be able to be compiled into a jar file, right?

我试图在我的 IDEA IDE 中使用它,但它看不到它。一个库项目应该可以编译成jar文件吧?

Question:

问题

What should I do to convert that AAR file to a JAR file?

我应该怎么做才能将该 AAR 文件转换为 JAR 文件?

采纳答案by Nestor

The AARfile consists of a JARfile and some resource files (it is basically a standard zip file with a custom file extension). Here are the steps to convert:

AAR文件由的JAR文件和一些资源文件(它基本上是一个自定义文件扩展名的标准ZIP文件)。以下是转换步骤:

  1. Extract the AAR file using standard zip extract (rename it to *.zipto make it easier)
  2. Find the classes.jarfile in the extracted files
  3. Rename it as you like and use that jar file in your project
  1. 使用标准 zip 解压提取 AAR 文件(将其重命名为*.zip以使其更容易)
  2. 在解压出来的文件中找到classes.jar文件
  3. 根据需要重命名并在项目中使用该 jar 文件

回答by AWT

.aar is a standard zip archive, the same one used in .jar. Just change the extension and, assuming it's not corrupt or anything, it should be fine.

.aar 是标准的 zip 存档,与 .jar 中使用的相同。只需更改扩展名,假设它没有损坏或任何东西,它应该没问题。

If you needed to, you could extract it to your filesystem and then repackage it as a jar.

如果需要,您可以将其解压缩到您的文件系统,然后将其重新打包为 jar。

1) Rename it to .jar
2) Extract: jar xf filename.jar
3) Repackage: jar cf output.jar input-file(s)

回答by ddmytrenko

For those, who want to do it automatically, I have wrote a little two-lines bash script which does next two things:

对于那些想要自动执行此操作的人,我编写了一个两行的 bash 脚本,它执行接下来的两件事:

  1. Looks for all *.aar files and extracts classes.jar from them
  2. Renames extracted classes.jar to be like the aar but with a new extension

    find . -name '*.aar' -exec sh -c 'unzip -d `dirname {}` {} classes.jar' \;
    find . -name '*.aar' -exec sh -c 'mv `dirname {}`/classes.jar `echo {} | sed s/aar/jar/g`' \;
    
  1. 查找所有 *.aar 文件并从中提取 classes.jar
  2. 将提取的 classes.jar 重命名为 aar,但具有新的扩展名

    find . -name '*.aar' -exec sh -c 'unzip -d `dirname {}` {} classes.jar' \;
    find . -name '*.aar' -exec sh -c 'mv `dirname {}`/classes.jar `echo {} | sed s/aar/jar/g`' \;
    

That's it!

就是这样!

回答by anand krish

 The 'aar' bundle is the binary distribution of an Android Library Project. .aar file 
 consists a JAR file and some resource files. You can convert it
 as .jar file using this steps

1) Copy the .aar file in a separate folder and Rename the .aar file to .zip file using 
 any winrar or zip Extractor software.

2) Now you will get a .zip file. Right click on the .zip file and select "Extract files". 
 Will get a folder which contains "classes.jar, resource, manifest, R.java,
 proguard(optional), libs(optional), assets(optional)".

3) Rename the classes.jar file as yourjarfilename.jar and use this in your project.

Note: If you want to get only .jar file from your .aar file use the above way. Suppose If you want to include the manifest.xml and resources with your .jar file means you can just right click on your .aar file and save it as .jar file directly instead of saving it as a .zip. To view the .jar file which you have extracted, download JD-GUI(Java Decompiler). Then drag and drop your .jar file into this JD_GUI, you can see the .class file in readable formats like a .java file.

注意:如果您只想从 .aar 文件中获取 .jar 文件,请使用上述方式。假设如果您想在 .jar 文件中包含 manifest.xml 和资源,则意味着您只需右键单击 .aar 文件并将其直接另存为 .jar 文件,而不是将其另存为 .zip。要查看解压后的 .jar 文件,请下载JD-GUI(Java Decompiler)。然后将您的 .jar 文件拖放到这个 JD_GUI 中,您可以看到 .class 文件的可读格式,如 .java 文件。

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

回答by Sébastien

Android Studio (version: 1.3.2) allows you to seamlessly access the .jar inside a .aar.

Android Studio(版本:1.3.2)允许您无缝访问 .aar 中的 .jar。

Bonus: it automatically decompiles the classes!

奖励:它会自动反编译类!

Simply follow these steps:

只需按照以下步骤操作:

  1. File > New > New Module > Import .JAR/.AAR Packageto import you .aar as a module

  2. Add the newly created module as a dependency to your main project (not sure if needed)

  3. Right click on "classes.jar" as shown in the capture below, and click "Show in explorer". Here is your .jar.

  1. File > New > New Module > Import .JAR/.AAR Package将您 .aar 作为模块导入

  2. 将新创建的模块作为依赖项添加到您的主项目中(不确定是否需要)

  3. 右键单击“classes.jar”,如下面的截图所示,然后单击“在资源管理器中显示”。这是您的 .jar。

access .jar from .aar

从 .aar 访问 .jar

回答by OneWorld

Resource based .aar-projects

基于资源的 .aar-projects

Finding the classes.jar file inside the .aar file is pretty trivial. However, that approach does not work, if the .aar-project defined some resources (example: R.layout.xyz)

在 .aar 文件中查找 classes.jar 文件非常简单。但是,如果 .aar-project 定义了一些资源(例如:R.layout.xyz),则该方法不起作用

  • Therefore deaarfrom CommonsGuy helped me to get a valid ADT-friendly project out of an .aar-file. In my case I converted subsampling-scale-image-view. It took me about an hour to set up ruby on my PC.

  • Another approach is using android-maven-plugin for Eclipse/ADT as CommonsGuy writes in his blog.

  • Yet another approach could be, just cloning the whole desired project as source from git and import it as "Existing Android project"

  • 因此deaar从CommonsGuy帮助我得到一个有效的ADT友好型项目出.aar档。就我而言,我转换了 subsampling-scale-image-view。我花了大约一个小时在我的 PC 上设置 ruby​​。

  • 另一种方法是将 android-maven-plugin 用于 Eclipse/ADT,正如 CommonsGuy 在他的博客中所写

  • 另一种方法可能是,只需从 git 克隆整个所需项目作为源并将其导入为“现有的 Android 项目”

回答by rsp1984

As many other people have pointed out, just extracting the .jar from the .aar file doesn't quite cut it as resources may be missing.

正如许多其他人指出的那样,仅仅从 .aar 文件中提取 .jar 并不能完全削减它,因为资源可能会丢失。

Here are the steps that worked for me (context of Android, your mileage may vary if you have other purposes):

以下是对我有用的步骤(Android 环境,如果您有其他目的,您的里程可能会有所不同):

  1. Rename the .aar file to .zip and extract.
  2. The extracted folder is an ADT project that you can import in Eclipse with some minor modifications (see below)!
  3. In the extracted folder rename the contained file classes.jarto whatever you like (in this example myProjectLib.jar) and move it to the lib folder within the extracted folder.
  4. Now in order for Eclipse to accept it you need to put two files into the extracted folder root:
    • .project
    • .classpath
  5. To do that, create a new Android dummy project in Eclipse and copy over the files, or copy over from an existing Android project.
  6. Open the .project file and look for the XML name tag and replace the contents of it with myProjectLib (or whatever you called your jar file above) and save.
  7. Now in Eclipse you can File -> New -> Project -> Android Project from existing source..and point to the extracted folder content.
  8. After import right click on the newly created project, select Properties -> Android, and check Is Library.
  9. In your main project that you want to use the library for, also go to Properties -> Androidand add the newly added myProjectLib to the list of dependencies.
  1. 将 .aar 文件重命名为 .zip 并解压缩。
  2. 提取的文件夹是一个 ADT 项目,您可以在 Eclipse 中进行一些小的修改(见下文)!
  3. 在提取的文件夹中,将包含的文件classes.jar重命名为您喜欢的任何名称(在此示例中为 myProjectLib.jar),并将其移动到提取文件夹中的 lib 文件夹中。
  4. 现在为了让 Eclipse 接受它,您需要将两个文件放入解压缩的文件夹根目录中:
    • 。项目
    • .classpath
  5. 为此,请在 Eclipse 中创建一个新的 Android 虚拟项目并复制文件,或从现有的 Android 项目复制。
  6. 打开 .project 文件并查找 XML 名称标记并将其内容替换为 myProjectLib(或您在上面调用的任何 jar 文件)并保存。
  7. 现在在 Eclipse 中,您可以File -> New -> Project -> Android Project from existing source..并指向提取的文件夹内容。
  8. 导入后右击新创建的项目,选择Properties -> Android,并勾选Is Library
  9. 在您想要使用该库的主项目中,还要转到Properties -> Android并将新添加的 myProjectLib 添加到依赖项列表中。

回答by Stepango

If you are using Gradle for your builds - there is a Gradle plugin which allows you to add aar dependency to your java|kotlin|scala|... modules.

如果您使用 Gradle 进行构建 - 有一个 Gradle 插件可以让您向 java|kotlin|scala|... 模块添加 aar 依赖项。

https://github.com/stepango/aar2jar

https://github.com/stepango/aar2jar

plugins {
    id 'java'
    id 'com.stepango.aar2jar' version “0.6” // <- this one
}

dependencies {
    compileOnlyAar "com.android.support:support-annotations:28.0.0" // <- Use any AAR dependencies
}