個人アプリのAndroidX対応時のメモ
Support Libraryに依存した3rd party libraryが入っていないせいかあっさり終わった。
移行前の環境
- Android Studio 3.2.1
- compileSdkVersion 28
- targetSdkVersion 28
- Support Library 28.0.0
Refactor -> Migrate to AndroidX
ほぼこれで終わる。ただこれだけではビルドが通らないので修正する。
コンパイルエラーの修正
Non-Nullが返ってきたり型指定が必須になるので直す。
override fun onAttachFragment(fragment: Fragment?)
->override fun onAttachFragment(fragment: Fragment)
override fun onAttach(context: Context?)
->override fun onAttach(context: Context)
progressDialog.show(activity?.supportFragmentManager, null)
->
activity?.supportFragmentManager?.let {
progressDialog.show(it, null)
}
findPreference()
->findPreference<Preference>()
リファクタリング
ツールの不具合かimportしているのにクラスを直指定しているところがあったので修正
- RecylcerView周りの
androidx.recyclerview.widget
を消す androidx.fragment.app.Fragment
->Fragment
androidx.swiperefreshlayout.widget.SwipeRefreshLayout
->SwipeRefreshLayout
クラッシュ修正
ビルドが通るようになったが実行時クラッシュがあった。
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample/com.sample.HogeActivity}: android.view.InflateException: Binary XML file line #23: Binary XML file line #23: Error inflating class com.google.android.material.textfield.TextInputLayout Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
どうやら新しい TextInputLayout
はマテリアルのテーマが必要なようなので、アプリのテーマを Theme.MaterialComponent
にするかTextInputLayoutにstyle="@style/Widget.MaterialComponents.FloatingActionButton”
を適用する。
FloatingActionButtonの色が変わる問題の修正
どうやらデフォルトの背景色がcolorAccentからcolorSecondaryに変わり、アイコンの色(tint)がcolorOnSecondaryに変わった模様。必要であれば色を指定する。
- 旧 https://developer.android.com/reference/com/google/android/material/floatingactionbutton/FloatingActionButton
- 新 https://material.io/develop/android/components/floating-action-button/
おわりに
以外とあっさり終わった。Support Libraryに依存しているライブラリが入っているとその対応を待ったり、下手したらライブラリの置き換えも考慮しなければいけないと思う。あとMigrate to AndroidXするとalphaだろうが最新のバージョンを指定してくるので会社のプロダクトではバージョンを下げたほうが良さそう。