LoginSignup
19
9

More than 5 years have passed since last update.

小さな Android アプリを AndroidX に置き換えてみる

Posted at

概要

Android-KTX の正式版(1.0.0以降)が AndroidX に依存していると知り、いずれは移行しないと厳しいかと思って、小さいアプリで AndroidX への置き換えを試してみました。

置き換え対象のアプリ

ごく小さい機能を持つアプリです。

  • Activity 1つ
  • Fragment 3つ
  • Data Binding なし
  • Android Architechture Components なし
  • SQLite 不使用
  • ほか Annotation Processing なし

環境

これらの情報は Android Studio の Help -> About で参照したものです。

Key Value
Android Studio 3.2.1
Build #AI-181.5540.7.32.5056338, built on October 9, 2018
JRE: 1.8.0_152-release-1136-b06 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0

依存の書き換え

準備

Android Studio の機能である程度は自動的に変換をしてくれるのですが、それを利用するためにはビルド環境をある程度更新しておかないといけませんでした。

build.gradle

Compile SDK Version を 28 に、Gralde Plugin を 3.2.0に更新します。

build.gradle
 android {
-    compileSdkVersion 27
+    compileSdkVersion 28
     defaultConfig {

//...

     dependencies {
-        classpath 'com.android.tools.build:gradle:3.1.3'
+        classpath 'com.android.tools.build:gradle:3.2.0'

Migrate to AndroidX を実行

Find Action (Windows なら Ctrl + Shift + A) で androidx と入力すればメニューが出てくるので、それを実行します。

ss_androidx.png

実行前に現時点のプロジェクトを .zip でバックアップするか聞いてくるので、一応しておきましょう。
(まあ、何かあっても git 操作で回復できるとは思いますが……)

手直し

いろいろ変換されているのですが、このままだとビルドが通らないので手直しをしていきます。

app/build.gradle
dependencies {
     def supportLibVersion = '27.1.1'
-    implementation "com.android.support:appcompat-v7:28.0.0"
-    implementation "com.android.support:cardview-v7:28.0.0"
+    implementation "androidx.appcompat:appcompat-v7:28.0.0"
+    implementation "androidx.cardview:cardview-v7:28.0.0"

自動変換でレイアウトファイルのFCDN指定も自動で直してくれるのかと思ったら、そのFCDN指定が間違っているので、結局自分で書き直す必要があります。


どこを直すか?

Support Library

大体は com.android.support -> androidx でいけそうです。また、v4やv7の指定は不要になっています。

app/build.gradle
-    implementation "com.android.support:appcompat-v7:28.0.0"
-    implementation "com.android.support:cardview-v7:28.0.0"
-    implementation "com.android.support:customtabs:28.0.0"
-    implementation "com.android.support:design:28.0.0"
-    implementation "com.android.support:support-v4:28.0.0"

+    implementation "androidx.appcompat:appcompat:1.0.0"
+    implementation "androidx.cardview:cardview:1.0.0"
+    implementation "androidx.browser:browser:1.0.0"
+    implementation "com.android.support:design:28.0.0"
+    implementation "androidx.core:core:1.0.0"

com.android.support:customtabsandroidx.browser:browser に変わったり、
com.android.support:designcom.android.support:design になっていたりします。

Test

Test の方はバージョン 1.0.0 がなく、 1.1.0 を指定しないと依存解決ができない点に注意します。

-    androidTestImplementation 'com.android.support.test:runner:1.0.2'
-    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
+    androidTestImplementation 'androidx.test:runner:1.1.0'
+    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'

それと、testInstrumentationRunner の方も直しておきます(上の Migration メニューで実行した場合は自動で直っています)。

-        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
+        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

Import と Layout の修正

ソースコードやレイアウトファイルの指定は自分で直す必要があるようでした。
FCDN が変わっただけで、インターフェイスの変更はないので、Import をし直すだけで大丈夫です。クラスやレイアウトのファイル数が多くなると煩雑な作業になるかもしれないです。

あと、Migration すると自動でレイアウトファイルの FCDN が一部書き換わっていますが、そのままだとコンパイルできないので、自分で直します。

修正全体

そこそこ長いので gist に置きました。


おわりに

Annotation Processing とかを使っていない、ごく簡単なアプリであれば、特に問題もなく1時間かからずに移行ができました。
これが業務で開発するような巨大なアプリだと、より時間はかかると思いますので、早めに計画をしておくと慌てずに済むと思います。

参考

19
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
9