简体   繁体   English

如何在eclipse中添加Android Support v7库?

[英]How to add Android Support v7 libraries in eclipse?

How do I add Android Support v7 libraries in eclipse? 如何在eclipse中添加Android Support v7库? For example how do I add CardView to an eclipse android project? 例如,如何将CardView添加到eclipse android项目中?

Note: This was a self answer question, so no need to waste your time making an answer. 注意:这是一个自我回答的问题,所以不需要浪费你的时间来回答。 Of course, if you have anything to add, please do. 当然,如果您有任何要添加的内容,请执行此操作。

From: https://developer.android.com/tools/support-library/setup.html#libs-with-res 来自: https//developer.android.com/tools/support-library/setup.html#libs-with-res

I know this looks like a lot of steps, but in reality it's just very broken down and only takes two minutes 我知道这看起来像很多步骤,但实际上它只是非常简单,只需要两分钟

To add a Support Library with resources (such as v7 cardview) to your application project using Eclipse: 使用Eclipse将包含资源(例如v7 cardview)的支持库添加到应用程序项目:

Create a library project based on the support library code: 基于支持库代码创建库项目:

  • Make sure you have downloaded the Android Support Library using the SDK Manager. 确保您已使用SDK Manager下载了Android支持库。

  • Create a library project and ensure the required JAR files are included in the project's build path: 创建一个库项目并确保所需的JAR文件包含在项目的构建路径中:

  • Select File > Import . 选择文件>导入

  • Select Existing Android Code Into Workspace and click Next. 选择Existing Android Code Into Workspace ,然后单击Next。

  • Browse to the SDK installation directory and then to the Support Library folder. 浏览到SDK安装目录,然后浏览到Support Library文件夹。 For example, if you are adding the cardview project, browse to <sdk>/extras/android/support/v7/cardview/ . 例如,如果要添加cardview项目,请浏览到<sdk>/extras/android/support/v7/cardview/

  • Click Finish to import the project. 单击“完成”以导入项目。 For the v7 cardview project, you should now see a new project titled android-support-v7-cardview . 对于v7 cardview项目,您现在应该看到一个名为android-support-v7-cardview的新项目。

  • In the new library project, expand the libs/ folder, right-click each .jar file and select Build Path > Add to Build Path . 在新库项目中,展开libs/文件夹,右键单击每个.jar文件,然后选择“ 构建路径”>“添加到构建路径” For example, when creating the v7 cardview project, add the android-support-v7-cardview.jar file to the build path. 例如,在创建v7 cardview项目时,将android-support-v7-cardview.jar文件添加到构建路径。 (Depending on your setup, Eclipse may have done this four you already.) (根据您的设置,Eclipse可能已经完成了这四个。)

  • Right-click the library project folder and select Build Path > Configure Build Path . 右键单击库项目文件夹,然后选择“ 构建路径”>“配置构建路径”

  • In the Order and Export tab, check the .jar files you just added to the build path, so they are available to projects that depend on this library project. 在“ 订购和导出”选项卡中,检查刚刚添加到构建路径的.jar文件,以便它们可供依赖于此库项目的项目使用。 For example, the cardview project requires you to export the android-support-v7-cardview.jar file. 例如,cardview项目要求您导出android-support-v7-cardview.jar文件。 (Depending on your setup, Eclipse may have done this four you already.) (根据您的设置,Eclipse可能已经完成了这四个。)

  • Uncheck Android Dependencies . 取消选中Android依赖项

  • Click OK (or Apply and Close ) to complete the changes. 单击“ 确定” (或“ 应用并关闭” )以完成更改。

You now have a library project for your selected Support Library that you can use with one or more application projects. 您现在拥有所选支持库的库项目,可以与一个或多个应用程序项目一起使用。

Now add the library to your application project: 现在将库添加到您的应用程序项目:

  • In the Project Explorer, right-click your project and select Properties . 在Project Explorer中,右键单击项目,然后选择“ 属性”

  • In the category panel on the left side of the dialog, select Android . 在对话框左侧的类别面板中,选择Android

  • In the Library pane, click the Add button. 在“ 库”窗格中,单击“ 添加”按钮。

  • Select the library project and click OK . 选择库项目,然后单击“ 确定” For example, the appcompat project should be listed as android-support-v7-cardview . 例如,appcompat项目应列为android-support-v7-cardview

  • In the properties window, click OK (or Apply and Close ). 在属性窗口中,单击“ 确定” (或“ 应用并关闭” )。

For some libraries, you will get error messages such as 对于某些库,您将收到错误消息,例如

No resource found that matches the given name 'android:Widget.Material.ActionButton'. 找不到与给定名称'android:Widget.Material.ActionButton'匹配的资源。 styles_base.xml/appcompat_v7/res/values-v21 styles_base.xml / appcompat_v7 / RES /值-V21

If that happens, record the highest API level you are seeing in those error messages (v21 in the example above). 如果发生这种情况,请记录您在这些错误消息中看到的最高API级别(上例中的v21)。 Then, in the properties of your library project, go to Android and set the target API to that level (or a higher one), then clean your projects. 然后,在库项目的属性中,转到Android并将目标API设置为该级别(或更高级别),然后清理项目。

This is what worked for me. 这对我有用。 (Using the same CardView example from above) (使用上面相同的CardView示例)

  1. Import CardView into the workspace: 将CardView导入工作区:
    1. Click on File , then click on Import . 单击“ 文件” ,然后单击“ 导入”
    2. Expand the Android folder, and select Existing Android Code into Workspace , then click on Next > . 展开Android文件夹,选择Existing Android Code into Workspace ,然后单击Next>
    3. Click on Browse beside the Root Directory text input, and select the CardView project folder in (Android SDK Install Location) > extras > android > support > v7 > cardview 单击Root Directory文本输入旁边的Browse ,然后在(Android SDK Install Location) > extras > android > support > v7 > cardview选择CardView项目文件夹
  2. Right click the CardView project, click Properties , then select Android from the list on the left. 右键单击CardView项目,单击Properties ,然后从左侧列表中选择Android
  3. Check Is Library checkbox, and click Apply . 选中Is Library复选框,然后单击Apply
  4. In your project, right click and click on Properties , then select Android from the list on the left, and click Add . 在项目中,右键单击并单击“ 属性” ,然后从左侧列表中选择Android ,然后单击“ 添加”
  5. Your imported CardView project will be listed there. 您的导入的CardView项目将在那里列出。 Select the CardView project , then click OK , and click Apply . 选择CardView项目 ,然后单击“ 确定” ,再单击“ 应用”

Now the library is imported. 现在导入库。

PS: I tried I'm_With_Stupid's answer, and while trying to add the library project to my project, it was not listed there. PS:我尝试了I'With_Stupid的答案,在尝试将库项目添加到我的项目时,它没有列在那里。
I did not have to uncheck Android Dependencies, etc. It might work in some other situation, I guess. 我没有取消选中Android Dependencies等。我猜它可能在其他情况下工作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM