android解决方法数超过65536问题

来源:互联网 发布:设置maven的java home 编辑:程序博客网 时间:2024/06/07 00:22
  1. 随着android应用软件开发的不断发展,应用软件不断扩展,相信作为一个android开发者的你遇见过,或者将来会遇见的一个问题:  
[html] view plain copy
  1. Unable to execute dex: method ID not in[0, 0xffff]: 65536)  
[html] view plain copy
  1. 当出现这个错误时说明你本身自己的工程代码中含有的太多的方法,或者你的工程lib文件夹下引用的第三方插件jar包有太多的方法,这两者的方法加起来已经超过了65536这个数目。而谷歌规定单个dex文件中的方法不能超过65536的限制。  
[html] view plain copy
  1. 既然要用到ant,首先就要先下载ant和配置ant环境,下载链接地址为:http://ant.apache.org/bindownload.cgi下载好apache-ant-1.9.4-bin.zip包后,解压到指定目录。然后配置环境变量,创建变量名为ANT_HOME,值为ant文件对应的路径,比如我的是ANT_HOME = E:\apache-ant-1.9.4-bin\apache-ant-1.9.4。然后在Path变量的值中追加%ANT_HOME%/bin;%ANT_HOME%/lib。这样ant环境变量就配置好了。  
[html] view plain copy
  1. 注:不会配置的,请移步到:https://www.google.com.hk/   ant配置  
[html] view plain copy
  1. 在你的android工程目录下新建一个xml文件,内容如下,需要注意的地方是表红色的部分  


[html] view plain copy
  1.   
