Unable to execute dex: method ID not in[0, 0xffff]: 65536 在eclipse最簡單的解法
如果可以用android studio此問題可更簡單的解決, 但非要用eclipse時, 就真的很麻煩, 網路找到最簡單的解法, 就是套用別人寫好的方案. 說是簡單, 但其實還是遇到很多挫折. 所以還是建議用android studio比較簡單.
當我們的程式碼過大時,編譯時會報Unable to execute dex: method ID not in[0, 0xffff]: 65536)錯誤。當出現這個錯誤時說明你程式碼含有的太多的方法,或者你引用的第三方jar包有太多的方法,這兩者的方法加起來已經超過了65536這個數目。
當我們的程式碼過大時,編譯時會報Unable to execute dex: method ID not in[0, 0xffff]: 65536)錯誤。當出現這個錯誤時說明你程式碼含有的太多的方法,或者你引用的第三方jar包有太多的方法,這兩者的方法加起來已經超過了65536這個數目。
以下基本參考下列文章, 加上自己實作的補充:
http://blog.csdn.net/stevenhu_223/article/details/41277827
ant操作參考:
http://www.cnblogs.com/yaozhongxiao/p/3523061.html
ant操作參考:
http://www.cnblogs.com/yaozhongxiao/p/3523061.html
到此頁面下載, 並按其說明實作
Step1: 複製以下兩個檔案到android project的根目錄下
Dex65536/custom_rules.xml
Dex65536/pathtool.jar
Copy these two files in your android project, and execute the following command to generate build.xml.
到如下adt的目錄
cd D:\adt-bundle-windows-x86-20140702\sdk\tools在命令列, 執行下列
android update project --target 2 -p "D:\My Documents\AndroidApp\docSearch"命令
因為還有用到google-play-services_lib(在另一個單獨的project), 所以還要加上這個
android update project -p "D:\My Documents\AndroidApp\google-play-services_lib"
cd D:\adt-bundle-windows-x86-20140702\sdk\tools在命令列, 執行下列
android update project --target 2 -p "D:\My Documents\AndroidApp\docSearch"命令
因為還有用到google-play-services_lib(在另一個單獨的project), 所以還要加上這個
android update project -p "D:\My Documents\AndroidApp\google-play-services_lib"
(以上 -p 後接專案根路徑, --target 2 , 其中的的2是在D:\adt-bundle-windows-x86-20140702\sdk\tools執行 android list targets, 得到的值.(id: 2 or "android-22")
http://developer.android.com/tools/projects/projects-cmdline.html#UpdatingAProject)
http://developer.android.com/tools/projects/projects-cmdline.html#UpdatingAProject)
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();
// copy dexTool() method into your own custom Application and call it after super.onCreate().
dexTool();
}
/**
* Copy the following code and call dexTool() after super.onCreate() in
* Application.onCreate()
*
* 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);
}
}
}
Step3: ant build and run
Make sure you have ant installed.
cd /YourProject
ant clean debug install run
ant debug install rund (不clean且進入debug 偵錯)
ant run (直接run, 不compile)
其他注意事項:
1.eclipse 中 Run as-> Ant build 未出現的解法
in eclipse :1. Help -> Install New SoftWare
After doing this...eclipse restart, then show option of Ant Build in Run as
|
2.使用ant遇到invalid resource directory name: res crunch
2.將bin/res/crunch刪除
這是因為ant無法處理crunch資料夾
1.先把Eclipse中[Project]-->[Build Automatically]關掉(否則crunch資料夾會一直被自動生成)2.將bin/res/crunch刪除