Android Studio Logo

New features

Android Studio 2.3 has finally been released in the Stable Channel. There are many improvements compared to version 2.2, including:

  • Instant run: now there are two different icons, one for restart the app and another one for apply changes without restart. Screen Shot 2017-03-02 at 9.12.10 AM.png
  • Build cache: it’s used to have faster clean builds by caching exploded AARs and pre-dexed external libraries.
  • App Links Assistant (Tools → App Link Assistant): it allows you to create new intent filters for your URLs, declare your app’s website association through a Digital Asset Links file, and test your Android App Links support.
  • Support to WebP lossless image format: the WebP format is . Android Studio 2.3 has a new wizard that converts any non-launcher PNG file to WebP (up to 25% smaller than a PNG) and WebP back to PNG.
  • Constraint Layout with Chains and Ratios: this new version of Android Studio includes the stable release of ConstraintLayout. You can now chain two or more Android views bi-directionally together to form a group on one dimension (helpful when you want to place two views close together but want to spread them across empty space).

Problems and issues

Migration from Android Studio 2.2 to Android Studio 2.3 may not be completely painless.

This is a list of things that you may need to check to have everything working correctly.

  • In your root build.gradle file the gradle version should be updated to 2.3.0:

    classpath ‘’

  • The gradle distribution should be updated to version 3.3, so check for the following line in file gradle/wrapper/


  • If you are using Kotlin, update both the Android Studio plugin and the dependency in your build.gradle to version 1.1.0

  • If you use annotation processors (like ButterKnife for example):

    • remove the following line:

      apply plugin: ‘android-apt’

    • change all occurrences of “apt” to “annotationProcessor”:

      compile ‘com.jakewharton:butterknife:8.4.0’

      annotationProcessor ‘com.jakewharton:butterknife-compiler:8.4.0’

You can find a complete list of improvements in the original blog post:

Android framework provides no support for creating a TextView that can fit its content to its size.

There are some libraries out there that try to solve this problem, like:

But no one of them seems to work in every situation. Looking into stackoverflow I’ve come to this post which seems to hold the best working answer that I’ve tried.

Except when I’ve tried to use it when textAllCaps=”true”. The solution to this issue has already been reported in one of the comments. So I put up all togheter, and the result is the following AutoResizeTextView implementation:

Oggi si è tenuto al FabLab di Padova un Workshop sull’Internet of Things, durante il quale ho avuto il piacere di anticipare l’intervento di Intel con un intervento dal titolo #Google, #Android, #IoT.

Durante questo intervento ho fornito agli attendenti una panoramica sui servizi che Google mette a disposizione per l’universo dell’Internet of Things e mostrato come è possibile far comunicare una app Android con una scheda Arduino, sia a corto raggio tramite Bluetooth che da remoto appoggiandosi su un server Firebase.

Le slide sono liberamente consultabili a questo indirizzo.

Di seguito i riferimenti ai servizi, prodotti e librerie citati nell’intervento:

Grazie a OfficineDigitali ZIP per aver ospitato il mio intervento!

When you run your Android tests (like espresso tests), you may want to be able to force the locale of your device to some specific value at runtime (during test execution). This could be really helpful if you want to test some features of your app against multiple locales.

You can do this by using Junit4 rules. The rule implementation looks like this:

public class ForceLocaleRule implements TestRule {

    private final Locale mTestLocale;
    private Locale mDeviceLocale;

    public ForceLocaleRule(Locale testLocale) {
        mTestLocale = testLocale;

    public Statement apply(Statement base, Description description) {
        return new Statement() {
            public void evaluate() throws Throwable {
                try {
                    if (mTestLocale != null) {
                        mDeviceLocale = Locale.getDefault();

                } finally {
                    if (mDeviceLocale != null) {

    public void setLocale(Locale locale) {
        Resources resources = InstrumentationRegistry.getTargetContext().getResources();
        Configuration config = resources.getConfiguration();
        config.locale = locale;
        resources.updateConfiguration(config, resources.getDisplayMetrics());

The test locale is passed in as a parameter to the constructor. Before test execution we set the test locale. When test is executed, the previous device’s locale will be automatically restored.

And here’s an example of a test where locale il setted to Locale.UK before test execution:

public class MyActivityTest {

    public static final ForceLocaleRule localeTestRule = new ForceLocaleRule(Locale.UK);

    public ActivityTestRule<MyActivity> mMyActivityRule = new ActivityTestRule<>(MyActivity.class);

    private Context mContext;

    public void setUp() {
    	// ...

    public void testSomeMethod() {
    	// ...

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.

First step: create an interface CrashReporter and two implementations: CrashReporterRealImpl and CrashReporterNullImpl.

// the interface
public interface CrashReporter {
  void init(Application application);

  void logException(Exception e);

// this implementation will report crashes
public class CrashReporterRealImpl implements CrashReporter {
  public void init(Application application) {
    Fabric.with(application, new Crashlytics()); 

  public void logException(Throwable t) {

// this implementation will do nothing
public class CrashReporterNullImpl implements CrashReporter {
  public void init(Application application) {
    // do nothing

  public void logException(Throwable t) {
    // do nothing

Second step: configure the build.gradle file for our app.

android {
  // constants
  def BOOLEAN = "boolean"
  def TRUE = "true"
  def FALSE = "false"

  // app features

  defaultConfig {

      // default features

  productFlavors {
      developer {
      qateam {
      playstore {

Third step: use dagger to provide the right instance of CrashReporter.

public class ApplicationModule {

  CrashReporter provideCrashReporter() {
          return new CrashReporterRealImpl();
      } else {
          return new CrashReporterNullImpl();

Final step: inject and use!

public class MyApp extends Application {

  CrashReporter mCrashReporter;

  public void onCreate() {

    component = DaggerMyApp_ApplicationComponent.builder()
            .applicationModule(new ApplicationModule(this))



It could seem a lot of code for just enabling/disabling a feature, but with this approach the code is modular (for example you could easily replace CrashReporterRealImpl with another one that uses another service, or you can temporarly enable crash reporting for another flavor for testing purposes) and there’s no logic that needs to be tested.

With this approach you could also develop new functions directly in the master branch without using a “feature branch”.

Use case: develop a new feture in master branch.

Suppose that we have to work on our new feature called “Experimental Feature”. We just need to create an interface and two implementations as done before:

// the interface
public interface ExperimentalFeatureController {
  void doSomething(Context context);


// this implementation will do the real work
public class ExperimentalFeatureControllerRealImpl implements ExperimentalFeatureController {
  public void doSomething(Context context) {
    // implementation logic here!


// this implementation will do nothing
public class ExperimentalFeatureControllerNullImpl implements ExperimentalFeatureController {
  public void doSomething(Context context) {
    // do nothing


Then create the definitions in build.gradle and add provide method to the dagger module:

public class ApplicationModule {

  ExperimentalFeatureController provideExperimentalFeatureController() {
          return new ExperimentalFeatureControllerRealImpl();
      } else {
          return new ExperimentalFeatureControllerNullImpl();

Now we can continue to develop in the master branch and commit even when our ExperimentalFeatureControllerRealImpl does not work correctly: FEATURE_EXPERIMENTAL_ENABLED will be TRUE only for our “dev build”, and in FALSE for production build.

When ExperimentalFeatureControllerRealImpl will be ready for production, we will just have to set FEATURE_EXPERIMENTAL_ENABLED to TRUE also for production build!