Tales from the jar side: Java toolchain support in Gradle, Android view binding, and Trust vs Loyalty
Plus avoiding jokes that will most definitely get you in trouble
Welcome to Tales from the jar side, the Kousen IT newsletter, for the week of November 22 - 29, 2020. This week I taught a class in Basic Android and a class called What’s New In Java, both on the O’Reilly Learning Platform, and worked on my Managing Your Manager book.
Conference In The Cloud
I should mention that on December 7, I’ll be appearing (online, of course) at an unusual event for me. It’s called Conference in the Cloud and is sponsored by The Pragmatic Bookshelf, who are publishing my upcoming Managing Your Manager book.
The conference describes itself as “an organizing mechanism for speakers and agile groups to give presentations on a diversity in topics in a short amount of time for agilists attending remotely.” I’m not normally associated with that group, but it fits for the new book. The conference is only a half-day long, from 1 PM to 5 PM EST. Another difference is that each speaker will have their own Zoom room.
My talk is called Your Boss Is Not Your Friend, and Other Keys to a Successful Manager Relationship, at is scheduled for 3:30 PM. The toughest part for me, as usual, will be compressing everything I have to say down to 50 minutes. :)
Tickets are only $20 and can be purchased here through EventBrite.
What’s New In Java
I always enjoy giving my class on new developments in Java. I present it as an opinionated tour through the features of Java added since Java 8, but since the really important changes to the language came in version 8, I spend more time than most attendees expect going over lambda expressions, method references, streams, static and default methods in interfaces, and the Optional class. I have a nice example that combines all of them, which involves finding the ten longest words in the dictionary. I always expect some student to object, given that Java 8 has been out for over five years now, but no one ever has. It’s probably because I throw in a demo of downstream collectors, which most developers don’t know, and the fact that even after all these years, my Functional Java course still gets a healthy audience every time.
If you want to see it, take a look at the class called ProcessDictionary in the fileio package this GitHub repository. You can execute it by typing gradlew run at the command line, too.
This time, however, the Java 8 information went more quickly than normal. That meant I had plenty of time to talk about all the other features:
Collection factory methods
Enhanced switch statements
The new HTTP client
Pattern matching with the instanceof operator
Records
Text blocks
Sealed classes
and more. I eventually even talked about the Java Platform Module System (JPMS), just enough to discourage anyone who doesn’t need it from using it. (As I said, it’s an opinionated presentation.)
After the class was over, I saw this blog post by the Gradle people, which talks about using the new Java toolchain support in version 6.7. It solves one problem I have, which is that in order to run Gradle on that particular project, I have to switch to Java 15 at the command line (where I normally run Java 11).
To take advantage of that capability, I revised my build file to look like:
Now Gradle will search for my Java 15 implementations, offering to download one if necessary, and run using it with the preview features enabled. It’s working now, though it took a bit more effort to get there than I’m implying. I’m looking forward to showing it to others the next time I teach Gradle. In the meantime, if you need an example, here it is. :)
Android with Jetpack
As I’ve mentioned previously, I struggle in the Basic Android course to explain everything that needs to be said, including fundamentals like activities, intents, and fragments, the Kotlin language (which is now preferred for Android), and the new Jetpack components that are used for modern development. This time I managed to include a few more than normal.
The easiest one is view binding. For example, say you have an XML layout file called activity_main.xml that includes:
Here’s the “before” picture. In the Java (or Kotlin, but let’s go with Java for the moment) activity, you would write code like:
In principle, you risk an error if you get the wrong id’s in the findViewById methods (though I have never once in my career done that), and you could accidentally cast them to variables of the wrong type (which I have also never, not once, ever done).
Now, for the “after” picture, switch to view bindings by adding this block to your build.gradle file in the android section of the app module:
That causes a class to be generated called ActivityMainBinding. You use its static inflate method to create one, assign it to an attribute, use its root property to the setContentView method, and preface everything with the new variable:
That’s way better, right? Well, supposedly view binding allows you to more easily write code that interacts with views. More formally, from the documentation:
View binding has important advantages over using findViewById:
Null safety: Since view binding creates direct references to views, there's no risk of a null pointer exception due to an invalid view ID. Additionally, when a view is only present in some configurations of a layout, the field containing its reference in the binding class is marked with @Nullable.
Type safety: The fields in each binding class have types matching the views they reference in the XML file. This means that there's no risk of a class cast exception.
These differences mean that incompatibilities between your layout and your code will result in your build failing at compile time rather than at runtime.
Yes, that’s all true. But again, it’s very typical of Android to replace a few lines of code with many. The result is arguably better, but certainly more work.
Is it worth it? I guess, in the end, I don’t really have a choice. If I’m going to continue to teach this stuff, and to write code that other Android developers respect, I have to do what they recommend. At least this one is simple.
I planned during the class to also introduce ViewModel and LiveData, but as usual there wasn’t time. I have a new app that’s a slightly revised version of my old joke app, now called ICNDB_Jetpack, in this GitHub repository. It uses data binding (a step beyond view binding), with a ViewModel and mutable LiveData, is organized according to the recommended Guide to Application Architecture, and even uses the Retrofit library to retrieve the jokes via Kotlin coroutines.
I had to use two-way binding to get the hero’s name into the view model, which I’m not sure is the right way to do it, but at least it works. I’m counting that as progress.
Trust vs Loyalty
Most of the preliminary reviews for my draft of Managing Your Manager were pretty short, giving overall comments without asking for specific changes. A couple of the reviewers suggested I talk about some of the issues I bring up change for women or underrepresented minorities. I find that a bit frustrating, because I’ve been warned not to go there, but I might anyway, at least in parts.
One reviewer, however, was quite detailed, and he pointed out a fundamental issue. I frequently use the term loyalty — as in constructive loyalty — when in reality what I’m talking about is trust.
I get that. Both terms — loyalty and trust — carry a lot of meaning. In my regular course, I speak about trust extensively. I set up the idea that both sides need to trust the other.
We trust our managers to:
Fight organizational battles for us
Look out for our best interests
Defend us when problems arise
Get us the resources we need to do our jobs effectively
Support us in our careers
Meanwhile, they trust us to:
Do our jobs to the best of our abilities
Tell them when problems arise, early enough to do something about them
Support their decisions
Back them up to the outside world
I collectively call that a loyalty relationship, the care and feeding of which is a long term project. The constructive part is that you have to be able to push back when you’re not happy, in a way that doesn’t threaten the relationship, otherwise your manager has no reason to adapt to what you want and need.
The reviewer has caused me to rethink how I phrase these issues. I’m probably not being clear enough about them to get my points across. We’ll see.
The Jokes You Don’t Tell
Finally, I used to tell my son that one measure of maturity is the really funny jokes that occur to you that you don’t tell, because they might hurt somebody.
On Thanksgiving, in the early afternoon I was in the kitchen. I briefly watched the turkey cooking away, or whatever it does in the oven. Dinner was going to be in about 30 minutes or so, and everything seemed to be coming along nicely.
I got that tingle I get when I think of something I really shouldn’t oughta do. I very nearly went into my wife’s office and said, “Oh, I forgot to mention. I noticed you accidentally left the oven on again, so I cut it off about an hour ago. Just wanted to let you know.”
Somehow I managed to stop myself, but it was a close call.
Of course, in this case the life I saved was my own. Happy Thanksgiving everybody.
Last week:
Basic Android on the O’Reilly Learning Platform
What’s New In Java, same
Revising Managing Your Manager
This week:
Kotlin Fundamentals on the O’Reilly Learning Platform
Getting Started with Spring and Spring Boot, ditto
Deep Dive into Spring, an NFJS Virtual Workshop
Free webinar on Next Generation Java Testing with JUnit 5 and Mockito, Friday at 1 PM EST. If you get a chance to attend, please say hi :)