1. 程式人生 > >您有一份AndroidX升級指南未領取

您有一份AndroidX升級指南未領取

轉載請註明出處:juejin.im/post/5bf203…

本文出自 容華謝後的部落格

1.介紹

相信大家對Android Support Libray都不陌生,隨著Android版本的快速迭代,Google推出了Support Libray用以相容低版本的Android裝置。但是隨著版本越來越多,支援庫的維護和使用也變得越來越複雜,於是AndroidX應運而生,接下來我們一起來學習下如何遷移到AndroidX吧!

AndroidX基於Android Support Libray 28.0.0版本進行迭代開發。

環境要求:

  • Android Studio 3.2.0及以上版本

  • compileSdkVersion 28及以上版本

2.遷移到AndroidX

在AS工具欄中點選Refactor——>Migrate to AndroidX選項,出現下面的提示框:

Migrate to AndroidX

意思是說,在遷移之前建議先備份一下當前的專案,勾選【Backup project as Zip File】選項備份,然後點選【MIGRATE】按鈕會提示選擇一個備份路徑,選擇完成後會出現如下提示:

Migrate to AndroidX

點選【DO REFACTOR】按鈕,繼續往下看:

Migrate to AndroidX

出現一個警告,說沒有搜尋到要修改的程式碼,沒關係,點選搜尋之後,再次點選【DO REFACTOR】按鈕就可以了,一陣編譯過後遷移完成,此時編譯一下整個工程,可能會提示一些類找不到的問題,重新匯入一下就可以了。

注意:由於支援庫升級到了AndroidX,一些使用了支援庫的第三方庫也需要升級到最新版本,比如rxlifecycle等,目前大部分主流第三方庫都已經支援AndroidX了。

3.對比

看下和原來的專案有哪些區別:

dependencies

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' } 複製程式碼

被修改成了:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
}
複製程式碼

可以看到com.android.support全部被修改成了androidx,版本號也改變了,同時專案中此依賴的引用包名也會被同時修改。

gradle.properties

增加了以下兩個配置:

  • android.useAndroidX=true

    設定true為時,Android外掛會使用相應的AndroidX庫而不是支援庫,預設為false。

  • android.enableJetifier=true

    設定true為時,Android外掛會自動遷移現有的第三方庫,通過重寫其二進位制檔案來使用AndroidX,預設為false。

4.支援庫遷移AndroidX速查表

