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

查看:73
本文介绍了指定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',btw,是一个清理版本的这个)。

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

推荐答案

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

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,它仅用于向其他项目提供代码和资源。像一个普通项目一样,图书馆项目通常包含src和res文件夹,以及一个AndroidManifest.xml文件;然而,清单应该大部分是空的,除了清单元素和包属性(不再是true - 现在可以在库项目的清单中声明活动和其他组件)。另外,Library Project的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生成)项目引用到图书馆项目,您需要添加一个android.library.reference.N行进入普通项目的project.properties文件。例如,如果我的主要项目想要指向两个Library Projects,那么我的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天全站免登陆