Giter Site home page Giter Site logo

mmin18 / dex65536 Goto Github PK

View Code? Open in Web Editor NEW
350.0 32.0 121.0 2.93 MB

Solve the issue with dalvik compiler limitation on 65536 methods (Unable to execute dex: method ID not in [0, 0xffff]: 65536)

License: MIT License

Java 100.00%
dalvik dex apk android

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

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dex65536's Issues

Build failed

H:\software\dex>ant debug install run
Buildfile: H:\software\dex\build.xml

-set-mode-check:

-set-debug-files:

-check-env:
[checkenv] Android SDK Tools Revision 22.6.0
[checkenv] Installed at D:\adt-bundle-windows-x86\sdk

-setup:
[echo] Project Name: MainActivity
[gettype] Project Type: Application

-set-debug-mode:

-debug-obfuscation-check:

-pre-build:

-build-setup:
[getbuildtools] Using latest Build Tools: 19.0.3
[echo] Resolving Build Target for MainActivity...
[gettarget] Project Target: Android 4.3
[gettarget] API level: 18
[echo] ----------
[echo] Creating output directories if needed...
[mkdir] Created dir: H:\software\dex\bin
[mkdir] Created dir: H:\software\dex\bin\res
[mkdir] Created dir: H:\software\dex\bin\rsObj
[mkdir] Created dir: H:\software\dex\bin\rsLibs
[mkdir] Created dir: H:\software\dex\gen
[mkdir] Created dir: H:\software\dex\bin\classes
[mkdir] Created dir: H:\software\dex\bin\dexedLibs
[echo] ----------
[echo] Resolving Dependencies for MainActivity...
[dependency] Library dependencies:
[dependency] No Libraries
[dependency]
[dependency] ------------------
[echo] ----------
[echo] Building Libraries with 'debug'...
[subant] No sub-builds to iterate on

-code-gen:
[mergemanifest] Merging AndroidManifest files into one.
[mergemanifest] Manifest merger disabled. Using project manifest only.
[echo] Handling aidl files...
[aidl] No AIDL files to compile.
[echo] ----------
[echo] Handling RenderScript files...
[echo] ----------
[echo] Handling Resources...
[aapt] Generating resource IDs...
[echo] ----------
[echo] Handling BuildConfig class...
[buildconfig] Generating BuildConfig class.

-pre-compile:

-compile:
[javac] Compiling 10 source files to H:\software\dex\bin\classes

