如何为 Android 库项目创建 jar [英] How to create jar for Android Library Project
问题描述
我必须创建一个库,我将在 jar 文件中将其导出到客户端.有没有办法用里面的资源创建一个jar?
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?
Google adMob 有这样一个 jar,里面有 R$layout.class 等资源文件.此页面 描述了一些方法来做到这一点,但我无法确切地理解我应该怎么做使用上述方法将库项目导入到应用程序中.
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.
推荐答案
这是你能得到的最接近的:
This is the closest that you can get:
第 1 步:创建一个常规的 Android 库项目,并使其正常工作.
Step #1: Create a regular Android library project, and get it working.
第 2 步:将该 Android 库项目复制到另一个目录中.
Step #2: Copy that Android library project into another directory.
第 3 步:从原始 Android 库项目中编译的 Java 类创建一个 JAR,并将该 JAR 放在您在第 2 步中创建的副本的 libs/
目录中.你应该能够在这个 JAR 上手动运行 ProGuard,虽然我还没有尝试过.
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.
第 4 步:删除复制的库项目的 src/
目录中的所有内容,留下空的 src/
目录.
Step #4: Remove everything inside the src/
directory of the copied library project, leaving behind and empty src/
directory.
第 5 步:压缩或以其他方式分发复制的 Android 库项目.
Step #5: ZIP up or otherwise distribute the copied Android library project.
这将为您提供一个类似于 Play Services 的 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.
更新:现在更好的方法是将您的库项目打包为 AAR,这是 Android 的原生方式,可根据您的编译代码和所需资源创建可重用工件.目前(2014 年 7 月),Android 版 Gradle、Android Studio 和 android-maven-plugin
可以创建和使用 AAR.
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.
这篇关于如何为 Android 库项目创建 jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!