Android Developer Links (July 2016)

Photo Credits: MKBHD

Articles

047: Custom Views & ViewGroups with Huyen Tue Dao – Fragmented

In this episode we talk to Trello engineer, GDE, YouTuber, Caster IO instructor and View magician Huyen about all things custom View/ViewGroups.

Exceed the Android Speed Limit! – Google Developers

When it comes to goals or benchmarks for app performance, there is really only one. Does your app consistently draw the screen fast enough so that your user sees smooth, fluid motions, transitions, and responses? It turns out that for that user experience, your application has to do all its work in 16 milliseconds or less for every single screen refresh.

Android Development: some of the best practices. Jun 2016 edition

Use Kotlin, yeah it’s time! Consider it long term investment in your project and skills.
Think twice before adding any third party library; it’s a serious commitment.Use only small, one-feature libraries and abstraction layer above it — would be easier to replace in future.

Instagram + Android: Four Years Later – Instagram Engineering

The first version of Instagram for Android was built in four months by a team of two engineers. It’s been four years since that launch, and in that time we’ve added features such as video, direct messaging, photo maps, advertiser support, and new ways to discover and explore the amazing content shared by users around the world.

Introduction to Kotlin: Android Programming For Humans

Java has been a dominant programming language for ages, but modern times call for modern languages. Meet Kotlin, a modern, clear, and expressive programming language powered by the same virtual machine technology that powers Java.

Create Intelligent, Context-Aware Apps with the Google Awareness APIs | Android Developers Blog

Last month at Google I/O 2016 we announced the new Google Awareness APIs, enabling your apps to intelligently react to user context using snapshots and fences with minimal impact on system resources. Today we’re proud to announce that the Google Awareness API is available to all developers through Google Play services.

Code and Libraries

beworker/featured

Simple yet powerful composition library for Android

Kotlin Basics: apply() and copy()

Without any doubt, Java is a verbose language. This verbosity of the language makes it easy to understand Java code by new programmers. It is though one of the reasons, why many Java programmers find writing Java code tedious. Kotlin brings a touch of succinctness in places, where even programmers new to Java, may find code unnecessarily over-bloated.

How many times have you had to do the same thing over and over again:

Having an object, you want to modify some of its properties and simply return the same object. In Java, you can’t do this in a single chained fashion. In Kotlin, you can squeeze the above method to the following single line of code:

apply()

By definition, apply accepts a function, and sets its scope to that of the object on which apply has been invoked. This means that no explicit reference to the object is needed. Apply() can do much more than simply setting properties of course. It is a transformation function, capable of evaluating complex logic before returning. At the end, the function simply returns the same object (with the added changes), so one can keep using it on the same line of code.