-post-compile:
[pathtool] include H:\software\dex\libs\10k-methods.jar
[pathtool] include H:\software\dex\libs\android-support-v4.jar
[mkdir] Created dir: H:\software\dex\bin\libs.apk
[dex] Converting compiled files and external libraries into H:\software\dex\bin\libs.apk\classes.
[dx] no input files specified
[dx] usage:
[dx] dx --dex [--debug] [--verbose] [--positions=<style>] [--no-locals]
[dx] [--no-optimize] [--statistics] [--[no-]optimize-list=] [--no-strict]
[dx] [--keep-classes] [--output=] [--dump-to=] [--dump-width=]
[dx] [--dump-method=[*]] [--verbose-dump] [--no-files] [--core-library]
[dx] [--num-threads=] [--incremental] [--force-jumbo]
[dx] [--multi-dex [--main-dex-list= [--minimal-main-dex]]
[dx] [.class | .{zip,jar,apk} | ] ...
[dx] Convert a set of classfiles into a dex file, optionally embedded in a
[dx] jar/zip. Output name must end with one of: .dex .jar .zip .apk or be a directory.
[dx] Positions options: none, important, lines.
[dx] --multi-dex: allows to generate several dex files if needed. This option is
[dx] exclusive with --incremental, causes --num-threads to be ignored and only
[dx] supports folder or archive output.
[dx] --main-dex-list=: is a list of class file names, classes defined by
[dx] those class files are put in classes.dex.
[dx] --minimal-main-dex: only classes selected by --main-dex-list are to be put in
[dx] the main dex.
[dx] dx --annotool --annotation= [--element=]
[dx] [--print=]
[dx] dx --dump [--debug] [--strict] [--bytes] [--optimize]
[dx] [--basic-blocks | --rop-blocks | --ssa-blocks | --dot] [--ssa-step=]
[dx] [--width=] [.class | .txt] ...
[dx] Dump classfiles, or transformations thereof, in a human-oriented format.
[dx] dx --find-usages <file.dex>
[dx] Find references and declarations to a field or method.
[dx] declaring type: a class name in internal form, like Ljava/lang/Object;
[dx] member: a field or method name, like hashCode
[dx] dx -J ... <arguments, in one of the above forms>
[dx] Pass VM-specific options to the virtual machine that runs dx.
[dx] dx --version
[dx] Print the version of this tool (1.8).
[dx] dx --help
[dx] Print this message.

BUILD FAILED
H:\software\dex\custom_rules.xml:26: null returned: 1

i took the updated clone still i am getting the same build failed error , can u please suggest me with this .....

in the project.properties,I have some my own -keep

in the project.properties,I have some my own -keeps, when I called "android update project - p",the file "project.properties" content auto changed to

-keepclassmembers class fqcn.of.javascript.interface.for.webview {
public *;
}

and the app can install normally, but it cannot open, and lots of Lcom cannot find

what i can do by this?thanks

Using loaded classes in Application

I'm facing the problem that I'm not able to call a class packaged in a JAR, in my Application on onCreate(), after calling dexTool(). However, I can call it within my Activity.
Any suggestions?

Build Failed

Hi , I had followed same process in my case. i am getting the error at exactly like

BUILD FAILED
D:\BuzzBoard_Workspace\BuzzBoardWork\custom_rules.xml:29: Problem: failed to
ate task or type pathtool
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any / declarations have taken place.

i used custome_rule file and did not chaged anything. i had keep as it is.

please give me sujjetion. i had waste almost one week on this 65356 method issue.

Library Project not loaded when completed the ant build with the process

Hi
Thanks for your ant build process.it is very much helpful to the large project(exceeds 65536 methods).but i got an issue while running the project, that is project launched successfully but my library project is not loaded properly.its getting error when accessing the library project.i have third party library project for swipe listview. its getting error when access the swipe list view view in xml. please help me for this issue.

i have mentioned the issues in below

Caused by: android.view.Inflate Exception: Binary XML file line #166: Error inflating class com.fortysevendeg.swipe list view.SwipeListView

Caused by: java.lang.NoClassDefFoundError: com.fortysevendeg.swipelistview.R$styleable

please help me
thanks in advance

ant debug ok, but ant release failed

I found ant debug runs well , but when i use 'ant release', build fail, the error msg is

"Multiple dex files define ...."

i want to know the reason and how to solve it, thank you!

BUILD FAILED

Hi,

I have downloaded the project and followed all the steps you have mentioned. i have ant installed on my system.The project was building successfully if i used just "ant" command on the root directory of the project but i get the following error if i use "ant debug" command.

BUILD FAILED
C:\Users\3124\Downloads\Reference_Materials\Dex65536-master\Dex65536\custom_rule
s.xml:26: null returned: 1.

I dont have a clue as to what is going wrong. so can someone explain what had i done wrong.

Build Failed

i got now another error after executing the debug command pressed;
error:---
Buildfile: D:\BuzzBoard_Workspace\BuzzBoardWork\build.xml

-set-mode-check:

-set-debug-files:

-check-env:
[checkenv] Android SDK Tools Revision 22.6.3
[checkenv] Installed at D:\09114\MobileDevSoftwares\android-sdk-windows

-setup:
[echo] Project Name: SigninActivity
[gettype] Project Type: Application

-set-debug-mode:

-debug-obfuscation-check:

-pre-build:

-build-setup:
[getbuildtools] Using latest Build Tools: 19.1.0
[echo] Resolving Build Target for SigninActivity...
[gettarget] Project Target: Android 4.4.2
[gettarget] API level: 19
[echo] ----------
[echo] Creating output directories if needed...
[mkdir] Created dir: D:\BuzzBoard_Workspace\BuzzBoardWork\bin\rsObj
[mkdir] Created dir: D:\BuzzBoard_Workspace\BuzzBoardWork\bin\rsLibs
[echo] ----------
[echo] Resolving Dependencies for SigninActivity...
[dependency] Library dependencies:
[dependency] No Libraries
[dependency]
[dependency] ------------------
[echo] ----------
[echo] Building Libraries with 'debug'...
[subant] No sub-builds to iterate on

-code-gen:
[mergemanifest] Found Deleted Target File
[mergemanifest] Merging AndroidManifest files into one.
[mergemanifest] Manifest merger disabled. Using project manifest only.
[echo] Handling aidl files...
[aidl] No AIDL files to compile.
[echo] ----------
[echo] Handling RenderScript files...
[echo] ----------
[echo] Handling Resources...
[aapt] Found Deleted Target File
[aapt] Generating resource IDs...
[echo] ----------
[echo] Handling BuildConfig class...
[buildconfig] Generating BuildConfig class.

-pre-compile:

-compile:
[javac] Compiling 5 source files to D:\BuzzBoard_Workspace\BuzzBoardWork
\classes

-post-compile:
[dex] Converting compiled files and external libraries into D:\BuzzBoa
orkspace\BuzzBoardWork\bin\libs.apk\classes.dex...
[dx] no input files specified
[dx] usage:
[dx] dx --dex [--debug] [--verbose] [--positions=<style>] [--no-loc

   [dx]   [--no-optimize] [--statistics] [--[no-]optimize-list=<file>] [

-strict]
[dx] [--keep-classes] [--output=] [--dump-to=] [--dump-
h=]
[dx] [--dump-method=[*]] [--verbose-dump] [--no-files] [--cor
brary]
[dx] [--num-threads=] [--incremental] [--force-jumbo]
[dx] [--multi-dex [--main-dex-list= [--minimal-main-dex]]
[dx] [.class | .{zip,jar,apk} | ] ...
[dx] Convert a set of classfiles into a dex file, optionally embe
in a
[dx] jar/zip. Output name must end with one of: .dex .jar .zip .a
r be a directory.
[dx] Positions options: none, important, lines.
[dx] --multi-dex: allows to generate several dex files if needed.
s option is
[dx] exclusive with --incremental, causes --num-threads to be ign
and only
[dx] supports folder or archive output.
[dx] --main-dex-list=: is a list of class file names
asses defined by
[dx] those class files are put in classes.dex.
[dx] --minimal-main-dex: only classes selected by --main-dex-list
to be put in
[dx] the main dex.
[dx] dx --annotool --annotation= [--element=]
[dx] [--print=]
[dx] dx --dump [--debug] [--strict] [--bytes] [--optimize]
[dx] [--basic-blocks | --rop-blocks | --ssa-blocks | --dot] [--ssa-
=]
[dx] [--width=] [.class | .txt] ...
[dx] Dump classfiles, or transformations thereof, in a human-orie
format.
[dx] dx --find-usages <file.dex>
[dx] Find references and declarations to a field or method.
[dx] declaring type: a class name in internal form, like Ljava/la
bject;
[dx] member: a field or method name, like hashCode
[dx] dx -J ... <arguments, in one of the above forms>
[dx] Pass VM-specific options to the virtual machine that runs dx
[dx] dx --version
[dx] Print the version of this tool (1.8).
[dx] dx --help
[dx] Print this message.

BUILD FAILED
D:\BuzzBoard_Workspace\BuzzBoardWork\custom_rules.xml:39: null returned: 1

Total time: 2 seconds

Mention .so files in custom rule file

Hi,

I have some .so files to do database encryption in my project.when apply ant task with this custom rule i got an error like

No implementation found for native Lcom.......

java.lang.UnsatisfiedLinkError: Native method not found:

how can solve this issue.how do i specify the .so files in custom rules file.please advice me.

i am helpless..

Thanks in Advance

Run question

i'm run my app, i got the question.please help me, thanks.

06-29 15:00:01.386: E/AndroidRuntime(13692): FATAL EXCEPTION: main
06-29 15:00:01.386: E/AndroidRuntime(13692): Process: com.mit.dstore, PID: 13692
06-29 15:00:01.386: E/AndroidRuntime(13692): java.lang.RuntimeException: Unable to instantiate application com.mit.dstore.engine.MyApplication: java.lang.ClassNotFoundException: Didn't find class "com.mit.dstore.engine.MyApplication" on path: DexPathList[[zip file "/data/app/com.mit.dstore-1/base.apk"],nativeLibraryDirectories=[/data/app/com.mit.dstore-1/lib/arm, /vendor/lib, /system/lib]]
06-29 15:00:01.386: E/AndroidRuntime(13692): at android.app.LoadedApk.makeApplication(LoadedApk.java:584)
06-29 15:00:01.386: E/AndroidRuntime(13692): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4947)
06-29 15:00:01.386: E/AndroidRuntime(13692): at android.app.ActivityThread.access$1600(ActivityThread.java:187)
06-29 15:00:01.386: E/AndroidRuntime(13692): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1584)
06-29 15:00:01.386: E/AndroidRuntime(13692): at android.os.Handler.dispatchMessage(Handler.java:111)
06-29 15:00:01.386: E/AndroidRuntime(13692): at android.os.Looper.loop(Looper.java:210)
06-29 15:00:01.386: E/AndroidRuntime(13692): at android.app.ActivityThread.main(ActivityThread.java:5809)
06-29 15:00:01.386: E/AndroidRuntime(13692): at java.lang.reflect.Method.invoke(Native Method)
06-29 15:00:01.386: E/AndroidRuntime(13692): at java.lang.reflect.Method.invoke(Method.java:372)
06-29 15:00:01.386: E/AndroidRuntime(13692): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1113)
06-29 15:00:01.386: E/AndroidRuntime(13692): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879)
06-29 15:00:01.386: E/AndroidRuntime(13692): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.mit.dstore.engine.MyApplication" on path: DexPathList[[zip file "/data/app/com.mit.dstore-1/base.apk"],nativeLibraryDirectories=[/data/app/com.mit.dstore-1/lib/arm, /vendor/lib, /system/lib]]
06-29 15:00:01.386: E/AndroidRuntime(13692): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
06-29 15:00:01.386: E/AndroidRuntime(13692): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
06-29 15:00:01.386: E/AndroidRuntime(13692): at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
06-29 15:00:01.386: E/AndroidRuntime(13692): at android.app.Instrumentation.newApplication(Instrumentation.java:985)
06-29 15:00:01.386: E/AndroidRuntime(13692): at android.app.LoadedApk.makeApplication(LoadedApk.java:579)
06-29 15:00:01.386: E/AndroidRuntime(13692): ... 10 more
06-29 15:00:01.386: E/AndroidRuntime(13692): Suppressed: java.lang.NoClassDefFoundError: com.mit.dstore.engine.MyApplication
06-29 15:00:01.386: E/AndroidRuntime(13692): at dalvik.system.DexFile.defineClassNative(Native Method)
06-29 15:00:01.386: E/AndroidRuntime(13692): at dalvik.system.DexFile.defineClass(DexFile.java:226)
06-29 15:00:01.386: E/AndroidRuntime(13692): at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
06-29 15:00:01.386: E/AndroidRuntime(13692): at dalvik.system.DexPathList.findClass(DexPathList.java:321)
06-29 15:00:01.386: E/AndroidRuntime(13692): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
06-29 15:00:01.386: E/AndroidRuntime(13692): ... 14 more
06-29 15:00:01.386: E/AndroidRuntime(13692): Suppressed: java.lang.ClassNotFoundException: com.mit.dstore.engine.MyApplication
06-29 15:00:01.386: E/AndroidRuntime(13692): at java.lang.Class.classForName(Native Method)
06-29 15:00:01.386: E/AndroidRuntime(13692): at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
06-29 15:00:01.386: E/AndroidRuntime(13692): at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
06-29 15:00:01.386: E/AndroidRuntime(13692): at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
06-29 15:00:01.386: E/AndroidRuntime(13692): ... 13 more
06-29 15:00:01.386: E/AndroidRuntime(13692): Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

