简体   繁体   English

如何在公共目录(不是libs)中引用外部jar文件以使用ant构建android项目?

[英]How do I reference external jar files in a common directory (not libs) to build android project using ant?

I would like to build several android projects that reference the same jar files using ant. 我想建立几个使用ant引用相同jar文件的android项目。 I do not want to copy the jar file to each libs directory in the project (due to how the source control tree is set up). 我不想将jar文件复制到项目中的每个libs目录(由于如何设置源代码管理树)。 The answers I find here say "put them in the libs directory" which does not solve this problem. 这里找到答案说“将它们放入libs目录”,这不能解决此问题。

So the question is, how do I configure my android ant build scripts to reference a common jar in a separate directory outside the project (not in "libs")? 所以问题是,我该如何配置我的android ant生成脚本以引用项目外部单独目录(而不是“ libs”中)的通用jar?

In the sdk, there are ant files under tools/ant . 在sdk中, tools/ant下有ant文件。 In main_rules.xml , you can find the following code section: main_rules.xml ,您可以找到以下代码部分:

<!-- Directory for the third party java libraries -->
<property name="jar.libs.dir" value="libs" />
<property name="jar.libs.absolute.dir" location="${jar.libs.dir}" />
<!-- create a path with all the jar files, from the main project and the
     libraries -->
<path id="jar.libs.ref">
    <fileset dir="${jar.libs.absolute.dir}" includes="*.jar" />
    <path refid="project.libraries.jars" />
</path>

This is how the ant build code determines where to get the jars from. 这就是蚂蚁构建代码确定从何处获取jar的方式。 You could overload the value of jar.libs.dir in your build.properties since that is read before this section and would overload the value. 您可以在build.properties中重载jar.libs.dir的值,因为这是在本节之前读取的,并且会重载该值。 Pointing that at a common directory you intend to use will do what you are suggesting. 将您打算使用的公用目录指向您将要执行的建议。 You may need to play with it some to get it to work the way you want. 您可能需要对其进行一些操作才能使其按所需的方式工作。

One caveat is that I'm not sure if this will negatively impact other build life cycle activities. 一个警告是,我不确定这是否会对其他构建生命周期活动产生负面影响。

It looks like the newer sdk tools make this easier to accomplish. 看起来,较新的SDK工具使此操作更容易实现。 I have a master libs folder that all my android projects share and I just added this to the ant.properties file to make it look up one level for those libs. 我有一个主libs文件夹,我所有的android项目都共享该文件夹,我刚刚将此文件夹添加到ant.properties文件中,以使其在这些lib的一级查找。

jar.libs.dir=../libs

As per the question here and my answer taken from here , the offered solution does not work as of now. 按问题在这里和我的答案取自这里 ,所提供的解决方案不作为现在的工作。 Until the filed bug is fixed. 直到已解决的bug修复。 The solution, as mentioned in the referenced answer, is copied below. 如参考答案中所述,该解决方案复制如下。


I add this to my custom_rules.xml template and it seems to be working fine. 我将此添加到我的custom_rules.xml模板中,似乎工作正常。 I supposed it's only a few more lines than adding path to the ant.properties file until we get a better solution. 我认为这只是比将路径添加到ant.properties文件多几行,直到我们得到一个更好的解决方案。

<target name="-pre-compile">
    <path id="project.all.jars.path">
    <path path="${toString:project.all.jars.path}"/>
    <fileset dir="${jar.libs.dir}">
        <include name="*.jar"/>
    </fileset>
    </path>
</target>

If you use Linux, just make symbolic links of the external jar files, then place them all in libs . 如果使用Linux,则只需建立外部jar文件的符号链接,然后将它们全部放入libs Ant will work fine. 蚂蚁可以正常工作。

Edited 已编辑

Make sure place all symbolic links directly in libs (not sub directories). 确保将所有符号链接直接放置在libs (而不是子目录中)。 I'm sure this can be re-config by ant script. 我确定可以通过ant脚本重新配置。 But I'm not familiar with ant :-) 但是我对蚂蚁不熟悉:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我引用一些jre库时,如何使用ant构建android项目 - How to build android project using ant, when i referenced some jre libs 如何在使用ant和外部库时更改android.jar优先级? - How to change the android.jar precedence while using ant and external libs? 如何使用Ant构建Android项目? (IntelliJ IDE) - How do I build an Android project using Ant? (IntelliJ IDE) 如何在Android项目中引用外部lib / jar - How to reference a external lib/jar in Android project 如何使用Ant编译和jar一个Android项目? - How to compile and jar an Android project using Ant? 当测试的项目在libs目录中有jar时,无法构建和运行使用“ant create test-project”创建的android测试项目 - Can't build and run an android test project created using “ant create test-project” when tested project has jars in libs directory 如何在适用于Android项目的Ant build.xml中包含.jar文件 - how to include a .jar file with Ant build.xml for Android project 如何在Android Test-Project(蚂蚁构建)中引用库项目 - How can I reference library project in an Android Test-Project (ant build) 如何在Android上配置ant使其包含外部jar而不将其导出到apk中? - How do I configure ant on Android to include an external jar without exporting it in the apk? 如何使用ant为uiautomator测试项目构建JAR文件 - How to build a JAR file using ant for uiautomator test project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM