如何为Android库项目创建jar

时间:2022-09-01 16:57:57

I have to create a library that I am going to export to the client in a jar file. Is there any way to create a jar with the resources in it?

我必须创建一个库,以便在jar文件中导出到客户机。是否有办法用其中的资源创建一个jar ?

The Google adMob have such a jar, which includes resource file such as R$layout.class in it. This page describes some way to do that but I am not able to understand exactly how am I supposed to import the library project to an application using the method above.

谷歌adMob有这样一个jar,其中包含资源文件,如R$layout。类。这个页面描述了一些这样做的方法,但是我不能完全理解如何使用上面的方法将库项目导入到应用程序中。

8 个解决方案

#1


79  

This is the closest that you can get:

这是你能得到的最接近的结果:

Step #1: Create a regular Android library project, and get it working.

第一步:创建一个常规的Android库项目,并让它运行。

Step #2: Copy that Android library project into another directory.

步骤#2:将Android库项目复制到另一个目录中。

Step #3: Create a JAR from the compiled Java classes from the original Android library project, and put that JAR in the libs/ directory of the copy you made in Step #2. You should be able to run ProGuard on this JAR manually, though I haven't tried that.

步骤#3:从最初的Android库项目中编译的Java类创建一个JAR,并将该JAR放到您在步骤#2中创建的副本的libs/目录中。您应该能够在这个JAR上手动运行ProGuard,尽管我还没有尝试过。

Step #4: Remove everything inside the src/ directory of the copied library project, leaving behind and empty src/ directory.

步骤4:删除复制库项目的src/目录内的所有内容,留下空src/目录。

Step #5: ZIP up or otherwise distribute the copied Android library project.

步骤5:压缩或分发复制的Android库项目。

This will give you an Android library project like the Play Services one, where the resources are available, but the source code is not.

这将为您提供一个Android库项目,比如Play Services项目,其中有可用的资源,但是源代码没有。

UPDATE: An even better approach nowadays is to package your library project as an AAR, which is Android's native way of creating a reusable artifact from your compiled code plus required resources. At the present time (July 2014), Gradle for Android, Android Studio, and the android-maven-plugin can create and consume AARs.

更新:现在更好的方法是将库项目打包为AAR,这是Android从编译代码和所需资源中创建可重用构件的本地方法。目前(2014年7月),Android、Android Studio和Android - mavend -plugin都可以创建和使用AARs。

#2


21  

This works!

这个工作!

  1. Make your library project a normal project by deselecting IsLibrary flag.
  2. 通过取消IsLibrary标志,使您的库项目成为一个正常的项目。
  3. Execute your project as Android Application. (It will not show any error)
  4. 以Android应用程序执行项目。(不会显示任何错误)
  5. You'll find a .jar file in bin folder along with .apk.
  6. 您将在bin文件夹中找到.jar文件和.apk。
  7. Give you .jar who want to use your library.
  8. 给你。jar谁想使用你的库。
  9. Tell them to just import external jar into build path.
  10. 告诉他们将外部jar导入到构建路径中。

This will work fine with all the resources. Thanking you all. for your responce

这对所有的资源都很有效。感谢你们所有人。对你的反应

#3


13  

Follow the below steps to obtain a distributable jar of your library (verified for library project without external dependencies):

