Posted at

Gradle Plugin 3.3.0-beta03でConstraintLayoutがinflateできない問題


事象

Android Studio 3.3 Beta3へのアップデートに伴いGradle Pluginを3.3.0-beta03にアップデートしたところ、アプリ起動時にクラッシュが発生し下記のログを吐き出しました。

2018-11-11 15:24:08.515 25666-25666/packageName E/AndroidRuntime: FATAL EXCEPTION: main

Process: packageName, PID: 25666
android.view.InflateException: Binary XML file line #13: Binary XML file line #13: Error inflating class androidx.constraintlayout.widget.ConstraintLayout
Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class androidx.constraintlayout.widget.ConstraintLayout
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:126)
at androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:95)
at packageName.ui.main.MainFragment.onCreateView(MainFragment.kt:43)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:620)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391)
at android.app.Activity.performStart(Activity.java:7165)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:2938)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6680)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/constraintlayout/widget/R$styleable;
at androidx.constraintlayout.widget.ConstraintLayout.init(ConstraintLayout.java:590)
at androidx.constraintlayout.widget.ConstraintLayout.<init>(ConstraintLayout.java:567)
at java.lang.reflect.Constructor.newInstance0(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:343) 
at android.view.LayoutInflater.createView(LayoutInflater.java:647) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:126) 
at androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:95) 
at packageName.ui.main.MainFragment.onCreateView(MainFragment.kt:43) 
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439) 
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460) 
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784) 
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852) 
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802) 
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625) 
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411) 
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366) 
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273) 
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273) 
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229) 
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:201) 
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:620) 
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:178) 
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391) 
at android.app.Activity.performStart(Activity.java:7165) 
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:2938) 
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180) 
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165) 
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6680) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
2018-11-11 15:24:08.515 25666-25666/packageName E/AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.constraintlayout.widget.R$styleable" on path: DexPathList[[zip file "/data/app/packageName-4VcsD5jf_H-fNi0zjif08A==/base.apk"],nativeLibraryDirectories=[/data/app/packageName-4VcsD5jf_H-fNi0zjif08A==/lib/arm64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 40 more


対策

エラーメッセージにError inflating class androidx.constraintlayout.widget.ConstraintLayoutと記載されていたため、ConstraintLayoutが怪しいと思いbuild.gradleのConstraintLayoutのバージョンを確認。

指定していたバージョンは1.1.2でしたが調査時点(2018年11月11日)では1.1.3が最新バージョンであったため1.1.3へアップデート。


build.gradle

    // before

implementation 'androidx.constraintlayout:constraintlayout:1.1.2'

// after
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'


この状態でアプリをビルドしたところエラーは発生しなくなりました。