Dex 65536


Source link: https://github.com/mmin18/Dex65536

Dex 65536

Unable to execute dex: method ID not in [0, 0xffff]: 65536)

When you get this message, normally it is not because your project itself has too much methods, but you are importing some big .jar libraries.

So the easy solution is to pack your .jar libraries in libs/ folder into a secondary .dex file and load that file before your application starts.

How to run sample project

/Dex65536 is the main project, depends on a android library project /Lib.

You can write code in eclipse, but you need to build/run in ant.

android update project -p ./Dex65536 android update project -p ./Lib cd Dex65536 ant clean debug install run 

(Make sure your android_sdk/tools is in the $PATH)

How to make it work on your own project

Step1: build tools

Dex65536/custom_rules.xml Dex65536/pathtool.jar 

Copy these two files in your android project, and execute the following command to generate build.xml.

android update project -p . 

(Make sure your android_sdk/tools is in the $PATH)

Step2: add some code

You need to add some code to load the secondary .dex file before your application starts.

public class App extends Application {

@Override  public void onCreate() {

super.onCreate();

dexTool();
  
}

/**
* Copy the following code and call dexTool() after super.onCreate() in
* Application.onCreate()
* <p>
* This method hacks the default PathClassLoader and load the secondary dex
* file as it's parent.
*/  @SuppressLint("NewApi")  private void dexTool() {

 File dexDir = new File(getFilesDir(), "dlibs");

dexDir.mkdir();

File dexFile = new File(dexDir, "libs.apk");

File dexOpt = new File(dexDir, "opt");

dexOpt.mkdir();

try {

 InputStream ins = getAssets().open("libs.apk");

 if (dexFile.length() != ins.available()) {

  FileOutputStream fos = new FileOutputStream(dexFile);

  byte[] buf = new byte[4096];
  int l;
  while ((l = ins.read(buf)) != -1) {

fos.write(buf, 0, l);

  
}

  fos.close();

 
}

 ins.close();

}
 catch (Exception e) {

 throw new RuntimeException(e);

}

 ClassLoader cl = getClassLoader();

ApplicationInfo ai = getApplicationInfo();

String nativeLibraryDir = null;
if (Build.VERSION.SDK_INT > 8) {

 nativeLibraryDir = ai.nativeLibraryDir;

}
 else {

 nativeLibraryDir = "/data/data/" + ai.packageName + "/lib/";

}

DexClassLoader dcl = new DexClassLoader(dexFile.getAbsolutePath(),
  dexOpt.getAbsolutePath(), nativeLibraryDir, cl.getParent());

 try {

 Field f = ClassLoader.class.getDeclaredField("parent");

 f.setAccessible(true);

 f.set(cl, dcl);

}
 catch (Exception e) {

 throw new RuntimeException(e);

}
  
}
  
}
 

If you don't have a custom Application class, register one in your AndroidManifest.xml like:

<application
  android:name="com.github.mmin18.dex65536.App"
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name" > 

Othersise you just need to copy dexTool() method into your own custom Application and call it after super.onCreate().

Step3: ant build and run

Make sure you have ant installed.

cd /YourProject ant clean debug install run 

Your project should be compile and runnable. Good luck.

Resources

GoogleMapsRippleEffect is an awesome android library to show multiple types of circular ripple effects on google map.

Most powerful wave load view, you can custom any style by yourself.

An avatar ImageView with any kind of shapes.

A demo project which has FlowLayout to work with tags.

A lot of utility classes for Android development.

A Java fluent assertion wrapper to improve readability of RxJava's TestSubscriber assertions.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes