Code Monkey home page Code Monkey logo

dex65536's Introduction

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.

dex65536's People

Contributors

mmin18 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.