Android - Enable app features at compile time

Sometimes some fetaures of your app must behave differently for different flavors o build type. Think about when you need to enable logging for a specific flavor only, or you have to disable crash reporting for your “dev build”. All this behaviours can be configured at compile time, insted of using many if/else blocks that are evaluated at runtime. In the following example I’ll use Gradle and Dagger. Use case: enable crash reporting only for production flavor....

January 28, 2016 · 3 min

OutOfMemoryError while compiling Android projects

Have you ever got a OutOfMemoryError while compiling an Android project, just like this? {% raw %} :app:dexDebug Unknown source file : Uncaught translation error: java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: Java heap space Unknown source file : 1 error; aborting :app:dexDebug FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1 {% endraw %} The quick solution is: add javaMaxHeapSize to dexOptions in your build....

November 15, 2015 · 1 min

A simple splash screen in Android

Splash screens are not so popular in Android development. But wait: now Google started to put splash screens in its apps??? Take a look at Google Drive app for example. Does it mean that now we have a new standard? This is not the right post to talk about if splash screen in Android is good or bad. Let’s think only about code: implementing a splash screen is easy. I’ve seen many splash screen implementations, but some of them have one common bug....

September 7, 2015 · 2 min

RxJava: convert a listener into an Observable

In Java it’s common to work with listeners. And if you work with RxJava, you will prefer to use an Observable instead of listeners. But what if you have to deal with a library and you cannot change the source code? There’s a simple way to create an Observable around a listener. Suppose we have an interface ValueUpdateListener and an object ValueUpdater that will call our listener: public interface ValueUpdateListener { void onValueChanged(@NonNull String value); } public class ValueUpdater { // in some point of the class....

August 28, 2015 · 1 min

ReactiveX, RxJava and RxAndroid - where to start

The next library you will learn should definetly be ReactiveX. What is ReactiveX? From the website: ReactiveX is a combination of the best ideas from the Observer pattern, the Iterator pattern, and functional programming. (But it’s a lot more.) The key concept to start with ReactiveX is: think reactive programming as operating on a stream of data. Can I use ReactiveX with my favourite language? Actually you can find a ReactiveX implementation for the following languages:...

July 30, 2015 · 3 min

Don't waste your time coding - part 2

You can find the first part of this article [here]({{site.baseurl}}/{% post_url 2014-12-16-dont-waste-time-coding-1 %}) Automatic code formatting and import organizing Code styling is important. A well formatted piece of code becomes more readable and more comprehensible. But sometimes trying to keep the code formatted can be negative: you lose your time and your focus. You will soon start to count the number of spaces at the beginning of the current line, or you will think about questions like “should I use 2 or 4 spaces for tabs?...

January 30, 2015 · 3 min

Access Storage Framework and the URI permissions nightmare

I’ve been working with the Access Storage Framework introduced with Android KitKat, a feature that I’ve been waiting for a long time. Everything seemed to be alright, quite easy to implement, until I faced a strange issue. Once the file has been selected by the user, I wanted to store the file’s URI and re-open that URI the next time application is started. To do this, I’ve followed what the official documentation says:...

January 9, 2015 · 2 min

Don't waste your time coding - part 1

Life is too short: please, don’t waste your time writing code! I’m not saying that you don’t have to code, but many people tend to waste too much time typing on the keyboard instead of producing code. Mee too. Following examples relate to Android’s world, but that can be easily adapted to other languages and programming tasks. Aliases Android developers, how many times do you have to call the command adb?...

December 16, 2014 · 2 min

Auto-increment versionCode in build.gradle file

Starting from this blog post by Bryan Rosenbaum, here is a gradle task to auto increment versionCode inside your build.gradle file. import java.util.regex.Pattern ... task incrementVersionCode << { println(":incrementVersionCode - Incrementing Version Code...") def buildGradleFile = file("build.gradle") def patternVersionCode = Pattern.compile("versionCode (\\d+)") def buildGradleFileText = buildGradleFile.getText() def matcherVersionCode = patternVersionCode.matcher(buildGradleFileText) matcherVersionCode.find() def mVersionCode = Integer.parseInt(matcherVersionCode.group(1)) def mNextVersionCode = mVersionCode + 1 def manifestContent = matcherVersionCode.replaceAll("versionCode " + mNextVersionCode) println(":incrementVersionCode - current versionCode=" + mVersionCode); println(":incrementVersionCode - next versionCode=" + mNextVersionCode); buildGradleFile....

November 4, 2014 · 1 min

Android Fragment Code Generator

There are many ways to create a fragment in Android, but to create a fragment properly you need to follow specific guidelines. For example, it’s required that the class has a public zero-parameters constructor. If we need to pass parameters to the fragment, we need to use the method setArguments(). Try to pass some parameters to the constructor as you do with any other class and then look at what happens after a device rotation: parameters will be lost....

September 8, 2014 · 1 min