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

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

问题描述

我有两个机器人项目,一个库项目包含自定义布局,并含有利用布局的应用程序的应用程序项目。

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>

然而,这并不:

Whereas this does not:

<?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:

错误没有在包中发现属性layout_horizo​​ntalSpacing资源标识符se.fnord.android

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

资源标识符确实存在的R-文件,并attrs.xml包含在库项目,如果我把布局code和资源,直接在应用程序项目的一切工作正常。该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>

在应用程序文件中体现这样的:

The 'application' manifest like this:

<?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',顺便说一句,是的清理后的版本)。

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

推荐答案

Android SDK中最早的版本不支持在源$ C ​​$ C级共享在一个不错的方式。你可以罐子你的.class文件,然后添加到lib /文件夹,但这种方法并没有允许直接共享源$ C ​​$ c和同样重要的是不支持的资源或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的推出图书馆计划的机制。库项目的结构非常相似,一个正常的Andr​​oid项目,而不是被用来产生一个apk文件,它只会提供code和资源等项目。就像一个普通的项目,库项目通常包含src和资源文件夹,以及一个AndroidManifest.xml文件;然而,清单应主要是空的,除了manifest元素和包的属性(不再是正确的 - 你现在可以宣布活动和其他组件库项目的清单)。此外,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文件。例如,如果我的主要项目要指向两个库项目,我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开始的,它不应该包含的空白或重复。谷歌很清楚,这不是一个完美的系统,但它是一个合理的解决方案,这是使用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.

起初库项目不支持以下内容:

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天全站免登陆