android解决方法数超过65536问题

随着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文件,内容如下,需要注意的地方是表红色的部分





    
    
        
    
    

    
        
        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"
            refid="project.all.jars.path"
            excludeRefid="out.dex.jar.input.ref"
            includeRefid="out.dex.jar.assets" />
        
        
            
        
        
        
    

    
        
    

    
        
        
        component: ${manifest.package}/${manifest.activity}
        
            
            
            
            
            
        
        
            
            
            
            
            
            
            
        
    
    
        
        
        component: ${manifest.package}/${manifest.activity}
        Debug package ${mainfest.package}. You should prepare your eclipse.
        Keep your project open, and if you get a red bug icon in DDMS, you
        should stop and manually debug it once.
        
            
            
            
            
            
        
        
            
            
            
            
            
        
        
            
            
            
            
            
            
            
            
        
    

    
        
        Android Ant Build. Available targets:
           help:      Displays this help.
           clean:     Removes output files created by other targets.
                      This calls the same target on all dependent projects.
                      Use 'ant nodeps clean' to only clean the local project
           debug:     Builds the application and signs it with a debug key.
                      The 'nodeps' target can be used to only build the
                      current project and ignore the libraries using:
                      'ant nodeps debug'
           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'
           instrument:Builds an instrumented package and signs it with a
                      debug key.
           test:      Runs the tests. Project must be a test project and
                      must have been built. Typical usage would be:
                          ant [emma] debug install test
           emma:      Transiently enables code coverage for subsequent
                      targets.
           install:   Installs the newly build package. Must either be used
                      in conjunction with a build target (debug/release/
                      instrument) or with the proper suffix indicating
                      which package to install (see below).
                      If the application was previously installed, the
                      application is reinstalled if the signature matches.
           installd:  Installs (only) the debug package.
           installr:  Installs (only) the release package.
           installi:  Installs (only) the instrumented package.
           installt:  Installs (only) the test and tested packages (unless
                      nodeps is used as well.
           uninstall: Uninstalls the application from a running emulator or
                      device. Also uninstall tested package if applicable
                      unless 'nodeps' is used as well.
        
        Custom targets:
           run:       Run your application.
           rund:      Run and attach to debugger.
        
        --> Example:
        -->    ant debug install run
        -->    ant rund
    


红色部分代码需要你自己配置,你要加载的某一部分的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方法下调用如下方法:
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);
        }
    }
到这里,算是完事了,最后只需要ant编译即可

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

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



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

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

你可能感兴趣的:(Android,jar,插件,扩展,65536)