Note: Please, note as well the direct use of properties instead of explicitly calling getters and setters. This is not a bad practice, since Kotlin implicitly invokes provided getters and setters of a property, when working with the property (similar to C#).

copy()

Please, remember that using apply on an object is NOT a thread-safe operation, and mutates the state of the object. If you want to retain the original object and return an immutable copy of it, you can use the function copy(), provided in all instances of data classes.

Assuming that your class contains only data (usually a good practice to separate logic from data), you can turn it into a data class:

Data classes have a few advantages, and one of them is definitely the copy() function, provided to each and every instance. It not only provides a copy of the original object, leaving its state intact, but also allows setting new values to some of its properties at copy-time:

Kotlin supports named parameters, known from other languages, such as Python and Scala. In this case, they make life much easier, as all you need to do, is specify the name of the particular property (or properties) you want to modify by copying.

At the end you can combine the power and succinctness of both apply() and copy() by calling apply after copying:

Dev Ergonomics Tip: Use a Dark-Style GitHub/StackOverflow Theme

As many of the developers I know and have met, I am also used to using a dark colour theme on all of my IDEs and text editors, and the command-line terminal. In fact, if an OSX app (still hard to get used to saying macOS) supports a dark mode, I’d most probably turn it on, to avoid the sharp contrast when switching between my work environment and other applications. One thing that has often irritated me though, has been the switch between my IDE and two of programmers’ go-to websites: GitHub, and StackOverflow. Both feature a completely white background, which has required a brief period of eye adjustment, every time I switched away from my IDE/editor. Well, until today. Turns out that there is a single dark theme for both websites that can be installed through a browser extension, called Stylish. Stylish supports Firefox, Chrome, Opera, Safari and Firefox Mobile, so you should hardly have any problems installing it. Once you have it installed on your browser, go to each of the following links, and install the respective styles for GitHub, and StackOverflow:

GitHub dark styles

StackOverflow dark styles
After installing the styles, refresh the respective pages, and they should then look this:

GitHub dark theme

StackOverflow dark theme

Dark Style Source Code:

https://github.com/StylishThemes/GitHub-Dark

https://github.com/StylishThemes/StackOverflow-Dark

 

Kotlin Basics: Create Executable Kotlin JARs, using Gradle

Kotlin is great for creating small command-line utilities, which can be packaged and distributed as normal JAR files. This short tutorial will show you how to:

  • Set up a Gradle project that supports Kotlin
  • Add a starting function
  • Configure your build to call this function when you execute your JAR.

Setting up Kotlin dependencies

You will also want your IDE (I assume you’re using IntelliJ or Android Studio) to mark the directory where your Kotlin source code resides as a source directory. Since Kotlin and Java are best friends, it is perfectly fine to keep the same package structure. It is a good practice though, to keep your Kotlin code physically separate from your Java one. Thus, you’d typically have two folders under srcsrc/main/java for Java classes, and src/main/kotlin for Kotlin ones. Same for tests. Again, in your build.gradle file, add the following:

Using IntelliJ, you could rely not he IDE to guide you with all of this, but I wanted to show you the basics, since one may not always rely on the comfort of an IDE. To see that everything is working as it should, go to your project directory and create a new build:

If everything has been set up correctly, you should be able to see a task named compileKotlin which the build has executed successfully.

Write your first Kotlin program

Unlike Java, Kotlin is friendlier with functions that reside outside of any class scope. You can create a Main class hosting a main() function, or you can create a top-level main() without necessarily wrapping it in a class. Perhaps, you wouldn’t be able to find any difference in a such a brief examaple, but I find the possibility to create top-level functions helpful in reducing boilerplate code.

Here is the mandatory HelloWorld example. Create a file with an arbitrary name (say Main) and an extension .kt, and write simply:

Note that adding a package is optional, as well as ending your lines with semicolons. In order to keep consistency with my Java code though, I’d usually add both, and expect that people I work with, do the same.

Configure your Gradle build to create an executable JAR

The main function we just added, is enough to test setting up an executable JAR, which one should then be able to call simply by executing:

If you simply try to build your project and then execute the above command, we would get the following message:

This means that we have to configure jar task, which Java Gradle builds go through, and tell it which the starting point of our project is. In a Java project, this would be the path to the class where our main() function resides:

Wait a minute? We have defined our main() function outisde of any class scope. That’s true and not entirely true at the same time. Actually, to keep things at the bytecode level consistent, and backwards-compatible with the JVM, the Kotlin compiler adds all top-level functions to respective classes. In our case, the class generated by the Kotlin compiler would have the same name the filename of the file where our function resides, plus the suffix Kt. This means, for example, that if our file is called Main.kt, the Kotlin compiler would generate a class with the name MainKt.class and add it to the generated JAR file. Knowing this, one could rewrite the Gradle configuration above, as follows:

Note: You can specifiy the name this class should be compiled with, by adding a file-scope annotation on top of your file, even before the package decalration:

This new name can be used within the JAR manifest configuration, as shown above.

Even though we specified our main class correctly in our JAR manifest configuration, if we try to execute our main function using jar -jar, we will still see an error message:

Experienced Java developers will quickly recognize this type of exception. By default when Gradle (as well as Maven) packs some Java class files into a JAR file, it is assumed that this JAR file will be referenced by an application, where all of its dependencies are also accessible within the classpath of the loading application. To execute a JAR without having to specifiy the path to itse dependencies, one must tell the build system to take all of this JAR’s referenced dependencies and copy them as part of the JAR itself. In the Java community, this is known as a “fat JAR”. In a “fat JAR” all of the dependencies end up within the class path of the loading application, so code can be executed without problems. The only downside to creating fat JARs is of course their growing file size (which kind of explains the name), though in most situations, it is not a big concern. In order to tell Gradle to copy all of a JAR’s dependencies, one should simply modify the abovementioned JAR task configuration, by adding the following piece of code:

Further Links

Kotlin Basics: Named Parameters and Default Arguments

Named parameters and default parameter values

Similar to their equivalents in Python and Scala, functions in Kotlin can have named parameters and default parameter values. This makes it easy to provide an arbitrary order of params, by specifiyng them by name. It also allows for skipping certain params altogether, when we do not need to specify them. When declaring a function, this leads to less overhead with overloads. Check out the function joinToString(...) provided to all collections:

when you only want to add values to one or two parameters, you can do like so:

via
Default Arguments
Named Parameters