遷移至Android X (Migrate to Android X)

Migrate an existing project using Android Studio

With Android Studio 3.2 and higher, you can migrate an existing project to AndroidX by selecting Refactor > Migrate to AndroidX from the menu bar.

The refactor command makes use of two flags. By default, both of them are set to true in your gradle.properties file:

android.useAndroidX=true

The Android plugin uses the appropriate AndroidX library instead of a Support Library.

android.enableJetifier=true

The Android plugin automatically migrates existing third-party libraries to use AndroidX by rewriting their binaries.

Note: The built-in Android Studio migration might not handle everything. Depending on your build configuration you may need to update your build scripts and Proguard mappings manually. For example, if you maintain your dependency configuration in a separate build file, use the mapping files mentioned below to review and update your dependencies to the corresponding AndroidX packages.


首先會build error:

import android.support.v4.app.Fragment;

原因:

From android.app.Fragment documentation:

This class was deprecated in API level 28.
Use the Support Library Fragment for consistent behavior across all devices and access to Lifecycle.

So support fragments (android.support.v4.app.Fragment) should be used everywhere instead of native fragments(android.app.Fragment) now.

解法1:

android.app.Fragment

解法2:

androidx.fragment.app.Fragment

Error:

android.support.v4.app.FragmentManager

解法1:
https://developer.android.com/reference/androidx/fragment/app/FragmentManager.html

androidx.fragment.app.FragmentManager

解法2:

android.app.FragmentManager

Error:

android.support.v4.app.FragmentTransaction

解法:

androidx.fragment.app.FragmentTransaction

Error:

error: package android.support.v7.widget does not exist

原因是使用了 RecyclerView:

import android.support.v7.widget.RecyclerView;

替代解法:
https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView

androidx.recyclerview.widget.RecyclerView

接著遇到Error:

android.support.v4.widget.SwipeRefreshLayout

解法:
https://developer.android.com/reference/androidx/swiperefreshlayout/widget/SwipeRefreshLayout

androidx.swiperefreshlayout.widget.SwipeRefreshLayout

遇到Error:

android.support.v7.widget.LinearLayoutManager

解法:
https://developer.android.com/reference/androidx/recyclerview/widget/LinearLayoutManager

androidx.recyclerview.widget.LinearLayoutManager

Error:

android.support.v4.app.ActivityCompat

解法:
https://developer.android.com/reference/androidx/core/app/ActivityCompat

androidx.core.app.ActivityCompat

Error:

android.support.v4.content.ContextCompat

解法:

androidx.core.content.ContextCompat

Error:

android.support.annotation.NonNull
android.support.annotation.Nullable

解法:
https://developer.android.com/reference/kotlin/androidx/annotation/NonNull

androidx.annotation.NonNull
androidx.annotation.Nullable;

Error:

android.support.design.widget.FloatingActionButton

解法:
https://developer.android.com/reference/com/google/android/material/floatingactionbutton/FloatingActionButton

com.google.android.material.floatingactionbutton.FloatingActionButton


Error:

android.support.design.widget.TabLayout

解法:
https://developer.android.com/reference/com/google/android/material/tabs/TabLayout

com.google.android.material.tabs.TabLayout

Error:

android.support.v4.app.DialogFragment

解法:
https://developer.android.com/reference/kotlin/androidx/fragment/app/DialogFragment

androidx.fragment.app.DialogFragment

Error:

android.support.v4.app.NavUtils
NavUtils.shouldUpRecreateTask()

舊的method:
https://developer.android.com/reference/android/support/v4/app/NavUtils

shouldUpRecreateTask(Activity sourceActivity, Intent targetIntent)

Returns true if sourceActivity should recreate the task when navigating ‘up’ by using targetIntent.

解法:
https://androidx.de/androidx/appcompat/app/AppCompatActivity.html

supportShouldUpRecreateTask

public boolean supportShouldUpRecreateTask(@NonNull
                                           Intent targetIntent)

Returns true if sourceActivity should recreate the task when navigating ‘up’ by using targetIntent.

