Several Android projects use a Android library project. Now I added a JAR (commons-lang.jar) to this library project. I want to export this commons-lang.jar to all projects that use the library project. I can't get it to work without duplicating the jar to all referencing projects.
一些Android项目使用Android库项目。现在我向这个库项目添加了一个JAR (common -lang. JAR)。我想导出这个common -lang。jar到所有使用库项目的项目。如果不将jar复制到所有引用项目,我就无法让它工作。
What I did so far:
到目前为止我所做的:
- Created a folder named libs within the library projects folder hierarchy.
- 在库项目文件夹层次结构中创建一个名为libs的文件夹。
- Copied the JAR to this libs folder.
- 将JAR复制到这个libs文件夹。
- Added this JAR in the Eclipse "Java Build Path" with "Add external JARs..." to the library project.
- 在Eclipse“Java构建路径”中添加了这个JAR,并向库项目“添加外部JAR…”。
- On the "Order and Export" tab selected the checkbox to the left of the JAR to export it.
- 在“Order and Export”选项卡上,选中了JAR的左边的复选框来导出它。
- On the referencing project on the "Android" page "Add" library project.
- 在“Android”页面上的引用项目“添加”库项目。
It seems that the commans-lang JAR is not exported with the library project. I need to create the libs folder on the referencing project as well, add the same JARs to the build path of the referencing project. After that everything works but the APK does contain that external JAR twice.
看起来commans-lang JAR没有随库项目导出。我还需要在引用项目上创建libs文件夹,向引用项目的构建路径添加相同的jar。在那之后,一切都开始工作了,但是APK包含了两次外部JAR。
What's wrong with my approach? What should I do in addition?
我的方法有什么问题?我还应该做什么?
Many thanks in advance.
提前感谢。
4 个解决方案
#1
1
I believe that what you do is go to Eclipse > preferences > Java > Build Path > User Libraries. Then when you add a jar file to that I believe that all of the projects in the workspace automatically have that jar in the classpath.
我相信您所做的是使用Eclipse >首选项> Java >构建路径>用户库。然后,当您向它添加jar文件时,我相信工作区中的所有项目都会自动地在类路径中包含这个jar。
#2
1
I tried the same thing using gson.jar file.
我用gson做了同样的尝试。jar文件。
- I put a copy of gson jar to the TestAndroidLib project and set the Java Build Path to include it.
- 我将gson jar的一个副本放到TestAndroidLib项目中,并设置Java构建路径来包含它。
- I added a reference to this TestAndroidLib project to the TestAndroidApp.
- 我将对这个TestAndroidLib项目的引用添加到TestAndroidApp中。
- I didn't need to duplicate the gson jar in the app project to use Gson class.
- 我不需要在app项目中复制gson jar来使用gson类。
- I could run and debug the app project.
- 我可以运行和调试应用程序项目。
What I found in the project properties of the app project is that the gson jar is added as part of Android Dependencies as shown below. You may want to check your app project's properties->Java Build Path->Libraries->Android dependencies to see if the jar you want to use is visible there.
我在app项目的项目属性中发现,gson jar作为Android依赖项的一部分添加,如下所示。您可能想要检查您的应用程序项目的属性——>Java构建路径——>库——>Android依赖项,以查看您想要使用的jar是否在那里可见。
It seems that if you have jar files in a Andoroid lib project with build path set properly, no extra build path setting is required in an app project that uses the lib project.
如果在一个Andoroid lib项目中有jar文件,并且正确地设置了构建路径,那么在使用lib项目的应用程序项目中,似乎不需要额外的构建路径设置。
#3
1
What i would do:
我想做的事:
Start from scratch (remove all project references and jar references that you created because of this problem). Create a new Android library project. Put the jar in the libs folder of this project. Let all projects for which you need the jar, reference the library project. This should work.
从头开始(删除由于这个问题而创建的所有项目引用和jar引用)。创建一个新的Android库项目。将jar放到这个项目的libs文件夹中。让您需要jar的所有项目引用库项目。这应该工作。
#4
0
I'm curious on what is actually happening when you 'Order and Export'. It sounds like you are introducing the jar twice because the external reference you are adding with 'Add external jars - this is modifying your .classpath file at the root of the project) and what ever build tool being used is automatically assuming the lib folder you have added as well.
我很好奇当你“订购和出口”的时候到底发生了什么。这听起来像你介绍jar两次因为外部参考你添加和添加外部jar——这是修改您的. classpath文件在项目的根)自动构建工具所使用过的是假设的lib文件夹添加。
The answer by Aidan is probably what you need.
艾丹的答案可能就是你所需要的。
There are a few other ways you could attach this common-lang jar as well.
还有一些其他方法可以附加这个通用jar。
If this is code you control and plan to change, it's probably better to open a separate project for this code and add the commons project as a 'Required project' on the build path. You can add the project at the Java Build Path - Projects tab. This will put the dependency on the current project you are working and allow for instant changes to the commons code.
如果您控制并计划更改这段代码,那么最好为这段代码打开一个单独的项目,并在构建路径上添加commons项目作为“必需的项目”。您可以在Java Build Path - Projects选项卡中添加项目。这将使您所工作的当前项目具有依赖性,并允许对commons代码进行即时更改。
I see the comment below about the Android libs. I wouldn't think eclipse would only allow jars for a user lib. I remember a few years ago setting up something for some dlls for a few Swing apps using Aidan's suggestion.
我看到下面关于Android libs的评论。我不认为eclipse只允许用户库使用jar。
For the little Android development I've done, i've established a maven/android template to get my projects started. If you aren't using Maven, i'm sure there are some other plugins that will take care of your Android library dependencies automatically so you don't have to worry about it.
对于我所做的Android开发,我建立了一个maven/ Android模板来启动我的项目。如果您不使用Maven,我肯定还有其他一些插件可以自动地管理您的Android库依赖,这样您就不必担心了。
#1
1
I believe that what you do is go to Eclipse > preferences > Java > Build Path > User Libraries. Then when you add a jar file to that I believe that all of the projects in the workspace automatically have that jar in the classpath.
我相信您所做的是使用Eclipse >首选项> Java >构建路径>用户库。然后,当您向它添加jar文件时,我相信工作区中的所有项目都会自动地在类路径中包含这个jar。
#2
1
I tried the same thing using gson.jar file.
我用gson做了同样的尝试。jar文件。
- I put a copy of gson jar to the TestAndroidLib project and set the Java Build Path to include it.
- 我将gson jar的一个副本放到TestAndroidLib项目中,并设置Java构建路径来包含它。
- I added a reference to this TestAndroidLib project to the TestAndroidApp.
- 我将对这个TestAndroidLib项目的引用添加到TestAndroidApp中。
- I didn't need to duplicate the gson jar in the app project to use Gson class.
- 我不需要在app项目中复制gson jar来使用gson类。
- I could run and debug the app project.
- 我可以运行和调试应用程序项目。
What I found in the project properties of the app project is that the gson jar is added as part of Android Dependencies as shown below. You may want to check your app project's properties->Java Build Path->Libraries->Android dependencies to see if the jar you want to use is visible there.
我在app项目的项目属性中发现,gson jar作为Android依赖项的一部分添加,如下所示。您可能想要检查您的应用程序项目的属性——>Java构建路径——>库——>Android依赖项,以查看您想要使用的jar是否在那里可见。
It seems that if you have jar files in a Andoroid lib project with build path set properly, no extra build path setting is required in an app project that uses the lib project.
如果在一个Andoroid lib项目中有jar文件,并且正确地设置了构建路径,那么在使用lib项目的应用程序项目中,似乎不需要额外的构建路径设置。
#3
1
What i would do:
我想做的事:
Start from scratch (remove all project references and jar references that you created because of this problem). Create a new Android library project. Put the jar in the libs folder of this project. Let all projects for which you need the jar, reference the library project. This should work.
从头开始(删除由于这个问题而创建的所有项目引用和jar引用)。创建一个新的Android库项目。将jar放到这个项目的libs文件夹中。让您需要jar的所有项目引用库项目。这应该工作。
#4
0
I'm curious on what is actually happening when you 'Order and Export'. It sounds like you are introducing the jar twice because the external reference you are adding with 'Add external jars - this is modifying your .classpath file at the root of the project) and what ever build tool being used is automatically assuming the lib folder you have added as well.
我很好奇当你“订购和出口”的时候到底发生了什么。这听起来像你介绍jar两次因为外部参考你添加和添加外部jar——这是修改您的. classpath文件在项目的根)自动构建工具所使用过的是假设的lib文件夹添加。
The answer by Aidan is probably what you need.
艾丹的答案可能就是你所需要的。
There are a few other ways you could attach this common-lang jar as well.
还有一些其他方法可以附加这个通用jar。
If this is code you control and plan to change, it's probably better to open a separate project for this code and add the commons project as a 'Required project' on the build path. You can add the project at the Java Build Path - Projects tab. This will put the dependency on the current project you are working and allow for instant changes to the commons code.
如果您控制并计划更改这段代码,那么最好为这段代码打开一个单独的项目,并在构建路径上添加commons项目作为“必需的项目”。您可以在Java Build Path - Projects选项卡中添加项目。这将使您所工作的当前项目具有依赖性,并允许对commons代码进行即时更改。
I see the comment below about the Android libs. I wouldn't think eclipse would only allow jars for a user lib. I remember a few years ago setting up something for some dlls for a few Swing apps using Aidan's suggestion.
我看到下面关于Android libs的评论。我不认为eclipse只允许用户库使用jar。
For the little Android development I've done, i've established a maven/android template to get my projects started. If you aren't using Maven, i'm sure there are some other plugins that will take care of your Android library dependencies automatically so you don't have to worry about it.
对于我所做的Android开发,我建立了一个maven/ Android模板来启动我的项目。如果您不使用Maven,我肯定还有其他一些插件可以自动地管理您的Android库依赖,这样您就不必担心了。