How to sign in release environment ?

I found something about sign in release ENV,but how ? below is the description in your demo(custom_rules.xml):
release: Builds the application. The generated apk file must be
signed before it is published.
The 'nodeps' target can be used to only build the
current project and ignore the libraries using:
'ant nodeps release'

PLS help me: How to use this command signature.(I already have a signature file)

[Error] java.lang.OutOfMemoryError

   [dx] UNEXPECTED TOP-LEVEL ERROR:
   [dx] java.lang.OutOfMemoryError: GC overhead limit exceeded

BUILD FAILED
D:\workspace\ViewTest\custom_rules.xml:26: null returned: 3

Total time: 6 minutes 31 seconds

D:\workspace\ViewTest>

Sometimes the Error also went like: [dx] java.lang.OutOfMemoryError: java heap space

What's the problem about? I'need help..

Runtime annotated classes not found

Hello.

First of all thank you for sharing your code and build script with us, it is very helpful. I did manage to setup my projects and there are no compilation issues. But there is one thing that I cannot get my head around.

I'm using ORMLite for Android and for some reason the DatabaseHelper (extends OrmLiteSqliteOpenHelper) can't be instantiated cause the annotated model classes cannot be found.

I tried running my project using the provided build file without dexing the libraries to find out if the project would run without the dexTool class loading. It doesn't work. So the problem is not with the dexTool but has something to do with annotation of the data model classes and the build script.

I will try to figure out what is causing the annotated classes missing from the apk but I'm not very familiar with the whole Android build script so any help is appreciated. Thanks.

Brgds,
Eduard

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.