July 6, 2023
Android studio generates a template for me that intends to use JetPack Compose. It inherits from ComponentActivity, not AppCompatActivity like the book example. I could just start slashing and typing from the book, but this sounds like a fine time to download code like the book offers. The book gives the links in a short form that resolve (at the present time anyway) to the following on Github:
cd /u1/Projects git clone https://github.com/PacktPublishing/How-to-Build-Android-Apps-with-Kotlin-Second-Edition.git cd How-to-Build-Android-Apps-with-Kotlin-Second-Edition cd Chapter01/Exercise1.03To make my life easier, I make a link for the long directory name:
cd /u1/Projects ln -s How-to-Build-Android-Apps-with-Kotlin-Second-Edition BuildThey give me the entire directory tree for each exercise (thank goodness). Android Studio is actually working in /home/tom/AndroidStudioProjects/Build1.
cd /u1/home/tom/AndroidStudioProjects mv Build1 Build1_XXX cp -var /u1/Projects/Build/Chapter01/Exercise1.03 /u1/home/tom/AndroidStudioProjectsI relaunch Android studio and it gives me a "chooser page", and does not show Exercise1.03. I use "Open" and navigate to Exercise1.03 - it asks me if I want to trust this. I get a couple of warnings. One about Gradle JVM settings to find the Java SDK. The other is more obscure about invalid VCS mapping wanting to use /home/tom as a Git root. It is upgrading Gradle from 7.3.1 to 7.4.2 -- I don't think this has anything to do with me wanting to import this project.
When this all finishes, I click the infamous green triangle and wait to see what happens. The virtual device is still running an old app I was working on, but shortly it reboots and ... after I click the green triangle a second time, I get what looks like a browser page on Google. Which is what is supposed to happen and more or less what I see on page 21 in the book. The thing now is to examine the sources and see if I can make some changes.
It calls itself "My Application" and has this line (which I change):
// webView.loadUrl("https://www.google.com") webView.loadUrl("http://cholla.mmto.org/android")I get an error "CLEARTEXT_NOT_PERMITTED.
This is a policy decision from Google that started with Android 9.0 where they distrust
anything that doesn't come via https: -- however if I pick up my Pixel 6 phone
(running Android 13) and use the browser to navigate to the same page -- no problem!
The fix is to add the following line to the
Now we will try this (just a 320 by 320 jpeg):
I click the green arrow and am running the app, just as shown on page 29.
Note that MainActivity.kt is now very simple:
The new way to lay out UI is using Jetpack compose.
This is using the old way (XML files).
The "ic_launcher" resources are graphics (webp files) that provide the icon used to select and launch
the application. These are "launcher icons".
This introduces the EditText view (two of them) to allow a user to input text.
And it introduces a Button. The method findViewById is vital to the kotlin code.
They also use a "Toast() to briefly display an error message, but it shows up
over the keyboard near the bottom of the screen and is easy to miss.
This is fine, and in some ways simpler (especially if you want to update a view element).
JetPack Compose is the new scheme. I have a book on Jetpack Compose and I am tempted to
complicate my life by learning it now and applying it to the rest of the exercises in
"the book".
android:usesCleartextTraffic=”true”
Now I get told that my application keeps stopping and looking at logcat shows a fatal exception.
Something I did trashed the AndroidManifest -- I find that and am back to another error:
Open quote is expected for attribute "android:usesCleartextTraffic"
This seems to be because I copy and pasted the line and the quotes are not really quotes.
If you squint at them above, you will see they are some other "flavor" of quote.
I use my editor to replace whatever they are with quotes from my keyboard, and that
error goes away. Now it takes me to my page on cholla:, but all I see is a black
screen. How about that? It looks like both foreground and background colors are
black, or some such.
We could pursue this, but that isn't the point right now.
// webView.loadUrl("https://www.google.com")
// webView.loadUrl("http://cholla.mmto.org/android")
webView.loadUrl("http://cholla.mmto.org/gtopo/gtopo.jpg")
Ha! That works just fine.
Gradle scripts
Gradle is the build system used by Android Studio.
By and large the scripts are written in "groovy" (I feel vaguely nauseous when I think about
using that word for a computer language), but it seems that there is a move on to migrate
those scripts to Kotlin. But "the book" uses Groovy for the build scripts.
There are two build.gradle files, one top "project" level file that specifies plugins,
and another "app" level file. These are full of version numbers. library names,
and dependency lists. The book does little more than tell you that these files
are there and give you a quick overview.
Get Exercise 1.04 from Github (Material Design and themes
I already cloned everything for the entire book, so this might be easy:
cp -var /u1/Projects/Build/Chapter01/Exercise1.04 /u1/home/tom/AndroidStudioProjects
I don't close Android studio, but use File - Open to select it and switch to it
in the current window. I get the same warnings as for 1.03.
I ignore the VCS warning, but go with the upgrade to Gradle 7.4.2
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
I navigate to res/layout and look at activity_main.xml. Android Design gives me a layout designer!
The top of the window also offers Code/Split/Design. I can select "Code" and just see the plain XML.
Notice the "R" thing in the Kotlin code. This is a structure that gets built for you
automagically and that you use to access stuff (resources) in the "res" directory.
I'll just note in passing that the project structure shown by android studio is
condensed and simplified from the actual file tree on disk.
Note that ConstraintLayout is a "view group" (other choices are available).
We have one "View", namely TextView.
Get Exercise 1.05 from Github
cp -var /u1/Projects/Build/Chapter01/Exercise1.04 /u1/home/tom/AndroidStudioProjects
The same as before, ignore the VCS warning and upgrade Gradle.
Then click the green arrow.
It builds and launches the app.
XML and "Views" versus Jetpack Compose
"The Book" sticks with the old scheme for setting up android apps through chapter 8.
This old scheme uses XML files to define the layout and individual elements in the
layout are called "Views" -- so you have Views and View Groups.
Have any comments? Questions?
Drop me a line!