Android Studio 重複jar包衝突問題
匯入一個新專案,同時用到了v4,v7都是22.1.1包,
compile ('com.android.support:support-v4:22.1.0')
compile('com.android.support:appcompat-v7:22.1.0')
然後就報瞭如下錯誤
com.Android.dex.DexException: Multiple dex files define Landroid/support/annotation/AnimRes;
出現這個問題是因為最新的v4包(compile ‘com.android.support:support-v4:22.1.0’)已經包含了annotation.jar這個包,但其他jar包裡也包含這個包造成的。在晚上找到了幾個正確的解決方法有如下幾種:
1,找到專案其他的依賴包,一定在libs裡的某個jar包裡,隱藏著annotation.jar包,將其刪掉就ok了,此為最正統的解決方法;
2,降級v4包,因為高版本的v4才包含annotation.jar,可以在最外面的build.grable裡強制設定:
allprojects {
repositories {
jcenter()
}
configurations.all {
resolutionStrategy.force ‘com.android.support:support-annotations:22.1.0‘}}
這個方法我試過了,對我這個程式並沒有什麼用
3,在需要v4包的地方exclude掉annotation包,注意compile要多加個括號:
compile (‘com.android.support:support-v4:22.1.0‘){exclude module:‘support-annotations‘}
缺點是每個用v4包的地方都要這麼設定下。 我設定了也還是一樣的問題,後來發現應該是v7內容部也包含了v4,但是和我依賴的v4包不同版本,但是不知道如何查詢v7包內容的v4包版本,所以直接不依賴v4包,又出現如下問題
com.android.dex.DexException: Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl;
好像是依賴的其他專案中包含了v4包,所以再排除v7包中的v4包
compile('com.android.support:appcompat-v7:22.1.0') { exclude module: 'support-v4' }這樣就大功告成了,這個是我一直吐槽的地方,Android Studio 還不能做到直接提示哪個包得哪個檔案重複了,需要自己去尋找,也不能自動選取其中的一個版本,希望在後面的版本中可以改進吧
其他可能忽略的說明:
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) }這邊會預設編譯libs下的jar,需要注意