- 随着android应用软件开发的不断发展,应用软件不断扩展,相信作为一个android开发者的你遇见过,或者将来会遇见的一个问题:
- Unable to execute dex: method ID not in[0, 0xffff]: 65536)
- 当出现这个错误时说明你本身自己的工程代码中含有的太多的方法,或者你的工程lib文件夹下引用的第三方插件jar包有太多的方法,这两者的方法加起来已经超过了65536这个数目。而谷歌规定单个dex文件中的方法不能超过65536的限制。
- 既然要用到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环境变量就配置好了。
- 注:不会配置的,请移步到:https://www.google.com.hk/ ant配置
- 在你的android工程目录下新建一个xml文件,内容如下,需要注意的地方是表红色的部分
- <?xml version="1.0" encoding="UTF-8"?>
- <project name="custom_rules">
- <dirname property="custom_rules.basedir" file="${ant.file.custom_rules}"/>
- <path id="pathtool.antlibs">
- <pathelement path="${custom_rules.basedir}/pathtool.jar" />
- </path>
- <taskdef resource="anttasks.properties" classpathref="pathtool.antlibs"/>
- <target name="-post-compile">
- <!--
- libs="libs/" means pack all .jar library into the secondary dex.
- if you to pack specific .jar into the secondary dex, change it to
- libs="/android-support-v4.jar,/10k-methods.jar"
- -->
- <pathtool
- <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>
- refid="project.all.jars.path"
- excludeRefid="out.dex.jar.input.ref"
- includeRefid="out.dex.jar.assets" />
- <mkdir dir="${out.absolute.dir}/libs.apk"/>
- <dex executable="${dx}"
- output="${out.absolute.dir}/libs.apk/classes.dex"
- dexedlibs="${out.absolute.dir}/libs.apk"
- nolocals="true"
- forceJumbo="false"
- disableDexMerger="false">
- <path refid="out.dex.jar.assets" />
- </dex>
- <zip destfile="${out.absolute.dir}/libs-unaligned.apk"
- basedir="${out.absolute.dir}/libs.apk"
- includes="classes.dex"
- update="true"/>
- <zipalign
- executable="${zipalign}"
- input="${out.absolute.dir}/libs-unaligned.apk"
- output="${asset.absolute.dir}/libs.apk"
- verbose="${verbose}" />
- </target>
- <target name="-post-package">
- <delete file="${asset.absolute.dir}/libs.apk"/>
- </target>
- <target name="run">
- <xpath input="${manifest.abs.file}" expression="/manifest/@package" output="manifest.package" />
- <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"
- output="manifest.activity"/>
- <echo>component: ${manifest.package}/${manifest.activity}</echo>
- <exec executable="${adb}" failonerror="false">
- <arg line="${adb.device.arg}" />
- <arg value="shell" />
- <arg value="am" />
- <arg value="force-stop" />
- <arg value="${manifest.package}" />
- </exec>
- <exec executable="${adb}" failonerror="true">
- <arg line="${adb.device.arg}" />
- <arg value="shell" />
- <arg value="am" />
- <arg value="start" />
- <arg value="-n" />
- <arg value="${manifest.package}/${manifest.activity}" />
- <arg value="-W" />
- </exec>
- </target>
- <target name="rund">
- <xpath input="${manifest.abs.file}" expression="/manifest/@package" output="manifest.package" />
- <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"
- output="manifest.activity"/>
- <echo>component: ${manifest.package}/${manifest.activity}</echo>
- <echo>Debug package ${mainfest.package}. You should prepare your eclipse.</echo>
- <echo>Keep your project open, and if you get a red bug icon in DDMS, you</echo>
- <echo>should stop and manually debug it once.</echo>
- <exec executable="${adb}" failonerror="false">
- <arg line="${adb.device.arg}" />
- <arg value="shell" />
- <arg value="am" />
- <arg value="force-stop" />
- <arg value="${manifest.package}" />
- </exec>
- <exec executable="${adb}" failonerror="true">
- <arg line="${adb.device.arg}" />
- <arg value="shell" />
- <arg value="am" />
- <arg value="set-debug-app" />
- <arg value="${manifest.package}" />
- </exec>
- <exec executable="${adb}" failonerror="true">
- <arg line="${adb.device.arg}" />
- <arg value="shell" />
- <arg value="am" />
- <arg value="start" />
- <arg value="-n" />
- <arg value="${manifest.package}/${manifest.activity}" />
- <arg value="-W" />
- <arg value="-D" />
- </exec>
- </target>
- <target name="help">
- <!-- displays starts at col 13
- |13 80| -->
- <echo>Android Ant Build. Available targets:</echo>
- <echo> help: Displays this help.</echo>
- <echo> clean: Removes output files created by other targets.</echo>
- <echo> This calls the same target on all dependent projects.</echo>
- <echo> Use 'ant nodeps clean' to only clean the local project</echo>
- <echo> debug: Builds the application and signs it with a debug key.</echo>
- <echo> The 'nodeps' target can be used to only build the</echo>
- <echo> current project and ignore the libraries using:</echo>
- <echo> 'ant nodeps debug'</echo>
- <echo> release: Builds the application. The generated apk file must be</echo>
- <echo> signed before it is published.</echo>
- <echo> The 'nodeps' target can be used to only build the</echo>
- <echo> current project and ignore the libraries using:</echo>
- <echo> 'ant nodeps release'</echo>
- <echo> instrument:Builds an instrumented package and signs it with a</echo>
- <echo> debug key.</echo>
- <echo> test: Runs the tests. Project must be a test project and</echo>
- <echo> must have been built. Typical usage would be:</echo>
- <echo> ant [emma] debug install test</echo>
- <echo> emma: Transiently enables code coverage for subsequent</echo>
- <echo> targets.</echo>
- <echo> install: Installs the newly build package. Must either be used</echo>
- <echo> in conjunction with a build target (debug/release/</echo>
- <echo> instrument) or with the proper suffix indicating</echo>
- <echo> which package to install (see below).</echo>
- <echo> If the application was previously installed, the</echo>
- <echo> application is reinstalled if the signature matches.</echo>
- <echo> installd: Installs (only) the debug package.</echo>
- <echo> installr: Installs (only) the release package.</echo>
- <echo> installi: Installs (only) the instrumented package.</echo>
- <echo> installt: Installs (only) the test and tested packages (unless</echo>
- <echo> nodeps is used as well.</echo>
- <echo> uninstall: Uninstalls the application from a running emulator or</echo>
- <echo> device. Also uninstall tested package if applicable</echo>
- <echo> unless 'nodeps' is used as well.</echo>
- <echo></echo>
- <echo>Custom targets:</echo>
- <echo> run: Run your application.</echo>
- <echo> rund: Run and attach to debugger.</echo>
- <echo></echo>
- <echo>--> Example:</echo>
- <echo>--> ant debug install run</echo>
- <echo>--> ant rund</echo>
- </target>
- </project>
注:我新建的xml文件名为:custom_rules.xml(build.xml中会用到,这个名字是ant默认的,所以使用这个名字)
在build.xml中你会发现有这一段
然后你还需要拷贝一个jar到你的工程目录下,下载地址:http://download.csdn.net/detail/u010231111/9372883
最后当然是代码咯,这个需要在application中调用,自定义一个application类,在applicition类中的oncreate方法下调用如下方法:
编译命令:先进入到你的工程所在目录 cd xxx会吧?
例如:我有一个类,在其中的某一个jar包里面,但是在application中在dexTool方法调用之前需要使用到,我又把这个jar放到这里去加载的话,那么就会报空指针异常,这个需要时刻谨记,某些jar不会立刻用到的才能放到这里来,否则编译通过了就直接报空指针了!!!
如图片所示:DemoHXSDKHelper 需要开始的时候就使用,如果我把这个包含这个类的jar放到custom_rules.xml中后,那么就会报空指针异常了。因为我要使用这个类点时候,这些个jar还没加载,肯定是找不到这个类的。编译的时候是不会有问题的!!!
一定要注意custom_rules.xml中红色的部分!!!