Hacker Rank’s 30 days of code challenge

For people who are new to programming to those who want to sharpen their coding skills, Hacker Rank has been running the 30 days of code challenge.

Every day a new challenge is posted ranging from introductory topics to mid-level challenges. There is also a new video tutorial every day related to the topic of the challenge.

Hacker Rank 30 days of code challenge

It is a great way to practice problem solving skills and it accepts solutions on several programming languages, such as Java, C, C++, Clojure, D, Fortran, Go, Perl, Swift, Objective-C, Python, among others (you can even code on the Brainfuck language!).

You can access the challenges here: 30-days-of-code challenges

I am posting the Java solutions on my Github account, so if you want to check them, access them here: 30-days-of-code solutions

Google I/O 2016 announced

This week Sundar Pichai, Google CEO, announced in a tweet that Google I/O 2016 has been scheduled for May 18th through the 20th. It will take place on Mountain View’s Shoreline Ampitheatre, back where the conference first stated.

Google I:O 2016

Android N,  updates to Project Tango, Google Cars, Android Wear, Chrome OS, Project Ara and a possible second-generation model of Google Glass are expected!

Google I/O 2015 announced

One of the most anticipated events for Android developers, Google I/O, will occur on May 28-29 and will take place at the Moscone Center West in San Francisco.

Registration for tickets will open on March 17th at 9:00 a.m. PDT and will run for two days. Just like last year, Google will pick the people who will “receive the opportunity to purchase one ticket” at random.

Google I/O

It will also be possible to watch the keynote and select sessions in real-time, from a laptop or mobile device.
More information can be found on the official website: Google I/O 2015

Unit testing support on Android Studio!

With the release of Android Studio 1.1 Beta 4 to the beta channel it now supports unit testing.

Unity testing
Unity testing

The first step to use it is to update your build.gradle file and add the following dependencies:

dependencies {
  testCompile 'junit:junit:4.12'
  testCompile 'org.mockito:mockito-core:1.9.5'
}

Android Studio also needs to be set, as explained in details here.

This is a great addition to Android Studio, improving an already awesome IDE for development and allowing software engineers to become more productive and efficient.

Updating Android properties of all modules from root project

Today at work a friend gave me a very good tip to update properties of all modules of my project from the main project.

My project has 6 modules and a main (root) project. What I used to do when releasing a new version of an app, or updating a property such as build tools, was to go to each module and manually update these properties on their build.gradle file:
apply plugin: 'com.android.application'

android {
compileSdkVersion 20
buildToolsVersion '21.1.1'
    
    defaultConfig {
       versionCode 20
       versionName '20.1.5'
       minSdkVersion 10
       targetSdkVersion 21
    }
}

That meant updating 6 different files (or even more if any of these properties were set on AndroidManifest).

The best approach for this scenario is to have all these properties set on your main project, adding a configuration like this in its build.gradle file:
ext {
    compileSdkVersion = 20
    buildToolsVersion = '21.1.1'


    versionCode = 20
    versionName = '20.1.5'


    minSdkVersion = 10
    targetSdkVersion = 21
}
And on your modules’ build.gradle files you add these configurations:
apply plugin: 'com.android.application'

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        versionCode rootProject.ext.versionCode
        versionName rootProject.ext.versionName
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
    }
}

This way your modules will reference your main project and whenever you need to update one of these properties you will just need to modify one file, improving your project’s maintainability.

Also, ext.* properties are dynamically created so you can add new properties of any type depending on your needs.

That means that you can configure other properties such as sourceCompatibility and targetCompatibility.

Instead of adding them to every module with:
android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}
Add them to the other properties in your main project:
ext {
    compileSdkVersion = 20
    buildToolsVersion = '21.1.1'


    versionCode = 20
    versionName = '20.1.5'


    minSdkVersion = 10
    targetSdkVersion = 21

    sourceCompatibility = JavaVersion.VERSION_1_7
    targetCompatibility = JavaVersion.VERSION_1_7
}
And reference them in the other modules, just like the other properties:
android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        versionCode rootProject.ext.versionCode
        versionName rootProject.ext.versionName
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
    }

    compileOptions {
        sourceCompatibility rootProject.ext.sourceCompatibility
        targetCompatibility rootProject.ext.targetCompatibility
    }

}

Android – Managing the activity lifecycle

Today I will talk about a very important topic in Android mobile apps development: managing the lifecycle of an activity.

When someone navigates through an app the Android system calls a series of lifecycle methods on activities in which developers can set up the user interface and other components and actions. One example is when an user performs an action that starts another activity. When that happens the system calls another set of lifecycle methods on the activity as it moves into the background (where the activity is no longer visible).

The following picture (from Android developer training website, a great place to learn things about Android) shows a simplified illustration of the Activity lifecycle.

Activity lifecycle
Activity lifecycle

When the user first opens an activity the onCreate() callback is called. This method should implement basic application startup logic that should happen only once for the entire life of the activity, like the definition of the user interface.

Once onCreate() finishes execution, the system calls the onStart() and onResume() methods in quick succession. The activity never reside in the Created or Started states. The onStart() method is a good place to verify that required system features are enabled. The onResume() method should perform any other initializations that must occur each time the activity enters the Resumed state (such as initialize components only used while the activity has user focus).

The system calls the onPause() method when the activity is still partially visible and most often is an indication that the user is leaving the activity and it will soon enter the Stopped state. The onPause() callback should be used to stop animations or other ongoing actions that could consume CPU, commit the necessary unsaved changes and release system resources.

The onStop() method is called when the activity is no longer visible and should be used to release almost all resources that aren’t needed while the user is not using it.

The last last callback, onDestroy() is called by the system as the final signal that the activity instance is being completely removed from the system memory. If the app decides to implement this method it can be used to kill background threads created during onCreate() or other long-running resources that could potentially leak memory.

This explanation was a summary of the main points related to the activity lifecycle. There is a lot more involved, especially if you decide to use fragments in your activities. For more information, check the Android developers training website.

The highlights in mobile news this week:

Techcrunch wrote an Android 5.0 Lollipop review: Tablet Edition

The Android official blog wrote about how to design the perfect Lollipop

Google calendar is smarter and prettier

Facebook is changing election day

Dropbox’s Drew Houston responded Snowden’s privacy criticism

Android developers can now publish apps for Android TV on Google Play