解決Android單個dex檔案不能超過65536個方法問題

銳湃發表於2015-09-23

當我們的專案程式碼過大時,編譯執行時會報Unable to execute dex: method ID not in[0, 0xffff]: 65536)錯誤。當出現這個錯誤時說明你本身自己的工程程式碼中含有的太多的方法,或者你的工程lib資料夾下引用的第三方外掛jar包有太多的方法,這兩者的方法加起來已經超過了65536這個數目。而谷歌規定單個dex檔案中的方法不能超過65536的限制。

  那麼這個時候,我們就需要分包處理解決。一般情況下的解決方案就是把整個專案工程包括jar,區分開來分解成兩個dex檔案。

  網上很多這些解決方案,有的把專案程式碼中比較獨立的模組打包成jar檔案,然後利用dx工具將打包的jar檔案轉成dex檔案的jar,然後將其放到SD卡中去動態載入。這種方案是不符合我們的需求的。

  那麼問題來了,該如何更好的去拆分Dex檔案,繞過谷歌規定的65536呢?其實,網上已經有些牛人幫我們提出了很多方案了,尤其是在github上。特別是mmin18提出的方案,githut地址如下:

  https://github.com/mmin18/Dex65536

該解決方案的原理差不多是這樣:

  1.在工程目錄下建立custom_rules.xml檔案,修改編譯策略。將工程lib的檔案中含有的第三方外掛jar包全部打包成libs.apk,然後將其作為編譯執行時的第二個dex檔案。

  2.最後通過ant命令執行操作,執行整個工程或簽名加密打包整個工程。

   怎麼樣,通過上面的介紹是不是覺得很簡單,其實不然,如果要真正的去了解整個原理,還是很有難度,首先你得對custom_rules.xml檔案的相關配置和android工程的編譯策略非常熟悉。不過,這裡我們不用管它,既然牛人已經幫我們寫好了,那我們只要知道怎麼去用到我們的專案中就行了。

   接下來就是怎麼去用到我們的專案程式碼中了(當然,感興趣的同志可以去研究研究它的實現原理)。

custom_rules.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
            libs="libs/"
            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>


一.配置和執行工程步驟如下:

  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環境變數就配置好了。

  2. 接下來就是拷貝檔案custom_rules.xml和pathtool.jar到我們專案的根目錄下。

  3.  然後就在我們的專案執行之前新增程式碼執行去載入第二個dex檔案,下面的dexTool方法就是執行載入第二個dex檔案的功能程式碼,直接copy到我們的自定義application類中就行了,程式碼如下:

@SuppressLint("NewApi")
  privatevoid dexTool() {
         FiledexDir = new File(getFilesDir(), "dlibs");
         dexDir.mkdir();
         FiledexFile = new File(dexDir, "libs.apk");
         FiledexOpt = getCacheDir();
         try{
                InputStreamins = getAssets().open("libs.apk");
                if(dexFile.length() != ins.available()) {
                       FileOutputStreamfos = new FileOutputStream(dexFile);
                       byte[]buf = new byte[4096];
                       intl;
                       while((l = ins.read(buf)) != -1) {
                              fos.write(buf,0, l);
                       }
                       fos.close();
                }
                ins.close();
         }catch (Exception e) {
                thrownew RuntimeException(e);
         }
 
         ClassLoadercl = getClassLoader();
         ApplicationInfoai = getApplicationInfo();
         StringnativeLibraryDir = null;
         if(Build.VERSION.SDK_INT > 8) {
                nativeLibraryDir= ai.nativeLibraryDir;
         }else {
                nativeLibraryDir= "/data/data/" + ai.packageName + "/lib/";
         }
         DexClassLoaderdcl = new DexClassLoader(dexFile.getAbsolutePath(),
                       dexOpt.getAbsolutePath(),nativeLibraryDir, cl.getParent());
 
         try{
                Fieldf = ClassLoader.class.getDeclaredField("parent");
                f.setAccessible(true);
                f.set(cl,dcl);
         }catch (Exception e) {
                thrownew RuntimeException(e);
         }
  }

接著在自定義application類的onCreate方法中呼叫dexTool。

  4. 自動生成build.xml檔案。開啟命令視窗,進入到工程的根目錄下,輸入如下命令android update project -p .  在輸入該命令之前,要確保你配置的sdk/tools目錄和sdk/tools/lib資料夾中有android.bat和find_java.bat檔案。

  5. 然後就是執行該工程了。輸入命令ant clean debug install run,在輸入該命令之前要確保你的ant環境配置沒有問題。

二.簽名混淆程式碼:

上面的執行apk並沒有通過程式碼混淆和簽名,一般情況下我們需要生成一個經過程式碼混淆和簽名的apk,那麼ant環境下怎麼去配置才能生成程式碼混淆和簽名的apk呢。接下來將進行說明。

     1. 在剛剛已經配置好的工程根目錄下建立ant.properties檔案,該檔案在建立工程時是不會自動生成的,需要我們自己去建立。這個檔案會在build.xml檔案中宣告。

     2. 然後在建立好的ant.properties中新增相關資訊,比如我新增的資訊如下  

   

         第一行內容為配置關聯相關的加密資訊檔案(也可能為proguard.config = proguard.cfg)

        第二行內容為指定簽名檔案所在路徑,./keystore.eking,說明該簽名檔案在工程根目錄下(拷貝簽名檔案到工程根目錄)

        第三行內容為簽名檔案的alias值為eking

        第四、第五行分別為簽名檔案對應的store、alias密碼。 

      3.接著在工程目錄下執行如下命令antrelease, 執行完後會自動在工程的bin目錄下生成appname-release.apk檔案,這個就是簽名後生成的apk。


   Demo下載地址:http://download.csdn.net/detail/stevenhu_223/8184135

   注:該Demo已經通過驗證。其中lib/60k-methods.jar有60k個方法,Demo工程中也有60k個方法,但是該Demo可以順利執行,說明該方案是可行的。

   關於拆分dex檔案解決dex 65536有效解決方案的介紹就到此結束。有興趣的同志還可以去深入研究它的實現原理。

轉自:http://blog.csdn.net/stevenhu_223/article/details/41277827

相關文章