指定 Android 项目依赖项(在 Eclipse 中) [英] Specifying Android project dependencies (in Eclipse)

查看:20
本文介绍了指定 Android 项目依赖项(在 Eclipse 中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 Android 项目,一个包含自定义布局的库项目"和一个包含使用该布局的应用程序的应用程序项目".

I have two Android projects, a 'library project' containing a custom layout, and an 'application project' containing an application which uses the layout.

除了可视化布局编辑器抛出 ClassNotFoundException(我认为这是插件中的错误)之外,一切似乎都构建和执行得很好,但是当我尝试开始使用我为xml 中的自定义布局,我无法再构建.那是;这行得通:

Everything seems to build and execute fine, except that the visual layout editor throws a ClassNotFoundException (which I assume is a bug in the plug-in), but when I try to start to make use of the attributes I defined for the custom layout in the xml, I can no longer build. That is; this works:

<?xml version="1.0" encoding="utf-8"?>
<se.fnord.android.layout.PredicateLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
  <TextView
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="asdfasdf"
    />
</se.fnord.android.layout.PredicateLayout>

而这不是:

<?xml version="1.0" encoding="utf-8"?>
<se.fnord.android.layout.PredicateLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fnord="http://schemas.android.com/apk/res/se.fnord.android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
  <TextView
    fnord:layout_horizontalSpacing="1px"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="asdfasdf"
    />
</se.fnord.android.layout.PredicateLayout>

构建失败并带有来自 aapt 的消息:

The build fails with a message from aapt:

错误在包se.fnord.android"中找不到属性layout_horizo​​ntalSpacing"的资源标识符

ERROR No resource identifier found for attribute 'layout_horizontalSpacing' in package 'se.fnord.android'

资源标识符确实存在于 R 文件中,并且 attrs.xml 包含库项目,如果我将布局代码和资源直接放在应用程序项目中,一切正常.layout_horizo​​ntalSpacing 属性(和 layout_verticalSpacing)是 PredicateLayout.LayoutParam 类中用于指定到下一个小部件的距离的自定义属性.

The resource identifier does exist in the R-file and attrs.xml contained the library project, and if I put the layout code and resources directly in the application project everything works fine. The layout_horizontalSpacing attribute (and layout_verticalSpacing) is a custom attribute used in the PredicateLayout.LayoutParam class to specify the distance to the next widget.

到目前为止,我已经通过指定项目引用和构建路径项目依赖项尝试了标准的 Eclipse 方式.我还被告知尝试应用程序清单中的标记,但没有帮助.

So far I've tried the standard eclipse ways by specifying project references and build path project dependencies. I was also told to try the tag in the application manifest, which did not help.

那么,我需要做什么才能使 xml 文件中的引用起作用?

So, what do I need to do for the references in the xml-file to work?

我不知道它是否相关,但库"清单如下所示:

I don't know if it's relevant, but the 'library' manifest looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="se.fnord.android"
      android:versionCode="1" android:versionName="1.0.0">
</manifest>

应用程序"清单如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="se.fnord.appname"
      android:versionCode="1" android:versionName="1.0.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AppName"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

(顺便说一句,'PredicateLayout' 是 this 的清理版本).

(The 'PredicateLayout', btw, is a cleaned-up version of this).

推荐答案

Android sdk 最早的版本并没有很好的支持源代码级别的共享.您可以将 .class 文件打包,然后将其添加到 lib/文件夹中,但此解决方案不允许直接共享源代码,同样重要的是不支持共享资源或aidl 文件.

The earliest versions of Android sdk did not support sharing at the source code level in a nice way. You could jar up your .class files and then add that into the lib/ folder, but this solution did not allow direct sharing of source code and just as importantly did not support the sharing of resources or aidl files.

然后在 2010 年 5 月,Android 引入了 Library Project 机制.库项目的结构与普通的 Android 项目非常相似,但不是用于生成 apk,它仅用于为其他项目提供代码和资源.与普通项目一样,Library Project 通常包含 src 和 res 文件夹,以及一个 AndroidManifest.xml 文件;然而,除了 manifest 元素和 package 属性之外,清单应该大部分是空的(不再正确 - 您现在可以在图书馆项目的清单中声明活动和其他组件).此外,库项目的 project.properties 文件需要包含以下属性:

Then in May 2010, Android introduced the Library Project mechanism. A Library Project is structured very similar to a normal Android project, but rather than being used to produce an apk, it serves only to provide code and resources to other projects. Like an ordinary project, a Library Project usually contains src and res folders, along with an AndroidManifest.xml file; however the manifest should be mostly empty with the exception of the manifest element and the package attribute (no longer true - you can now declare Activities and other components in the manifest of your Library Project). In addition, the project.properties file for a Library Project needs to contain the property:

"android.library=true"

要从普通(生成apk)项目引用库项目,您需要在普通项目的project.properties 文件中添加android.library.reference.N"行.例如,如果我的主项目想要指向两个库项目,我的主项目的 project.properties 文件将包含以下内容:

To make a reference from an ordinary (apk-producing) project to a Library Project, you need to add a "android.library.reference.N" line into the project.properties file of the ordinary project. For example, if my main project wants to point to two Library Projects, my project.properties file for the main project would include the following:

android.library.reference.1=../LibraryA
android.library.reference.2=../../LibraryB

其中 ../和 ../../是从主项目到库项目的各自路径(这只是一个示例).请注意,此参考列表是从 1 开始的,不应包含空白或重复.Google 很清楚这不是一个完美的系统,但它是一个与 Ant 和 Eclipse 兼容的合理解决方案.一般来说,您的 IDE 会尝试为您维护这些文件,但有时您可能需要手动编辑它们.

where the ../ and the ../../ are the respective paths from the main project to the Library Projects (this is just an example). Note this list of references is 1-based and it should not contain gaps or duplicates. Google is well aware that this is not a perfect system but it was a reasonable solution that was compatible with Ant and Eclipse. Generally speaking, your IDE will attempt to maintain these files for you, but sometimes you may need to edit them by hand.

一开始,Library Projects 不支持以下内容:

At first Library Projects did not support the following:

  1. 指向其他图书馆项目的图书馆项目
  2. 包含aidl 文件的库项目
  3. 包含资产文件夹的库项目

但是随后的 sdk 版本解决了所有这些问题.

However subsequent sdk releases solved all of these problems.

有关图书馆项目的更多信息,请参阅官方文档.

For more information on Library Projects, see the official documentation.

这篇关于指定 Android 项目依赖项(在 Eclipse 中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