按照下面的步骤获得您的库的可分发jar(在没有外部依赖关系的情况下对库项目进行验证):

  1. Create your library project. Clean it. You will get a libraryProjectName.jar file inside the bin folder. (Make sure "is Library" is selected in the library project.
  2. 创建你的图书馆项目。清洁它。你会得到一个libraryProjectName。在bin文件夹中的jar文件。(确保“is Library”在Library project中被选中。
  3. Copy the .jar file into the libs folder of your target application
  4. 将.jar文件复制到目标应用程序的libs文件夹中
  5. Refresh your workspace and press Ctr+Shift+O (on Windwows/Linux) or Cmd+Shift+O (on Mac) to resolve any import dependencies from the library added.
  6. 刷新您的工作空间并按Ctr+Shift+O(在Windwows/Linux上)或Cmd+Shift+O(在Mac上)来解析库中添加的任何导入依赖项。

Comment in case facing any issue.

如有任何问题,请发表意见。

#4


3  

Tested with adt 22.2.1

测试了adt 22.2.1

  1. Create your library project
  2. 创建你的图书馆项目
  3. Create empty project
  4. 创建空的项目
  5. Add library project to your empty project
  6. 向空项目中添加库项目
  7. Build project
  8. 建设项目
  9. In /gen folder you will find class for your library project, this will contain your resources for lib project.
  10. 在/gen文件夹中,您将为您的库项目找到类,它将包含您的lib项目资源。
  11. Export JAR (select only this class ... from /gen folder)
  12. 导出JAR(只选择这个类…从/创文件夹)
  13. Export JAR from your lib project
  14. 从lib项目导出JAR
  15. Rename exported files to zip and merge content
  16. 将导出的文件重命名为zip和合并内容
  17. Rename the final file as JAR and add it to project.
  18. 将最终文件重命名为JAR并将其添加到项目中。

#5


2  

You cannot package resource files into jar. You can export your project as a library project.

不能将资源文件打包到jar中。您可以将项目导出为库项目。

You export your utility classes that do not refer to resources in android ie pure java classes as jar file.

将不引用android ie纯java类中的资源的实用程序类导出为jar文件。

Check the link below

检查下面的链接

http://developer.android.com/tools/projects/index.html

http://developer.android.com/tools/projects/index.html

To make your project a library project

让你的项目成为一个图书馆项目。

Right click on your project. Go to properties. Choose Android. Make sure Is Library is checked.

右键单击您的项目。去的属性。选择Android。确定是否检查了库。

To refer the library project in your android project

在您的android项目中引用库项目

Right click on your project. Go to properties. Choose Android. Click Add. Browse and the library project.

右键单击您的项目。去的属性。选择Android。单击Add. Browse和library项目。

Quoting from the docs

引用的文档

A library project differs from a standard Android application project in that you cannot compile it directly to its own .apk and run it on an Android device. 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.

库项目与标准的Android应用程序项目的不同之处在于,您不能直接将其编译为自己的.apk并在Android设备上运行。类似地,不能将库项目导出到自包含的JAR文件中,就像对真正的库所做的那样。相反,您必须通过引用依赖应用程序中的库并构建该应用程序,间接地编译该库。

#6


1  

Here is an easy way to do it with ant, found this in the Volley sourcecode. Add the code below to custom_rules.xml then build with ant jar

这里有一种简单的方法来处理ant,在截击源代码中发现这一点。将下面的代码添加到custom_rules中。然后使用ant jar构建xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="volley-rules" default="help">

  <!-- A rule to generate the JAR for inclusion in an Android
       application. Output file will be bin/volley.jar -->
  <target name="jar" depends="-compile">
    <jar destfile="bin/volley.jar"
         basedir="bin/classes" />
  </target>
</project>

#7


1  

As per latest Android Studio v1.2, its as easy as having a cake. Already answered complete solution with screenshots on this link

根据最新的Android Studio v1.2,这就像吃蛋糕一样简单。已经用这个链接上的屏幕截图回答了完整的解决方案

#8


0  

With Eclipse ADT v22.6.2 you can just export jar by clicking File->Export->Java->Jar File, select directories and destination where to save.

使用Eclipse ADT v22.6.2,只需单击File-> export ->Java-> jar文件,选择要保存的目录和目的地,即可导出jar。

#1


79  

This is the closest that you can get:

这是你能得到的最接近的结果:

Step #1: Create a regular Android library project, and get it working.

第一步:创建一个常规的Android库项目,并让它运行。

Step #2: Copy that Android library project into another directory.

步骤#2:将Android库项目复制到另一个目录中。

Step #3: Create a JAR from the compiled Java classes from the original Android library project, and put that JAR in the libs/ directory of the copy you made in Step #2. You should be able to run ProGuard on this JAR manually, though I haven't tried that.

步骤#3:从最初的Android库项目中编译的Java类创建一个JAR,并将该JAR放到您在步骤#2中创建的副本的libs/目录中。您应该能够在这个JAR上手动运行ProGuard,尽管我还没有尝试过。

Step #4: Remove everything inside the src/ directory of the copied library project, leaving behind and empty src/ directory.

步骤4:删除复制库项目的src/目录内的所有内容,留下空src/目录。

Step #5: ZIP up or otherwise distribute the copied Android library project.

步骤5:压缩或分发复制的Android库项目。

This will give you an Android library project like the Play Services one, where the resources are available, but the source code is not.

这将为您提供一个Android库项目,比如Play Services项目,其中有可用的资源,但是源代码没有。

UPDATE: An even better approach nowadays is to package your library project as an AAR, which is Android's native way of creating a reusable artifact from your compiled code plus required resources. At the present time (July 2014), Gradle for Android, Android Studio, and the android-maven-plugin can create and consume AARs.

更新:现在更好的方法是将库项目打包为AAR,这是Android从编译代码和所需资源中创建可重用构件的本地方法。目前(2014年7月),Android、Android Studio和Android - mavend -plugin都可以创建和使用AARs。

#2


21  

This works!

这个工作!

  1. Make your library project a normal project by deselecting IsLibrary flag.
  2. 通过取消IsLibrary标志,使您的库项目成为一个正常的项目。
  3. Execute your project as Android Application. (It will not show any error)
  4. 以Android应用程序执行项目。(不会显示任何错误)
  5. You'll find a .jar file in bin folder along with .apk.
  6. 您将在bin文件夹中找到.jar文件和.apk。
  7. Give you .jar who want to use your library.
  8. 给你。jar谁想使用你的库。
  9. Tell them to just import external jar into build path.
  10. 告诉他们将外部jar导入到构建路径中。

This will work fine with all the resources. Thanking you all. for your responce

这对所有的资源都很有效。感谢你们所有人。对你的反应

#3


13  

Follow the below steps to obtain a distributable jar of your library (verified for library project without external dependencies):

按照下面的步骤获得您的库的可分发jar(在没有外部依赖关系的情况下对库项目进行验证):

  1. Create your library project. Clean it. You will get a libraryProjectName.jar file inside the bin folder. (Make sure "is Library" is selected in the library project.
  2. 创建你的图书馆项目。清洁它。你会得到一个libraryProjectName。在bin文件夹中的jar文件。(确保“is Library”在Library project中被选中。
  3. Copy the .jar file into the libs folder of your target application
  4. 将.jar文件复制到目标应用程序的libs文件夹中
  5. Refresh your workspace and press Ctr+Shift+O (on Windwows/Linux) or Cmd+Shift+O (on Mac) to resolve any import dependencies from the library added.
  6. 刷新您的工作空间并按Ctr+Shift+O(在Windwows/Linux上)或Cmd+Shift+O(在Mac上)来解析库中添加的任何导入依赖项。

Comment in case facing any issue.

如有任何问题,请发表意见。

#4


3  

Tested with adt 22.2.1

测试了adt 22.2.1

  1. Create your library project
  2. 创建你的图书馆项目
  3. Create empty project
  4. 创建空的项目
  5. Add library project to your empty project
  6. 向空项目中添加库项目
  7. Build project
  8. 建设项目
  9. In /gen folder you will find class for your library project, this will contain your resources for lib project.
  10. 在/gen文件夹中,您将为您的库项目找到类,它将包含您的lib项目资源。
  11. Export JAR (select only this class ... from /gen folder)
  12. 导出JAR(只选择这个类…从/创文件夹)
  13. Export JAR from your lib project
  14. 从lib项目导出JAR
  15. Rename exported files to zip and merge content
  16. 将导出的文件重命名为zip和合并内容
  17. Rename the final file as JAR and add it to project.
  18. 将最终文件重命名为JAR并将其添加到项目中。

#5


2  

You cannot package resource files into jar. You can export your project as a library project.

不能将资源文件打包到jar中。您可以将项目导出为库项目。

You export your utility classes that do not refer to resources in android ie pure java classes as jar file.

将不引用android ie纯java类中的资源的实用程序类导出为jar文件。

Check the link below

检查下面的链接

http://developer.android.com/tools/projects/index.html

http://developer.android.com/tools/projects/index.html

To make your project a library project

让你的项目成为一个图书馆项目。

Right click on your project. Go to properties. Choose Android. Make sure Is Library is checked.

右键单击您的项目。去的属性。选择Android。确定是否检查了库。

To refer the library project in your android project

在您的android项目中引用库项目

Right click on your project. Go to properties. Choose Android. Click Add. Browse and the library project.

右键单击您的项目。去的属性。选择Android。单击Add. Browse和library项目。

Quoting from the docs

引用的文档

A library project differs from a standard Android application project in that you cannot compile it directly to its own .apk and run it on an Android device. 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.

库项目与标准的Android应用程序项目的不同之处在于,您不能直接将其编译为自己的.apk并在Android设备上运行。类似地,不能将库项目导出到自包含的JAR文件中,就像对真正的库所做的那样。相反,您必须通过引用依赖应用程序中的库并构建该应用程序,间接地编译该库。

#6


1  

Here is an easy way to do it with ant, found this in the Volley sourcecode. Add the code below to custom_rules.xml then build with ant jar

这里有一种简单的方法来处理ant,在截击源代码中发现这一点。将下面的代码添加到custom_rules中。然后使用ant jar构建xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="volley-rules" default="help">

  <!-- A rule to generate the JAR for inclusion in an Android
       application. Output file will be bin/volley.jar -->
  <target name="jar" depends="-compile">
    <jar destfile="bin/volley.jar"
         basedir="bin/classes" />
  </target>
</project>

#7


1  

As per latest Android Studio v1.2, its as easy as having a cake. Already answered complete solution with screenshots on this link

根据最新的Android Studio v1.2,这就像吃蛋糕一样简单。已经用这个链接上的屏幕截图回答了完整的解决方案

#8


0  

With Eclipse ADT v22.6.2 you can just export jar by clicking File->Export->Java->Jar File, select directories and destination where to save.

使用Eclipse ADT v22.6.2,只需单击File-> export ->Java-> jar文件,选择要保存的目录和目的地,即可导出jar。