Which Activity should we use?


There are four different activities:

1- android.app.Activity

Every activity inherits from this one.

This class doesn’t support fragment until API level 11.

This class doesn’t support nested fragments until API level 17.

2- android.support.v4.app.FragmentActivity

This class is from appcompact-v4 library.


  • Supporting nested fragments
  • Loader

To use this class you should add below dependency to gradle.build

compile 'com.android.support:appcompat-v4:x.x.x'

3- android.support.v7.app.AppCompatActivity

This class is from appcompact-v7 library.


  • Supporting all v4 features
  • Material Design

To use this class you should add below dependency to gradle.build

compile 'com.android.support:appcompat-v7:x.x.x'

4- android.support.v7.app.ActionBarActivity

This class is the old name of AppCompatActivity. For various reasons they changed the name.


  • If you want support Material Design look and fragment under API level 21 you should use AppCompatActivity
  • If you want support nested fragments under API level 17 you should use FragmentActivity
  • If you want support fragment under API level 11 you should use FragmenActivity
  • Else use Activity

Quick Overview on Java 8 Features


After all we have some features of java 8 on android :D, lets look at them.

Default Methods

Default methods enable us to add new functionalities to interfaces without breaking the classes that implements that interface (For more information please check this link).

Important Note: This feature just work on API level 24.

Static Methods

A static method is a method that is associated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.

Important Note: This feature just work on API level 24

Repeating Annotations

There are some situations where you want to apply the same annotation to a declaration or type use. Repeating annotations enable you to do this.

Important Note: This feature just work on API level 24

Lambda Expressions

Lambda expression facilitates functional programming, and simplifies the development a lot.
Important characteristics of a lambda expression:

  • Optional type declaration
  • Optional parenthesis around parameter
  • Optional curly braces
  • Optional return keyword

This feature is available on API level 24 and lower.

Method References

You use lambda expressions to create anonymous methods. Sometimes, however, a lambda expression does nothing but call an existing method. In those cases, it’s often clearer to refer to the existing method by name.

This feature is available on API level 24 and lower.



Migrating to Jack compiler


What is Jack?
Google says:

Jack is a new Android toolchain that compiles Java source into Android dex bytecode. It replaces the previous Android toolchain, which consists of multiple tools, such as javac, ProGuard, jarjar, and dx.

What about existing .jar libraries?
Jill tool translates the existing jar libraries into new library format 😀

Why we need migrating to Jack compiler?

  • Speeds compilation time
    Jack compiler can compile java code to .dex in one step

    • Old toolchain: javac (java -> .class) -> dx (.class -> .dex)
    • Jack toolchain: Jack (java -> .jack -> .dex)
  • Using Java8 features
  • Shrinking, obfuscation, repacking without using different tools


Note: Make sure your Android Studio is updated to the last available version.

To enabling Java 8 and Jack for your project your build.gradle file should be like this:

android {
   defaultConfig {
      targetSdkVersion 24
         enabled true
   compileOptions {
      sourceCompatibility JavaVersion.VERSION_1_8
      targetCompatibility JavaVersion.VERSION_1_8

Rebuild your project and it’s should work without any error, But if you see below error:

Error:Could not get unknown property ‘classpath’ for task ‘:app:transformJackWithJackForDebug’ of type com.android.build.gradle.internal.pipeline.TransformTask.

It’s means you should use annotationProcessor dependency scope instead of apt.


Important Note: Instant Run doesn’t work with Jack.