Some assignments for filling knowledge gaps. And for your pleasure.

This is a list of questions that was given to one of our Junior Developers, Heinrich Wesson. First part: Knowledge boost for junior Android developers — Part I. The result is a collection of handy skills and information.

1. Annotations

Mention a few useful annotations in Android that we are not using in our code yet.

@RestrictTo(RestrictTo.Scope.SUBCLASSES) : Where the protected modifier makes the method accessible to all classes in the same package, this annotation can be used to make an element accessible to its subclasses only.

@CheckResult : Used on a method, this will cause the compiler to show a warning when the method is called and the returned value is not checked or used, usually meaning that the developer misunderstood what the method does.

2. Java 8 Streams

What are Java (8) streams? When should we use it?

A Stream is a code abstraction added in Java 8. It is a pipeline of operations that can be performed on a data source, usually on Collections . It continues to provide concise and readable code when many different transformations are done. A short overview of Java streams in this Cheat Sheet.

myList

.stream()

.filter(s -> s.startsWith("c"))

.map(String::toUpperCase)

.sorted()

.forEach(System.out::println);

The above would be a long list of for loops in imperative code. However, speed improvements are not guaranteed using Streams. It is a debated subject. Especially the use of Parallel Streams may harm performance. I would suggest the use of Streams only when it would be really difficult for future developers to understand the imperative code.

3. Testing

What types of testing are available on android? Describe some of them shortly.

Unit Tests: Automated testing of code-units by calling for example methods with varying values. Often the response and results are checked, sometimes also the inner workings are spied upon. Frameworks as Mockito and Robolectric ease implementing Unit Tests on Android.

Instrumented Tests. Put your UI to the test on a device or emulator using these tests. You can test many small pieces of your code and also run broader screen and navigation tests. See: Espresso-testing.

Monkey Tests. Used for stress-testing; a monkey slamming your phone.

StrictMode. Notifies you when you created Google policy breaking code.

Firebase Test Lab is Google’s collection of testing tools. Includes automated, general ui testing (Robo Tests, no coding needed) and instrumentation tests on real devices. Reports back with performance metrics, screenshots and video’s.

LeakCanary. Guards your app from the most blatant memory leaks.

Google’s Test Procedures. A checklist for manual testing.

4. Generics

What are Generics? Show a good use case for it.

Generics enables the use of types as parameters using angle brackets ( <> ). Before generics were introduced, developers had to more often cast elements, easily causing ClassCastExceptions . This can be avoided using generics.

List<String> myList = new ArrayList<>();

When using this list, you won’t have to worry that there is anything other than String objects in it. In the entity implementing the type a generic is generally denoted by a T or other single letter:

public class ArrayList<T> extends …

During your code’s compilation, all these generics are replaced with their respective type erasure. Wildcards can be used for type parameters when the type is unknown: <?> , bounded: <? extends Number> or inferred: <? super Integer> . More info about Generics and More Fun with Wildcards.

5. Pair Programming

Explain Pair Programming. Do you think we should try it?

In pair programming, two developers work on one work station. One developer plays the role of the driver, writing the code, and the other person, the observer, reviews the code constantly as it gets written. The two developers switch these roles frequently. (source: Wikipedia)

Pair programming is an agile software development technique. In my opinion, doing stand-ups to discuss problems, code reviews and building a large automated test coverage serves the same purpose in a more efficient way. Also, I am afraid I will fall asleep watching my colleague code. It never hurts to try though, let’s pick a date for it!

Some tips here: Effective pair programming. For the enthusiasts there is also Mob Programming.

6. Support Libraries

What is the difference between support library v4, v7 and v13? When would you use v13 instead of v7?

The difference is no longer the minimum API version:

Starting with Support Library version 26.0.0 (July 2017), the minimum supported API level has changed to Android 4.0 (API level 14) for all support library packages. (source: Google Docs)

v13 features have been merged into v4

v7 packages depend on v4

So in general, referencing v7 is all you need. Unless you want to exclude packages and you do not use anything from v7, then you could reference v4 libraries. Using the support library is also recommended when you target late Android versions; It adds extra features like RecyclerView and CardView. Furthermore, the support library is constantly updated and improved, where Android versions are rather static.

Some semi-recent investigations into the support lib here and here.

7. Reflection

What is reflection and why should you usually not use it?

Reflection enables developers to inspect packages and APIs, for example to see if a specific class or method is available at runtime. One can also access and change private field’s values using reflection.

Method method = classInstance.getMethod(methodNameStr, View.class);

retValue = handler.invoke(method, view);

Drawbacks to using it includes:

Significant performance overhead.

Solutions based on reflection are often fragile. If you are accessing private members and the underlying API changes it might lead to unexpected problems inside your app.

8. Browsing Dependencies

How does one browse a project’s dependency libraries’ packages in Android Studio?

Click on the arrows in the panel of your Project View and select Project. The Project view will now have an External Libraries section where all dependency libraries can be browsed.

9. Anonymous Classes

What is an anonymous class? Show a good use case.

Anonymous classes enable you to declare and instantiate a class at the same time, inline. It often increases code readability, for example when using listeners or callbacks:

myButton.setOnClickListener(

new View.OnClickListener() {

public void onClick(View v) {

addNewItem();

}

});

It becomes more readable using a lambda, same code as above:

myButton.setOnClickListener(v -> addNewItem());

Or even shorter, but maybe less readable:

myButton.setOnClickListener(this::addNewItem);

A warning: inner classes keep a reference to their outer class. This can in certain implementations lead to a memory leak. More info here and here.

10. MVP

There are many flavors of the Model-view-presenter architecture. Note a few things to keep in mind when deciding on one.

Will our presenter persist through orientation change or will it be reconstructed? Both have their benefits; Pros and cons. Will we inject presenters and views using Dagger 2? Using Dagger 2 makes the code more complex to set up, but easier to test. Example. Will we use an abstraction layer (Interface) from View to Presenter as well, or just from Presenter to View? Example of a two-way Contract. Will we use data binding and mix MVP with MVVM? As done here. Will we allow the presenter to know of / access the Context? (Preferably not: it makes testing more difficult.) Some tips here. Will we use an abstraction layer (Interactors) from the Presenter to the data part of the app? Example of Interactor usage. Will we use RXJava and have our presenter subscribe to observables? Just as Dagger 2, RXJava might take some time to get used to, but it is a very powerful tool.

A great collection of MVP flavors and architecture examples can be found in Android Architecture Blueprints.

Happy coding!