[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project name="custom_rules">  
  3.     <dirname property="custom_rules.basedir" file="${ant.file.custom_rules}"/>  
  4.     <path id="pathtool.antlibs">  
  5.         <pathelement path="${custom_rules.basedir}/pathtool.jar" />  
  6.     </path>  
  7.     <taskdef resource="anttasks.properties" classpathref="pathtool.antlibs"/>  
  8.   
  9.     <target name="-post-compile">  
  10.         <!--  
  11.         libs="libs/" means pack all .jar library into the secondary dex.  
  12.         if you to pack specific .jar into the secondary dex, change it to  
  13.         libs="/android-support-v4.jar,/10k-methods.jar"  
  14.         -->  
  15.         <pathtool  
  16.             <span style="color:#ff0000;">libs="/activeandroid.jar,/Android_Location_V1.3.2.jar,/android-support-v4.jar,/android-support-v7-recyclerview.jar,/com.umeng.message.lib_v2.2.0.jar,/google-play-services.jar,/httpmime-4.1.3.jar,/SocialSDK_QQZone_1.jar,/SocialSDK_QQZone_2.jar,/SocialSDK_QQZone_3.jar,/SocialSDK_Sina.jar,/SocialSDK_WeiXin_1.jar,/SocialSDK_WeiXin_2.jar,/umeng_community_push.jar,/umeng_community_sdk_core.jar,/umeng_community_sdk_login.jar,/umeng_community_sdk_ui.jar,/umeng_community_share.jar,/umeng_social_sdk.jar,/universal-image-loader-1.9.4.jar"</span>  
  17.             refid="project.all.jars.path"  
  18.             excludeRefid="out.dex.jar.input.ref"  
  19.             includeRefid="out.dex.jar.assets" />  
  20.         <mkdir dir="${out.absolute.dir}/libs.apk"/>  
  21.         <dex executable="${dx}"  
  22.                 output="${out.absolute.dir}/libs.apk/classes.dex"  
  23.                 dexedlibs="${out.absolute.dir}/libs.apk"  
  24.                 nolocals="true"  
  25.                 forceJumbo="false"  
  26.                 disableDexMerger="false">  
  27.             <path refid="out.dex.jar.assets" />  
  28.         </dex>  
  29.         <zip destfile="${out.absolute.dir}/libs-unaligned.apk"  
  30.             basedir="${out.absolute.dir}/libs.apk"  
  31.             includes="classes.dex"  
  32.             update="true"/>  
  33.         <zipalign  
  34.             executable="${zipalign}"  
  35.             input="${out.absolute.dir}/libs-unaligned.apk"  
  36.             output="${asset.absolute.dir}/libs.apk"  
  37.             verbose="${verbose}" />  
  38.     </target>  
  39.   
  40.     <target name="-post-package">  
  41.         <delete file="${asset.absolute.dir}/libs.apk"/>  
  42.     </target>  
  43.   
  44.     <target name="run">  
  45.         <xpath input="${manifest.abs.file}" expression="/manifest/@package" output="manifest.package" />  
  46.         <xpath input="${manifest.abs.file}" expression="/manifest/application/activity[intent-filter/action/@android:name="android.intent.action.MAIN" and intent-filter/category/@android:name="android.intent.category.LAUNCHER"]/@android:name"  
  47.             output="manifest.activity"/>  
  48.         <echo>component: ${manifest.package}/${manifest.activity}</echo>  
  49.         <exec executable="${adb}" failonerror="false">  
  50.             <arg line="${adb.device.arg}" />  
  51.             <arg value="shell" />  
  52.             <arg value="am" />  
  53.             <arg value="force-stop" />  
  54.             <arg value="${manifest.package}" />  
  55.         </exec>  
  56.         <exec executable="${adb}" failonerror="true">  
  57.             <arg line="${adb.device.arg}" />  
  58.             <arg value="shell" />  
  59.             <arg value="am" />  
  60.             <arg value="start" />  
  61.             <arg value="-n" />  
  62.             <arg value="${manifest.package}/${manifest.activity}" />  
  63.             <arg value="-W" />  
  64.         </exec>  
  65.     </target>  
  66.     <target name="rund">  
  67.         <xpath input="${manifest.abs.file}" expression="/manifest/@package" output="manifest.package" />  
  68.         <xpath input="${manifest.abs.file}" expression="/manifest/application/activity[intent-filter/action/@android:name="android.intent.action.MAIN" and intent-filter/category/@android:name="android.intent.category.LAUNCHER"]/@android:name"  
  69.             output="manifest.activity"/>  
  70.         <echo>component: ${manifest.package}/${manifest.activity}</echo>  
  71.         <echo>Debug package ${mainfest.package}. You should prepare your eclipse.</echo>  
  72.         <echo>Keep your project open, and if you get a red bug icon in DDMS, you</echo>  
  73.         <echo>should stop and manually debug it once.</echo>  
  74.         <exec executable="${adb}" failonerror="false">  
  75.             <arg line="${adb.device.arg}" />  
  76.             <arg value="shell" />  
  77.             <arg value="am" />  
  78.             <arg value="force-stop" />  
  79.             <arg value="${manifest.package}" />  
  80.         </exec>  
  81.         <exec executable="${adb}" failonerror="true">  
  82.             <arg line="${adb.device.arg}" />  
  83.             <arg value="shell" />  
  84.             <arg value="am" />  
  85.             <arg value="set-debug-app" />  
  86.             <arg value="${manifest.package}" />  
  87.         </exec>  
  88.         <exec executable="${adb}" failonerror="true">  
  89.             <arg line="${adb.device.arg}" />  
  90.             <arg value="shell" />  
  91.             <arg value="am" />  
  92.             <arg value="start" />  
  93.             <arg value="-n" />  
  94.             <arg value="${manifest.package}/${manifest.activity}" />  
  95.             <arg value="-W" />  
  96.             <arg value="-D" />  
  97.         </exec>  
  98.     </target>  
  99.   
  100.     <target name="help">  
  101.         <!-- displays starts at col 13  
  102.               |13                                                              80| -->  
  103.         <echo>Android Ant Build. Available targets:</echo>  
  104.         <echo>   help:      Displays this help.</echo>  
  105.         <echo>   clean:     Removes output files created by other targets.</echo>  
  106.         <echo>              This calls the same target on all dependent projects.</echo>  
  107.         <echo>              Use 'ant nodeps clean' to only clean the local project</echo>  
  108.         <echo>   debug:     Builds the application and signs it with a debug key.</echo>  
  109.         <echo>              The 'nodeps' target can be used to only build the</echo>  
  110.         <echo>              current project and ignore the libraries using:</echo>  
  111.         <echo>              'ant nodeps debug'</echo>  
  112.         <echo>   release:   Builds the application. The generated apk file must be</echo>  
  113.         <echo>              signed before it is published.</echo>  
  114.         <echo>              The 'nodeps' target can be used to only build the</echo>  
  115.         <echo>              current project and ignore the libraries using:</echo>  
  116.         <echo>              'ant nodeps release'</echo>  
  117.         <echo>   instrument:Builds an instrumented package and signs it with a</echo>  
  118.         <echo>              debug key.</echo>  
  119.         <echo>   test:      Runs the tests. Project must be a test project and</echo>  
  120.         <echo>              must have been built. Typical usage would be:</echo>  
  121.         <echo>                  ant [emma] debug install test</echo>  
  122.         <echo>   emma:      Transiently enables code coverage for subsequent</echo>  
  123.         <echo>              targets.</echo>  
  124.         <echo>   install:   Installs the newly build package. Must either be used</echo>  
  125.         <echo>              in conjunction with a build target (debug/release/</echo>  
  126.         <echo>              instrument) or with the proper suffix indicating</echo>  
  127.         <echo>              which package to install (see below).</echo>  
  128.         <echo>              If the application was previously installed, the</echo>  
  129.         <echo>              application is reinstalled if the signature matches.</echo>  
  130.         <echo>   installd:  Installs (only) the debug package.</echo>  
  131.         <echo>   installr:  Installs (only) the release package.</echo>  
  132.         <echo>   installi:  Installs (only) the instrumented package.</echo>  
  133.         <echo>   installt:  Installs (only) the test and tested packages (unless</echo>  
  134.         <echo>              nodeps is used as well.</echo>  
  135.         <echo>   uninstall: Uninstalls the application from a running emulator or</echo>  
  136.         <echo>              device. Also uninstall tested package if applicable</echo>  
  137.         <echo>              unless 'nodeps' is used as well.</echo>  
  138.         <echo></echo>  
  139.         <echo>Custom targets:</echo>  
  140.         <echo>   run:       Run your application.</echo>  
  141.         <echo>   rund:      Run and attach to debugger.</echo>  
  142.         <echo></echo>  
  143.         <echo>--> Example:</echo>  
  144.         <echo>-->    ant debug install run</echo>  
  145.         <echo>-->    ant rund</echo>  
  146.     </target>  
  147.   
  148. </project>  
红色部分代码需要你自己配置,你要加载的某一部分的jar,这里需要注意,这里面的jar是application类加载的时候去加载这些jar,格式:/***,/***  里面用到的某些类必须等加载了这一部分的jar后才可以使用,否则会报错。具体举例请看最后的举例。

注:我新建的xml文件名为:custom_rules.xml(build.xml中会用到,这个名字是ant默认的,所以使用这个名字)

在build.xml中你会发现有这一段


然后你还需要拷贝一个jar到你的工程目录下,下载地址:http://download.csdn.net/detail/u010231111/9372883

最后当然是代码咯,这个需要在application中调用,自定义一个application类,在applicition类中的oncreate方法下调用如下方法:
[java] view plain copy
  1. private void dexTool() {  
  2.   
  3.         File dexDir = new File(getFilesDir(), "dlibs");  
  4.         dexDir.mkdir();  
  5.         File dexFile = new File(dexDir, "libs.apk");  
  6.         File dexOpt = new File(dexDir, "opt");  
  7.         dexOpt.mkdir();  
  8.         try {  
  9.             InputStream ins = getAssets().open("libs.apk");  
  10.             if (dexFile.length() != ins.available()) {  
  11.                 FileOutputStream fos = new FileOutputStream(dexFile);  
  12.                 byte[] buf = new byte[4096];  
  13.                 int l;  
  14.                 while ((l = ins.read(buf)) != -1) {  
  15.                     fos.write(buf, 0, l);  
  16.                 }  
  17.                 fos.close();  
  18.             }  
  19.             ins.close();  
  20.         } catch (Exception e) {  
  21.             throw new RuntimeException(e);  
  22.         }  
  23.   
  24.         ClassLoader cl = getClassLoader();  
  25.         ApplicationInfo ai = getApplicationInfo();  
  26.         String nativeLibraryDir = null;  
  27.         if (Build.VERSION.SDK_INT > 8) {  
  28.             nativeLibraryDir = ai.nativeLibraryDir;  
  29.         } else {  
  30.             nativeLibraryDir = "/data/data/" + ai.packageName + "/lib/";  
  31.         }  
  32.         DexClassLoader dcl = new DexClassLoader(dexFile.getAbsolutePath(),  
  33.                 dexOpt.getAbsolutePath(), nativeLibraryDir, cl.getParent());  
  34.   
  35.         try {  
  36.             Field f = ClassLoader.class.getDeclaredField("parent");  
  37.             f.setAccessible(true);  
  38.             f.set(cl, dcl);  
  39.         } catch (Exception e) {  
  40.             throw new RuntimeException(e);  
  41.         }  
  42.     }  
到这里,算是完事了,最后只需要ant编译即可

编译命令:先进入到你的工程所在目录  cd xxx会吧?
[plain] view plain copy
  1. android update project -p 工程名  
  2. ant clean debug install run  

最后解释一下上面的custom_rules.xml文件红色部分
例如:我有一个类,在其中的某一个jar包里面,但是在application中在dexTool方法调用之前需要使用到,我又把这个jar放到这里去加载的话,那么就会报空指针异常,这个需要时刻谨记,某些jar不会立刻用到的才能放到这里来,否则编译通过了就直接报空指针了!!!



如图片所示:DemoHXSDKHelper 需要开始的时候就使用,如果我把这个包含这个类的jar放到custom_rules.xml中后,那么就会报空指针异常了。因为我要使用这个类点时候,这些个jar还没加载,肯定是找不到这个类的。编译的时候是不会有问题的!!!

一定要注意custom_rules.xml中红色的部分!!!

0 0