Kotlin and Android Development featuring Jetpack: Unable to run sample code with Android Studio 2022.2.1

I’ve downloaded the sources for the book (dated 2021) after having bought it on Amazon. However, the downloaded sources don’t run in Android Studio. I was able to enter the example code manually from chapter one and run it without issue, but once I hit chapter 2 things just seem to fall apart: the changes called for on page 37 don’t work (found solutions here), but simply adding the dataBindings line to my gradle file causes Android Studio to complain about duplicate classes, and I’m unable to proceed.

Have there been any attempt to update the sample code?

Hey Darryl!
As you’ve seen, I’ve called out some updates to the code in the past, but I haven’t checked on the code with more recent versions of Android Studio. While the concepts in the book are still applicable, I’m not surprised that you’re running into setup/config issues as you go along.

Can you share the specific error you’re facing after you add the dataBinding = true line to your project? That’ll help me figure out what’s going on.

Sure thing, and thank you for replying so quickly.

I have a working foundation from chapter one, which I put into a git repository here [1]. I then apply the changes from chapter 2, page 37, (with adjustments I found here) to enable databinding:

I then let the project sync. When it finishes, I try to run the app and I get this error:

If I comment out the dataBinding = true change, the app will run.

[1] Added code from chapter 1 · mcpierce/android@15b0080 · GitHub

If I leave the dataBinding = true portion out and add the change to fragment_pick_players.xml (simplified here to only show the changes and not formatting):

and run the app, it blows up when I select the Players button with:

2023-06-04 08:47:46.777 8341-8341 AndroidRuntime dev.mfazio.pennydrop E FATAL EXCEPTION: main
Process: dev.mfazio.pennydrop, PID: 8341
android.view.InflateException: Binary XML file line #4 in dev.mfazio.pennydrop:layout/fragment_pick_players: Binary XML file line #4 in dev.mfazio.pennydrop:layout/fragment_pick_players: Error inflating class layout
Caused by: android.view.InflateException: Binary XML file line #4 in dev.mfazio.pennydrop:layout/fragment_pick_players: Error inflating class layout
Caused by: java.lang.ClassNotFoundException: android.view.layout
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:454)
at android.view.LayoutInflater.createView(LayoutInflater.java:819)
at android.view.LayoutInflater.createView(LayoutInflater.java:780)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:917)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:934)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:954)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1008)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965)
at android.view.LayoutInflater.inflate(LayoutInflater.java:663)
at android.view.LayoutInflater.inflate(LayoutInflater.java:538)
at dev.mfazio.pennydrop.fragments.PickPlayersFragment.onCreateView(PickPlayersFragment.kt:38)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: java.lang.ClassNotFoundException: Didn’t find class “android.view.layout” on path: DexPathList[[zip file “/data/app/~~HBsM_FKp2uF1hBCs4Gfhzg==/dev.mfazio.pennydrop-wv7KlGWNyU0BSJQa_dlnng==/base.apk”],nativeLibraryDirectories=[/data/app/~~HBsM_FKp2uF1hBCs4Gfhzg==/dev.mfazio.pennydrop-wv7KlGWNyU0BSJQa_dlnng==/lib/arm64, /system/lib64, /system_ext/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:218)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:454)
at android.view.LayoutInflater.createView(LayoutInflater.java:819)
at android.view.LayoutInflater.createView(LayoutInflater.java:780)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:917)
at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:934)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:954)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1008)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965)
at android.view.LayoutInflater.inflate(LayoutInflater.java:663)
at android.view.LayoutInflater.inflate(LayoutInflater.java:538)
at dev.mfazio.pennydrop.fragments.PickPlayersFragment.onCreateView(PickPlayersFragment.kt:38)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
---------------------------- PROCESS ENDED (8341) for package dev.mfazio.pennydrop ----------------------------
2023-06-04 08:47:46.796 8341-8341 Process dev.mfazio.pennydrop I Sending signal. PID: 8341 SIG: 9

Another update on this:

If I add back in the dataBinding = true gradle setting then the FragmentPickPlayersBinding class gets generated, which is expected. So the issue here seems to be with the duplicate class being found when adding the plugins for kotlin-kapt and kotlin-android; i.e., something’s causing the runtime to find two versions (2.4.0 and 2.2.0-runtime) for the classes ViewModelLazy and ViewModelProviderKt. So I’m unable to run the code locally when trying to test my changes.

Unfortunately, my gradle fu is relatively weak.

Something you can try quickly to see if it helps: upgrade your appcompat library to 1.6.1:

implementation 'androidx.appcompat:appcompat:1.6.1'

Based on what I’m seeing here, you are definitely not alone in facing this issue with an Android app.

Thank you! That has definitely unstuck me for chapter 2 so far. I’ve entered changes, without running them, up to page 44 as of now. By changing the appcompat dependency to v1.6.1 I’m now able to run it on the emulator.

You’re welcome! Let me know if you run into anything else.