1. 程式人生 > 實用技巧 >第一次使用androidx,解決報錯:More than one file was found with OS independent,‘META-INF/androidx..‘

第一次使用androidx,解決報錯:More than one file was found with OS independent,‘META-INF/androidx..‘

提示錯誤為:More than one file was found with OS independent ‘META-INF/androidx.vectordrawable_vectordrawable.version’
我做的工作,本身專案為androidx配置的專案,這個時候我添加了一個Activity和一個IntentService,就報告以上的錯誤,這個錯誤查閱各大網站沒有得到解決,最後自己猜測是引包的原因,將自己的處理步驟整理如下:其中有前幾個步驟可能不太影響,這裡就不去測試了,但是也整理出來。
步驟1:(關鍵因素)
去掉: // implementation ‘com.android.support:appcompat-v7:28.+’

更換為:implementation ‘androidx.appcompat:appcompat:1.0.0+’
檢查庫中是不是有support的庫,全部更換為androidx,更換關係如下:
Old build artifact AndroidX build artifact
com.android.support:support-compat androidx.core:core:1.0.0+
com.android.support:appcompat-v7 androidx.appcompat:appcompat:1.0.0+
com.android.support:design com.google.android.material:material:1.0.0+
com.android.support:multidex androidx.multidex:multidex:2.0.0+
com.android.support:recyclerview-v7 androidx.legacy:legacy-support-v4:1.0.0+
com.android.support:viewpager androidx.viewpager:viewpager:1.0.0+
com.android.support:support-fragment androidx.fragment:fragment:1.0.0+
在這裡插入圖片描述
support遷移到androidx的官方說明:https://developer.android.google.cn/jetpack/androidx/migrate
,其實遷移很簡單,宣告兩個配置android.useAndroidX=true,android.enableJetifier=true,然後引入androidx對應的庫就可以了。

步驟2,(關鍵因素)
androdx的1.0版本支援的是28.0.0以上的版本,一般我們用29.0.0
將版本資訊的配置檔案更改為如下:
buildToolsVersion = “29.0.0”
minSdkVersion = 21
compileSdkVersion = 29
targetSdkVersion = 29
步驟3,(可能不是關鍵影響因素)
忽略有提示錯誤的庫,提示什麼,我們就在配置檔案中新增忽略,新增方法如下
在這裡插入圖片描述
步驟4,(核心關鍵因素)檢查我新建的activity和IntentService發現在標頭檔案中引入的庫包含了Support庫,刪掉import的Support庫,重新import引入androidx的庫,
檢查layout中的xml檔案,發現為android.support.constraint.ConstraintLayout,更改為androidx.constraintlayout.widget.ConstraintLayout
改完後,重新執行,可以正常運行了