June 8, 2023

Android Studio - first steps

I have installed Android Studio Flamingo, and now will try their "first app" tutorial.

(Note - a month later 7-1-2023 -- the tutorial has vanished from the web! It had multiple serious issues, so the world is perhaps better for its loss)

Last year I tried following the "firstapp" tutorial. It had me start with a "Greeting Card" project, and the tutorial was unfortunately telling me to use things in the IDE that did not exist. Clearly the tutorial was not being maintained and kept in sync with changes to the IDE. As it turns out, things have not gotten much better, though the details have changed.

So, I am trying a fresh start now with "Flamingo". I shove my old Greeting Card demo projects out of the way by doing:

cd /home/tom
mv mv AndroidStudioProjects AndroidStudio_OLD
studio
Now I type "studio" and it doesn't bring up the old project by default. It has it in a "recent projects list", but there is a handy "x" to just delete those and I seem to have a clean new start with android studio. I am following the first tutorial. (Now gone).

I select "New Project", and then select the "Empty Activity" template and type "next". I get a form to fill out. As instructed, I call it "Greeting Card". It tells me it will save to /home/tom/AndroidStudioProjects/GreetingCard and that the minimum Android SDK will be Android 7.0 (Nougat). This is a menu that goes as far back as Lollipop (5.0). I click "Finish" and it does a bunch of stuff, but finishes very quickly. Actually it just seems to. The IDE launches but there is a lot of action going on in the progress bar. When it is done I see a message telling my "Gradle sync finished".

Once again, the tutorial is broken

Not only that, but a month later, the tutorial is gone altogether! Well, it was a pretty miserable tutorial, and the template code it started you with had stuff from Jetlab Compose, which I consider somewhat advanced material, not something I first time user should be running into.

The broken part: The tutorial tells me to click "split" at the top right of the GUI, but there ain't no such thing. Near as I can tell it is already done been split. I have 3 big regions side by side. The left shows files and folders (sort of a project navigator). The middle shows code. If I am lucky, the right will show a preview of the app. The tutorial calls these "Project, Code, Design".

If I had a Design window, it would supposedly have a link "Build and Refresh". I fish around looking for such a thing, but no dice. So this tutorial is for some unknown (and not specified in the tutorial) version of the IDE. Maybe Google should get with it and either create a tutorial specific to each version of the IDE, or at least verify which versions of the IDE the tutorial pertains to and specify that up front.

There is a little icon that looks like a hammer that says "build project". I try this and it builds in no time. There is a green triangle to "run project", but it complains with "No target device found"

So let's learn all we can from this broken tutorial.

There is one kotlin file "MainActivity.kt". It has a MainActivity class with an "onCreate" method that serves as main() for an Android App. Within this is a "setContent" function that does the app layout.

We also get a "Greeting" function as well as "GreetingPreview". These are tagged with @Composable.

Use my pixel phone

Details about this in another page, but it is actually pretty easy to enable USB debugging on my Pixel 6 phone and connect it via a USB cable. Once I do this android studio notices it and shows "Google Pixel 6" up top and the green arrow works to launch the app.

And it works! The demo app is now running on my phone. To close it I pull up on the bottom center, then swipe up on the demo app itself. This seems a bit flaky, but when it works the green triangle reappears on android studio and I can load a changed version of the app.

And when I disconnect the cable, the app continues to run. Not only that, but it is installed on my phone and stays there. So, if I only care about being able to run the app myself, I don't need to work up an "apk" file for distribution, but I will definitely want to relearn how to do that.


Have any comments? Questions? Drop me a line!

Adventures in Computing / [email protected]