Old build artifact AndroidX build artifact
android.arch.core:common androidx.arch.core:core-common:2.0.0-rc01
android.arch.core:core androidx.arch.core:core:2.0.0-rc01
android.arch.core:core-testing androidx.arch.core:core-testing:2.0.0-rc01
android.arch.core:runtime androidx.arch.core:core-runtime:2.0.0-rc01
android.arch.lifecycle:common androidx.lifecycle:lifecycle-common:2.0.0-rc01
android.arch.lifecycle:common-java8 androidx.lifecycle:lifecycle-common-java8:2.0.0-rc01
android.arch.lifecycle:compiler androidx.lifecycle:lifecycle-compiler:2.0.0-rc01
android.arch.lifecycle:extensions androidx.lifecycle:lifecycle-extensions:2.0.0-rc01
android.arch.lifecycle:livedata androidx.lifecycle:lifecycle-livedata:2.0.0-rc01
android.arch.lifecycle:livedata-core androidx.lifecycle:lifecycle-livedata-core:2.0.0-rc01
android.arch.lifecycle:reactivestreams androidx.lifecycle:lifecycle-reactivestreams:2.0.0-rc01
android.arch.lifecycle:runtime androidx.lifecycle:lifecycle-runtime:2.0.0-rc01
android.arch.lifecycle:viewmodel androidx.lifecycle:lifecycle-viewmodel:2.0.0-rc01
android.arch.paging:common androidx.paging:paging-common:2.0.0-rc01
android.arch.paging:runtime androidx.paging:paging-runtime:2.0.0-rc01
android.arch.paging:rxjava2 androidx.paging:paging-rxjava2:2.0.0-rc01
android.arch.persistence.room:common androidx.room:room-common:2.0.0-rc01
android.arch.persistence.room:compiler androidx.room:room-compiler:2.0.0-rc01
android.arch.persistence.room:guava androidx.room:room-guava:2.0.0-rc01
android.arch.persistence.room:migration androidx.room:room-migration:2.0.0-rc01
android.arch.persistence.room:runtime androidx.room:room-runtime:2.0.0-rc01
android.arch.persistence.room:rxjava2 androidx.room:room-rxjava2:2.0.0-rc01
android.arch.persistence.room:testing androidx.room:room-testing:2.0.0-rc01
android.arch.persistence:db androidx.sqlite:sqlite:2.0.0-rc01
android.arch.persistence:db-framework androidx.sqlite:sqlite-framework:2.0.0-rc01
com.android.support.constraint:constraint-layout androidx.constraintlayout:constraintlayout:1.1.2
com.android.support.constraint:constraint-layout-solver androidx.constraintlayout:constraintlayout-solver:1.1.2
com.android.support.test.espresso.idling:idling-concurrent androidx.test.espresso.idling:idling-concurrent:3.1.0
com.android.support.test.espresso.idling:idling-net androidx.test.espresso.idling:idling-net:3.1.0
com.android.support.test.espresso:espresso-accessibility androidx.test.espresso:espresso-accessibility:3.1.0
com.android.support.test.espresso:espresso-contrib androidx.test.espresso:espresso-contrib:3.1.0
com.android.support.test.espresso:espresso-core androidx.test.espresso:espresso-core:3.1.0
com.android.support.test.espresso:espresso-idling-resource androidx.test.espresso:espresso-idling-resource:3.1.0
com.android.support.test.espresso:espresso-intents androidx.test.espresso:espresso-intents:3.1.0
com.android.support.test.espresso:espresso-remote androidx.test.espresso:espresso-remote:3.1.0
com.android.support.test.espresso:espresso-web androidx.test.espresso:espresso-web:3.1.0
com.android.support.test.janktesthelper:janktesthelper androidx.test.jank:janktesthelper:1.0.1
com.android.support.test.services:test-services androidx.test:test-services:1.1.0
com.android.support.test.uiautomator:uiautomator androidx.test.uiautomator:uiautomator:2.2.0
com.android.support.test:monitor androidx.test:monitor:1.1.0
com.android.support.test:orchestrator androidx.test:orchestrator:1.1.0
com.android.support.test:rules androidx.test:rules:1.1.0
com.android.support.test:runner androidx.test:runner:1.1.0
com.android.support:animated-vector-drawable androidx.vectordrawable:vectordrawable-animated:1.0.0
com.android.support:appcompat-v7 androidx.appcompat:appcompat:1.0.0
com.android.support:asynclayoutinflater androidx.asynclayoutinflater:asynclayoutinflater:1.0.0
com.android.support:car androidx.car:car:1.0.0-alpha5
com.android.support:cardview-v7 androidx.cardview:cardview:1.0.0
com.android.support:collections androidx.collection:collection:1.0.0
com.android.support:coordinatorlayout androidx.coordinatorlayout:coordinatorlayout:1.0.0
com.android.support:cursoradapter androidx.cursoradapter:cursoradapter:1.0.0
com.android.support:customtabs androidx.browser:browser:1.0.0
com.android.support:customview androidx.customview:customview:1.0.0
com.android.support:design com.google.android.material:material:1.0.0-rc01
com.android.support:documentfile androidx.documentfile:documentfile:1.0.0
com.android.support:drawerlayout androidx.drawerlayout:drawerlayout:1.0.0
com.android.support:exifinterface androidx.exifinterface:exifinterface:1.0.0
com.android.support:gridlayout-v7 androidx.gridlayout:gridlayout:1.0.0
com.android.support:heifwriter androidx.heifwriter:heifwriter:1.0.0
com.android.support:interpolator androidx.interpolator:interpolator:1.0.0
com.android.support:leanback-v17 androidx.leanback:leanback:1.0.0
com.android.support:loader androidx.loader:loader:1.0.0
com.android.support:localbroadcastmanager androidx.localbroadcastmanager:localbroadcastmanager:1.0.0
com.android.support:media2 androidx.media2:media2:1.0.0-alpha03
com.android.support:media2-exoplayer androidx.media2:media2-exoplayer:1.0.0-alpha01
com.android.support:mediarouter-v7 androidx.mediarouter:mediarouter:1.0.0
com.android.support:multidex androidx.multidex:multidex:2.0.0
com.android.support:multidex-instrumentation androidx.multidex:multidex-instrumentation:2.0.0
com.android.support:palette-v7 androidx.palette:palette:1.0.0
com.android.support:percent androidx.percentlayout:percentlayout:1.0.0
com.android.support:preference-leanback-v17 androidx.leanback:leanback-preference:1.0.0
com.android.support:preference-v14 androidx.legacy:legacy-preference-v14:1.0.0
com.android.support:preference-v7 androidx.preference:preference:1.0.0
com.android.support:print androidx.print:print:1.0.0
com.android.support:recommendation androidx.recommendation:recommendation:1.0.0
com.android.support:recyclerview-selection androidx.recyclerview:recyclerview-selection:1.0.0
com.android.support:recyclerview-v7 androidx.recyclerview:recyclerview:1.0.0
com.android.support:slices-builders androidx.slice:slice-builders:1.0.0
com.android.support:slices-core androidx.slice:slice-core:1.0.0
com.android.support:slices-view androidx.slice:slice-view:1.0.0
com.android.support:slidingpanelayout androidx.slidingpanelayout:slidingpanelayout:1.0.0
com.android.support:support-annotations androidx.annotation:annotation:1.0.0
com.android.support:support-compat androidx.core:core:1.0.0
com.android.support:support-content androidx.contentpager:contentpager:1.0.0
com.android.support:support-core-ui androidx.legacy:legacy-support-core-ui:1.0.0
com.android.support:support-core-utils androidx.legacy:legacy-support-core-utils:1.0.0
com.android.support:support-dynamic-animation androidx.dynamicanimation:dynamicanimation:1.0.0
com.android.support:support-emoji androidx.emoji:emoji:1.0.0
com.android.support:support-emoji-appcompat androidx.emoji:emoji-appcompat:1.0.0
com.android.support:support-emoji-bundled androidx.emoji:emoji-bundled:1.0.0
com.android.support:support-fragment androidx.fragment:fragment:1.0.0
com.android.support:support-media-compat androidx.media:media:1.0.0
com.android.support:support-tv-provider androidx.tvprovider:tvprovider:1.0.0
com.android.support:support-v13 androidx.legacy:legacy-support-v13:1.0.0
com.android.support:support-v4 androidx.legacy:legacy-support-v4:1.0.0
com.android.support:support-vector-drawable androidx.vectordrawable:vectordrawable:1.0.0
com.android.support:swiperefreshlayout androidx.swiperefreshlayout:swiperefreshlayout:1.0.0
com.android.support:textclassifier androidx.textclassifier:textclassifier:1.0.0
com.android.support:transition androidx.transition:transition:1.0.0
com.android.support:versionedparcelable androidx.versionedparcelable:versionedparcelable:1.0.0
com.android.support:viewpager androidx.viewpager:viewpager:1.0.0
com.android.support:wear androidx.wear:wear:1.0.0
com.android.support:webkit androidx.webkit:webkit:1.0.0

5.一些問題

升級到AS 3.2版本後,Android Lint增加了一些程式碼檢測項,其中最為明顯的就是AndroidManifest.xml中的警告:

App is not indexable by Google Search; consider adding at least one Activity with an ACTION-VIEW intent filter. See issue explanation for more details.
複製程式碼

意思是當前APP不支援Google搜尋,需要至少在一個Activity中新增intent filter,沒問題,滿足你:

<action android:name="android.intent.action.VIEW" />
複製程式碼

但是因為萬里長城的原因,添加了這個filter也沒什麼用,不新增的話滿頁的黃色波浪又看著鬧心,我們還可以這樣做:

在app的build.gradle中新增一個警告忽略:

android {
    ...

    lintOptions {
        disable 'GoogleAppIndexingWarning'
    }
}
複製程式碼

或者在AndroidManifest.xml中的application項中新增一下忽略也可以:

tools:ignore="GoogleAppIndexingWarning"
複製程式碼

6.寫在最後

到這裡,AndroidX升級相關的內容就學習完了,希望對你能夠有所幫助,如果你在升級AndroidX的過程中,遇到了一些其他的問題,歡迎給我留言。