Enable Multidex support

The problem

In case the application is using a lot of plugins and compile dependencies, the dexing may fail with

Output: UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536 at ...

It turns out this is Android limitation:

Android application (APK) files contain executable bytecode files in 
the form of Dalvik Executable (DEX) files, 
which contain the compiled code used to run your app. 
The Dalvik Executable specification limits the total number 
of methods that can be referenced within a single DEX file to 65,536, 
including Android framework methods, 
library methods, and methods in your own code.

Android way to fix this

In order to overcome this limitation, here's the official guide how to support multidexing:
https://developer.android.com/tools/building/multidex.html
The idea is to add the following to build.gradle:

android {
    defaultConfig {
        // Enabling multidex support.
        multiDexEnabled true
    }
}

dependencies {
  compile 'com.android.support:multidex:1.0.0'
}

Also some changes must be applied in AndoridManifest.xml OR our own NativeScriptApplication class:

  • modifying default AndroidManifest.xml requires changing the application name:
 <application
        ...
        android:name="android.support.multidex.MultiDexApplication">
        ...
    </application>
  • modifying NativeScriptApplication - in attachBaseContext() method call

Possible way to handle this

Maybe NativeScript CLI can pass some flag to build.gradle and based on it we can enable multidexing and call MultiDex.install(this);