Android Jar 库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1983617/
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
Android Jar libraries
提问by Jeremy Edwards
How do you setup a project that can result in a jar library file that can be used for android? I want to create a custom library across all projects.
您如何设置可以生成可用于 android 的 jar 库文件的项目?我想在所有项目中创建一个自定义库。
Few other questions:
其他几个问题:
- Does it need to be compiled against a specific version of android sdk?
- When an android package is compiled against a jar library does the classes necessary to work with the code get compiled with main code into the apk or does the entire jar get included?
- Any notable optimizations or pitfalls I need to know about with using a jar instead of integrating the code directly?
- Does the jar have to be signed like the apk needs to?
- 是否需要针对特定版本的 android sdk 进行编译?
- 当针对 jar 库编译 android 包时,是否将使用代码所需的类与主代码一起编译到 apk 中,或者是否包含整个 jar?
- 我需要了解使用 jar 而不是直接集成代码的任何显着优化或陷阱?
- jar 是否必须像 apk 需要的那样签名?
采纳答案by ddcruver
Google just release a new version of the SDK that handles Shared Libraries!
Google 刚刚发布了处理共享库的新版 SDK!
https://developer.android.com/tools/projects/projects-eclipse.html#SettingUpLibraryProject
https://developer.android.com/tools/projects/projects-eclipse.html#SettingUpLibraryProject
ADT 0.9.7 (May 2010)
ADT 0.9.7(2010 年 5 月)
Library projects:
The ADT Plugin now supports the use of library projects during development, a capability that lets you store shared Android application code and resources in a separate development project. You can then reference the library project from other Android projects and, at build time, the tools compile the shared code and resources as part of the dependent applications. More information about this feature is available in the Developing in Eclipse with ADT document. If you are not developing in Eclipse, SDK Tools r6 provides the equivalent library project support through the Ant build system.
库项目:
ADT 插件现在支持在开发过程中使用库项目,该功能可让您将共享的 Android 应用程序代码和资源存储在单独的开发项目中。然后,您可以从其他 Android 项目中引用库项目,并且在构建时,这些工具会将共享代码和资源编译为相关应用程序的一部分。使用 ADT 在 Eclipse 中开发文档中提供了有关此功能的更多信息。如果您不是在 Eclipse 中进行开发,SDK Tools r6 将通过 Ant 构建系统提供等效的库项目支持。
回答by Mark B
There is nothing special you need to do to your project setup to create a jar file that will work in an Android app. Just create a normal Java project in Eclipse.
无需对项目设置进行任何特殊处理即可创建可在 Android 应用程序中运行的 jar 文件。只需在 Eclipse 中创建一个普通的 Java 项目。
It doesn't need to be compiled against Android at all. You can include any jar file as long as it doesn't reference classes that aren't included in Android. Just include your jar file in your build path of your Android projects in Eclipse, and it will automatically be included in your APK file.
Not sure but I assume all classes in the jar file get included in the APK.
I don't think just including some classes in a jar vs. in the project directly will make any difference in the resulting APK.
它根本不需要针对 Android 进行编译。您可以包含任何 jar 文件,只要它不引用 Android 中未包含的类。只需在 Eclipse 中 Android 项目的构建路径中包含您的 jar 文件,它就会自动包含在您的 APK 文件中。
不确定,但我假设 jar 文件中的所有类都包含在 APK 中。
我不认为仅仅在 jar 和项目中直接包含一些类会对生成的 APK 产生任何影响。
回答by Andras Balázs Lajtha
I asked myselft the same question: can I export and reuse a layout in Android. I tried the following: export a project with XML resources to a JAR file, and then try to access a layout through it's R.layout. Of course it didn't work, as R.layout.main was an integer, the same integer as was associated to the single layout (relative.xml) defined in the host project's R.layout.
我问自己同样的问题:我可以在 Android 中导出和重用布局吗?我尝试了以下操作:将带有 XML 资源的项目导出到 JAR 文件,然后尝试通过它的 R.layout 访问布局。当然它不起作用,因为 R.layout.main 是一个整数,与宿主项目的 R.layout 中定义的单个布局 (relative.xml) 关联的整数相同。
The Android SDK kompiles layout xml files into View resources, and as XML files in the included JAR file are not layout xml files, they are not compiled and no ID is associated to them in R.layout.
Android SDK 将layout xml 文件编译到View 资源中,由于包含的JAR 文件中的xml 文件不是layout xml 文件,所以它们没有被编译,并且在R.layout 中没有关联ID。
If you want to export a layout from your project, then you should export it as XML, and import it to the new project as a layout resource, not as part of an external library.
如果要从项目中导出布局,则应将其导出为 XML,并将其作为布局资源而不是外部库的一部分导入到新项目中。
If you want to reuse layouts from a JAR, you have to create a sublcalss of a View, and build the layout from code. Than you can instantiate it and set the created instance as the View of the Activity, or add the instance to the View of your Activity.
如果要重用 JAR 中的布局,则必须创建视图的子类,并从代码构建布局。然后您可以实例化它并将创建的实例设置为活动的视图,或者将实例添加到您的活动的视图中。
回答by cags
This work for me!!!. if you wanna use a .jar file into your application from a library project that uses resources like attrs.xml
(and suppose other ones like layouts, strings, etc) that generate a R.java. you should in eclipse go to export->Java/Jar file->
and check Export all output folders for checked projects -> Finish
... this will ensure that at run time your app that uses this jar does not generate an exception for missing clases and resources.
这对我有用!!!。如果您想将 .jar 文件从库项目中使用到您的应用程序中,该库项目使用attrs.xml
生成 R.java 的资源(并假设其他资源如布局、字符串等)。您应该在 Eclipse 中转到export->Java/Jar file->
并检查导出所有输出文件夹以进行检查projects -> Finish
...这将确保在运行时您使用此 jar 的应用程序不会因缺少类和资源而生成异常。
Of course, to include the jar into you new project go to. project properties-> Java build path -> Libraries -> Add external jar ->
and you are done.
当然,要将 jar 包含到您的新项目中,请转到。project properties-> Java build path -> Libraries -> Add external jar ->
你就完成了。
Hope this help!
希望这有帮助!
回答by Stefaan V
to create a jar file, you should use the Export option of Eclipse. In the export dialog, choose java, and the suboption JAR. THen a wizard is displayed where you need to make some selections. You need to specify the location where the jar file will be stored.
要创建 jar 文件,您应该使用 Eclipse 的导出选项。在导出对话框中,选择 java 和子选项 JAR。然后会显示一个向导,您需要在其中进行一些选择。您需要指定 jar 文件的存储位置。
An error I made the first time I created a jar file, was to include the Android Manifest. When adding the created jar file, I got a conflict of double android manifest.
我第一次创建 jar 文件时犯的一个错误是包含 Android Manifest。添加创建的jar文件时,我遇到了双重android清单的冲突。
回答by ymutlu
You can use a custom method to reach R file dynamicly, example link is below.
您可以使用自定义方法动态访问 R 文件,示例链接如下。
Packaging Android resource files within a distributable Jar file
public static int getResourseIdByName(String packageName, String className, String name) {
Class r = null;
int id = 0;
try {
r = Class.forName(packageName + ".R");
Class[] classes = r.getClasses();
Class desireClass = null;
for (int i = 0; i < classes.length; i++) {
if(classes[i].getName().split("\$")[1].equals(className)) {
desireClass = classes[i];
break;
}
}
if(desireClass != null)
id = desireClass.getField(name).getInt(desireClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
return id;
}
回答by ddcruver
If you look in the android.jar file under platforms/android-X.X/android.jar file it does include layout XML items so you should be able to create one as well. I just am not sure on exactly how you do this and how you reference them in the other project.
如果您查看platforms/android-XX/android.jar 文件下的android.jar 文件,它确实包含布局XML 项,因此您也应该能够创建一个。我只是不确定你是如何做到这一点的,以及你如何在其他项目中引用它们。