Add Android Compatibility Package (v4, v7 appcompat) in Eclipse

I added the Android compatibility package yesterday and encountered a lot of problems. I will record it here to make the road ahead easier.

how to choose a compatibility pack,

Please refer to Android Support Library Features (2)

1. Download Support Library

Method 1: Right-click on the project → select Android Tools → Add Support Library…

image

Method 2:

Get Support Library through SDK Manager:

1. Open Android  SDK Manager

2. In the SDK Manager window, scroll to the end of the Package List, find the Extra folder, and open the folder to display its contents if necessary.

3. Select the Android Support Library item.

Note: If you are developing with Android Studio, select and install the Android Support Repository project instead of the Android Support Library project.

4. Click the Install packages button.

After the download is complete, the SDK will install the Support Library file to your existing Android SDK directory. The library files are located in the following subdirectories of the SDK: <sdk>/extras/android/support/ directory.

 

2. Add V4 compatibility package (v4 appconpat)

  1. Make sure you have downloaded the Android Support Library using SDK Manager .
  2. Create a libs/ directory at the root of your project .
  3. 从你的Android SDK安装目录(例如,<sdk>/extras/android/support/v4/android-support-v4.jar)下拷贝JAR文件到你项目的libs/目录下。
  4. 右键点击JAR文件并选择Build Path > Add to Build Path。

三、添加V7兼容包(v7 appconpat)

创建一个基于support library代码的 library project

  1. 确保你已经利用 SDK Manager下载了Android Support Library
  2. 创建一个library项目并且确保需要的JAR文件包含在了项目的build path中:
  1. 选择File > Import
  2. 选择Existing Android Code Into Workspace 并点击Next
  3. 浏览SDK安装目录,并进入Support Library目录下。例如,如果你要添加appcompat项目,浏览 <sdk>/extras/android/support/v7/appcompat/。
  4. 点击Finish引入项目。对于v7 appcompat项目,你将看到一个标题为android-support-v7-appcompat的新项目。
  • image
    1. 在新项目中,展开libs/ 文件夹,右键点击每一个.jar文件,并选择Build Path > Add to Build Path。例如,当创建v7 appcompat项目时,同时将android-support-v4.jar和android-support-v7-appcompat.jar文件添加到build path中。
    2. 右键点击library项目文件夹并选择Build Path > Configure Build Path
    3. 在Order and Export选项中,在刚刚添加到build path中的.jar文件上打勾,这时这些文件成为项目可用的了并依赖于这个library项目。例如,appcompat项目要求同时导出android-support-v4.jar和android-support-v7-appcompat.jar文件。
    4. 去掉Android Dependencies上的对勾。
    5. 点击OK完成设置

image

 

 

现在你拥有了一个包含你选择的Support Library的library项目,你可以在一个或多个应用项目中利用这个Support Library。

 

现在我们要向应用工程(需要加入Support Library的工程)添加库:

  1. 在项目浏览器中右键单击你的项目,选择Properties
  2. 在左边的分类面板中,选择Android
  3. 在Library面板中,点击Add
  4. Select the library item and click OK. For example, the appcompat project would appear in the list as android-support-v7-appcompat .
  5. In the properties window, click OK .

image

 

Problems encountered:

1、Java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$attr

This problem is caused by the app not finding the corresponding V7 compatibility package at runtime. Please refer to the article to   add a V7 compatibility package (v7 appconpat)

2、java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

After completing steps 1 and 3 mentioned above, modify android:theme to @style/Theme.AppCompat

image

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326751266&siteId=291194637