Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

65K Dex Limit

Slide 3

Slide 3 text

The Good life in Android Land…

Slide 4

Slide 4 text

Unable to execute dex: method ID not in [0, 0xffff]: 65536 Conversion to Dalvik format failed: Unable to execute dex: method ID not in [0, 0xffff]: 65536 … becomes hell

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

FIGHT BACK

Slide 7

Slide 7 text

• What is this 65K DEX Limit • How to “solve” it • What we have been doing at QuizUp I will talk about..

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

• Android Manifests • Assets folder • Resources folder • Some other files • classes.dex Inside APK

Slide 10

Slide 10 text

The 65K Dex limit • Your app is too big, sorry mate! :( • Simple explanation: You have too many methods in the dex file • More specific explanation: You have too many methods (over 65.536 methods), that can being invoked, in the dex file

Slide 11

Slide 11 text

How to fight this animal Two things you can do:
 A. Cut down the code
 B. Creating multiple Dex files

Slide 12

Slide 12 text

A. Cut down the code 1. Analyze where the code is coming from
 - https://github.com/mihaip/dex-method- counts package com.stupid.dexlimit;
 
 import android.app.Activity;
 import android.os.Bundle;
 import android.util.Log;
 
 public class MainActivity extends Activity
 {
 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
 super.onCreate(savedInstanceState);
 helloPeople();
 }
 
 private void helloPeople()
 {
 Log.d(MainActivity.class.getSimpleName(),
 "How many methods are in this Application??");
 }
 }

Slide 13

Slide 13 text

A. Cut down the code package com.stupid.dexlimit;
 
 import android.app.Activity;
 import android.os.Bundle;
 import android.util.Log;
 
 public class MainActivity extends Activity
 {
 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
 super.onCreate(savedInstanceState);
 helloPeople();
 }
 
 private void helloPeople()
 {
 Log.d(MainActivity.class.getSimpleName(),
 "How many methods are in this Application??");
 }
 } Read in 60307 method IDs. : 60307 : 6 android: 4842 [...] aurelienribon: 99 tweenengine: 99 equations: 22 paths: 5 bolts: 82 butterknife: 36 internal: 7 com: 46491 badlogic: 12414 gdx: 12414 [...] facebook: 3261 [...] [...]

Slide 14

Slide 14 text

A. Cut down the code 1. Analyze where the code is coming from
 - https://github.com/mihaip/dex-method- counts
 - https://github.com/siggijons/dex- methods 
 package com.stupid.dexlimit;
 
 import android.app.Activity;
 import android.os.Bundle;
 import android.util.Log;
 
 public class MainActivity extends Activity
 {
 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
 super.onCreate(savedInstanceState);
 helloPeople();
 }
 
 private void helloPeople()
 {
 Log.d(MainActivity.class.getSimpleName(),
 "How many methods are in this Application??");
 }
 }

Slide 15

Slide 15 text

A. Cut down the code 1. Analyze where the code is coming from
 - https://github.com/mihaip/dex-method- counts
 - https://github.com/siggijons/dex- methods 2. Use smaller libraries - Repack libraries with JarJar package com.stupid.dexlimit;
 
 import android.app.Activity;
 import android.os.Bundle;
 import android.util.Log;
 
 public class MainActivity extends Activity
 {
 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
 super.onCreate(savedInstanceState);
 helloPeople();
 }
 
 private void helloPeople()
 {
 Log.d(MainActivity.class.getSimpleName(),
 "How many methods are in this Application??");
 }
 }

Slide 16

Slide 16 text

package com.stupid.dexlimit;
 
 
 public class MainActivity extends Activity
 {
 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
 super.onCreate(savedInstanceState);
 helloPeople();
 }
 
 private void helloPeople()
 {
 Log.d(MainActivity.class.getSimpleName(),
 "How many methods are in this Application??");
 }
 } How many methods are in this example? • 1 Activity w/ 2 methods • Compile the play service library • Question: How many methods are in the APK file? • Answer: ~20.000 methods (1/3 of 65K) • Solution: only compile what you need (e.g. com.google.android.gms:play- services-maps:7.8.0)

Slide 17

Slide 17 text

A. Cut down the code 1. Analyze where the code is coming from
 - https://github.com/mihaip/dex-method- counts
 - https://github.com/siggijons/dex- methods 2. Use smaller libraries - Repack libraries with JarJar 3. Proguard / Dexguard to shrink your code
 pros: Dexguard can shrink quite much
 cons: Increases your build time package com.stupid.dexlimit;
 
 import android.app.Activity;
 import android.os.Bundle;
 import android.util.Log;
 
 public class MainActivity extends Activity
 {
 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
 super.onCreate(savedInstanceState);
 helloPeople();
 }
 
 private void helloPeople()
 {
 Log.d(MainActivity.class.getSimpleName(),
 "How many methods are in this Application??");
 }
 }

Slide 18

Slide 18 text

How to fight this animal Two things you can do:
 A. Cut down the code
 B. Creating multiple Dex files

Slide 19

Slide 19 text

B. Creating multiple dex files • Normally you can only have 1 dex file • Get around this limit by using 3rd party libraries
 - pros: we can build the project
 - cons: longer building time • Mult-Dexing library from Google 
 - com.android.support:support-v4:21.0.0
 - Easy to use • Dexguard offers split dexing
 - splitdexfile com.facebook.**
 - More complex to use with the obfuscation • Today we use Dexguard

Slide 20

Slide 20 text

Multiple Dex files in QuizUp

Slide 21

Slide 21 text

Multiple Dex files in QuizUp

Slide 22

Slide 22 text

Multiple Dex files in QuizUp

Slide 23

Slide 23 text

Dalvik vs. ART • Difference regarding the dex limit? YES • ART (Android 5.0+) contains native support for multidexing • No native support in Dalvik

Slide 24

Slide 24 text

What we have learned • Dexguard -> Extra complexity level for split dexing • Choose what goes to the secondary dex file! • Build can break on run time when certain code is in secondary dex file • Avoid always being on the edge of the limit • Compile time increases, when split dexing :( • The tools for split dexing is becoming much better • Smile :)

Slide 25

Slide 25 text

QUESTIONS?