Kotlin and Android Development featuring Jetpack: No activity_main.xml anymore

I just bought this book to learn about Android development, and I’m already running into a major issue in Ch. 1, p. 20: “Update activity_main.xml”. Following the instructions, I was able to build and run the project until this point on my Samsung tablet and in the emulator. However, this paragraph starts out mentioning activity_main.xml and also layout_main.xml, neither of which were created by Android Studio when creating the project.

I did some googling and found that, apparently, as of Android Studio 2022.2 Flamingo released in April 2023, Google changed from using XML to define the layout to using Kotlin only with “Jetpack Compose”. However, I found suggestions from other users that all they needed to do to use the old way was to add activity_main.xml to …/res/layout. After that, they claimed that it worked as before.

So, I tried this, creating …/res/layout/activity_main.xml exactly as described on p. 24 and re-writing MainActivity.kt exactly as described on p. 26. The project builds and loads “successfully” (per Android Studio’s messages) on my Samsung tablet, but all it does is show a white screen for a fraction of a second before crashing. I tried with the emulator, and it did the exact same thing! Unable to find where I went wrong, I re-created the entire project under a different name and followed the book exactly, hoping to fix whatever error I couldn’t see. However, the new project behaves exactly as before. On both real tablet and emulator, trying to launch the app two or three times brings up a message about how Penny Drop (or other name) keeps crashing and asking me if I want to force close it.

I then downloaded the provided project code from the book’s website and tried that, but it won’t even build! Android Studio gives me several errors when I try to do so. The fact that Google apparently so radically changed Android (or at least Android Studio) in the rather short time since this book was printed – and didn’t even provide tools to recognize that it was an “old” project and update it to run – is quite frustrating.

Could you offer any suggestions on how to get past these issues and enjoy (and learn from) the rest of this book, please? Thanks in advance!

Hey Derek!
Thanks so much for the heads up and overview of your troubles! The reality of the situation is that Android Studio has changed quite a bit from when the book was written, so there are extra concerns with trying to get up and running.

We’ve dealt with a few of those issues in other threads on this forum, which could help. Also, I’m going to put together an FAQ post about trying to get up and running with newer versions of Android Studio.

As far as Compose, that was barely in Alpha when the book was released which is why I went with classic XML views for the whole thing. I happen to love Compose, but most places are still partially or fully XML for UI.

I definitely understand the frustration - just after the book came out Google made a change to AS that removed a variable I assumed was there, breaking things on page 7.

I’ll reply here once I get that FAQ together and then you can see if it helps. Otherwise, feel free to post again with specific issues and I can help from there.

Hi!

Thanks for your quick reply! That’s too bad about the timing of Google’s changes to Android Studio. I look forward to reading your FAQ when it is done.

At the moment, I don’t see a forum thread about my most immediate issue: How can I make a project that uses classic XML views, including for the main activity? It seems like I ought to be able to plumb the thing together manually – and that’s what I saw suggested elsewhere, but Android is (apparently) not happy about the result, so I must be missing something that AS used to do automatically. Aside from creating activity_main.xml and making MainActivity.kt look as it is written in the book, is there somewhere else I need to change something to make it work?

Thank you!

Hey!
When creating a new project, the title they use now is “Empty Views Activity”:


That’ll give you a basic project with XML views rather than Compose.

Let me know if that works!

Hi!

I tried that, and it did work for starting a project the way that the book expects – more or less. The “less” is that, as of the latest (just released since I asked the question!) version of Android Studio (Giraffe) defaults to using the “Kotlin DSL” instead of the “Groovy DSL” for Gradle, making the Gradle files in the book incompatible! After some googling, I managed to get that part working (as far as I can tell).

But, as of p. 25 (end of Ch. 1), I ran into the same problem I had earlier: All files (except the non-Groovy Gradle files, of course) match the book, and the app starts to launch (on my real Android tablet), shows a white screen with a purple bar across the top, and then immediately crashes. It WAS working right before I modified activity_main.xml and MainActivity.kt as per pp. 24-25. But, of course, it just said “Hello World” rather than anything too useful. Once I modified these two files to match pp. 24-25, the instant crashing occurs. Confusingly, Android Studio says that it “launched successfully” and doesn’t bring up any debug information. This despite the fact that my tablet brings up a message about “Penny Drop” repeatedly crashing and asks me if I want to close “Penny Drop” or view app information, after I try to run it twice in a row. How perplexing! This is the same behavior I was seeing when I started the “Empty Activity” and then heavily modified it to match the book in my earlier attempts.

I will try to attach a screenshot of the apparently not-concerned Android Studio after all this happens, in case it helps in any way.

Have you opened up logcat yet to see the crash stack trace? That’ll give more insight into what’s going on here. You can find logcat in the bottom bar (or from the link in the screenshot you posted).

Hey!

Thank you for your helpful reply!

I thought that I had opened logcat and found it empty, but I must have opened the wrong panel, because this time logcat was full of helpful information, including exactly the sort of error message I was missing. :slight_smile: Armed with this information, I was able to track down an error I had made in typing the code in, one that was apparently close enough to correct to not trigger any build-time errors but still crash on run.

For the aid of anyone else who falls into this embarrassingly simple trap, here are the details:

FATAL EXCEPTION: main
Process: com.example.pennydrop4, PID: 6462
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pennydrop4/com.example.pennydrop4.MainActivity}: java.lang.ClassCastException: androidx.fragment.app.FragmentContainerView cannot be cast to com.google.android.material.bottomnavigation.BottomNavigationView

This made me realize that the error must be something in either MainActivity.kt or activity_main.xml. The relevant line in MainActivity.kt was:

findViewById<BottomNavigationView>(R.id.bottom_nav)
    .setupWithNavController(this.navController)

Since that was fine, I checked the entry for “bottom_nav” in activity_main.xml and found this:

<androidx.fragment.app.FragmentContainerView
    android:id="@+id/bottom_nav"

I must’ve read and re-read that at least a couple times, but it wasn’t until I saw the error message that I saw how and why it was wrong. It should have read:

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_nav"

Once I fixed that, the project (as it should be at the end of Ch. 1) built and ran correctly, without crashing.