1. 程式人生 > >關於Eclipse建立Android專案時,會多出一個appcompat_v7的問題

關於Eclipse建立Android專案時,會多出一個appcompat_v7的問題

轉自:http://blog.csdn.net/crazykbc/article/details/21553699

問題描述:

使用eclipse建立一個Android專案時,發現project列表中會多創建出一個appcompat_v7專案,再建立一個Android專案時,又會再多出一個appcompat_v7_2,如果再次建立,會以此類推地創建出appcompat_v7_x格式的“多餘專案”出來(此情況在ADT升級為22.6.x版本後出現,22.3.x前的版本不會有)

 查明原因:

ADT在22.3.x版本前沒有出現該情況,升級為22.6.x版本後,才出現該情況,可以猜測是新版本導致。猜測到原因後可以分析下appcompat_v7是用來做什麼的,展開appcompat_v7專案,會發現有一個readme.txt檔案,雙擊檢視,該檔案描述如下:

Library Project including compatibility ActionBar.

This can be used by an Android project to provide
access to ActionBar on applications running on API 7+.

There is technically no source, but the src folder is necessary
to ensure that the build system works.  The content is actually
located in libs/android-support-v7-appcompat.jar.
The accompanying resources must also be included in the application.

 字面意思大概可以看出,該專案是一個Library Project, 作用是讓你的Android Project 在API 7+的sdk版本(即Android2.1版本)可以訪問ActionBar的API,ActionBar相當於使用者介面的操作欄,具體ActionBar的解釋可以參考:http://developer.android.com/guide/topics/ui/actionbar.html。所以從中可以看出多出這個project是為了相容低版本的sdk,那我們建立的Android專案是如何引用這個類庫專案呢?通過右鍵點選Android專案---->Properties-->Android,可以檢視到該類庫專案已被引用進來了

 原因已查明,可以驗證一下:

首先建立一個Android專案,在選擇Android版本號的介面,如果選擇的最低Android版本的"大版本號”小於編譯時採用的Android大版本號時,就需要考慮版本相容問題,這樣子創建出專案後自然就會產生出appcompat_v7這個類庫專案出來。具體介面如下圖,如果編譯時採用4.3版本,選擇最低要求版本號為2.x或者3.x時創建出來的專案就會自動建立appcompat_v7支援庫。

 疑問:

可以不產生appcompat_v7這個類庫專案嗎?答案是可以的,只要版本號選擇時最低要求版本和編譯時用的版本選擇同一種即可,但不推薦這麼幹,開發Android程式需要考慮相容多個版本

 補充:

關於Library Project的相關內容,可以通過檢視官方文件介紹,具體檢視:http://developer.android.com/tools/support-library/features.html#v4

找到 v7 appcompat library 的解釋:內容如下:

This library adds support for the Action Bar user interface design pattern.

Note: This library depends on the v4 Support Library. If you are using Ant or Eclipse, make sure you include the v4 Support Library as part of this library's classpath.

Here are a few of the key classes included in the v7 appcompat library:

  •  - Provides an implementation of the action bar user interface pattern. For more information on using the Action Bar, see the Action Bar developer guide.
  •  - Adds an application activity class that must be used as a base class for activities that uses the Support Library action bar implementation.
  •  - Adds support for a standardized sharing action (such as email or posting to social applications) that can be included in an action bar.

This library is located in the <sdk>/extras/android/support/v7/appcompat/ directory after you download the Android Support Libraries. This library contains user interface resources. To include it in your application project, follow the instructions for adding libraries with resources.

The Gradle build script dependency identifier for this library is as follows:

以上是個人解決問題過程中查閱一些資料得出的一些結論,可能會有不對的地方,希望有看出問題的仁兄指點一二。剛開始學習Android開發,有些知識實現太薄弱,為了記錄下平時遇到的一些問題,也只好先將自己理解的一些東西寫出來,等對Android多熟悉一些,再回來檢查下這些文章吧<( ̄︶ ̄)>