If this method returns false the app can trivially call #supportNavigateUpTo(android.content.Intent) using the same parameters to correctly perform up navigation. If this method returns false, the app should synthesize a new task stack by using TaskStackBuilder or another similar mechanism to perform up navigation.Parameters:targetIntent – An intent representing the target destination for up navigationReturns:true if navigating up should recreate a new task stack, false if the same task should be used for the destination


Error:

android.support.v7.app.AlertDialog

解法1:
https://developer.android.com/reference/android/app/AlertDialog.html?hl=zh-tw

android.app.AlertDialog

解法2:

androidx.appcompact.app.AlertDialog

Error:

android.support.v4.app.FragmentPagerAdapter

解法:
https://developer.android.com/reference/androidx/fragment/app/FragmentPagerAdapter

androidx.fragment.app.FragmentPagerAdapter

Error:

android.support.v4.view.ViewPager

解法:
https://developer.android.com/reference/kotlin/androidx/viewpager/widget/package-summary

androidx.viewpager.widget.ViewPager

Error:

android.support.v7.app.AppCompatActivity

解法:
https://developer.android.com/reference/androidx/appcompat/app/AppCompatActivity

androidx.appcompat.app.AppCompatActivity

Error:

android.support.v7.app.ActionBar

解法:
https://developer.android.com/reference/androidx/appcompat/app/ActionBar

androidx.appcompat.app.ActionBar

Error:

android.support.design.widget.Snackbar

解法:
https://developer.android.com/reference/com/google/android/material/snackbar/Snackbar

com.google.android.material.snackbar.Snackbar

Error:

android.support.v4.app.TaskStackBuilder

解法:
https://developer.android.com/reference/androidx/core/app/TaskStackBuilder

androidx.core.app.TaskStackBuilder

Error:

android.support.v4.app.NotificationCompat.Builder

解法:
https://developer.android.com/reference/androidx/core/app/NotificationCompat

androidx.core.app.NotificationCompat

Error:

android.support.multidex.MultiDex

解法:

you can simply add it as a dependency, without non-transparent mangling of name-spaces:

implementation "androidx.multidex:multidex:2.0.1"

除了 source code 有影響之外,layout 或 style xml 檔也跟著被影響:
https://developer.android.com/reference/androidx/coordinatorlayout/widget/CoordinatorLayout

<android.support.design.widget.CoordinatorLayout> 要換成 <androidx.constraintlayout.widget.ConstraintLayout>


<android.support.design.widget.AppBarLayout> 換成 <com.google.android.material.appbar.AppBarLayout>

參考:
https://developer.android.com/reference/com/google/android/material/appbar/AppBarLayout


<android.support.design.widget.TabLayout> 換成 <com.google.android.material.tabs.TabLayout>


<android.support.v4.view.ViewPager> 換成 <androidx.viewpager.widget.ViewPager>

<android.support.design.widget.FloatingActionButton> 換成 <com.google.android.material.floatingactionbutton.FloatingActionButton>


<android.support.v4.widget.SwipeRefreshLayout> 換成 <androidx.swiperefreshlayout.widget.SwipeRefreshLayout>


<android.support.v7.widget.RecyclerView> 換成 <androidx.recyclerview.widget.RecyclerView>


<android.support.v7.widget.CardView> 換成 <androidx.cardview.widget.CardView>

參考:
https://developer.android.com/reference/androidx/cardview/widget/CardView.html


<android.support.v4.widget.NestedScrollView> 換成 <androidx.core.widget.NestedScrollView>

參考:
https://developer.android.com/reference/androidx/core/widget/NestedScrollView


原本的 support 系列:

implementation 'com.android.support:design:28.0.0'

換成:

implementation 'com.google.android.material:material:1.0.0'

<android.support.design.widget.TextInputLayout> tag 換成 <com.google.android.material.textfield.TextInputLayout>

參考:
https://developer.android.com/reference/com/google/android/material/textfield/TextInputLayout


相關文章:

Getting started with Material Components for Android
https://material.io/develop/android/docs/getting-started/

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *