65536 solution for ant build

65536

Android 的 App 如果有包 Google play service ,

非常容易出現以下錯誤,

Unable to execute dex: method ID not in [0, 0xffff]: 65536

官方目前只有提供 Gralde 解法,

Multidex Support Library

但是仍然在使用 Ant build 的專案,

官方並沒有提供使用說明,

其實仍然是可以使用,

只是方法比較麻煩,

寫成教學提供大家參考。

教學

  1. 將 android-support-multidex.jar 加入自己的專案中。(通常在這個目錄下 {sdk.dir}/extras/android/support/multidex/library/libs/android-support-multidex.jar

  2. 繼承MultiDexApplication

    @Override
    protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
    }
    
  3. 將 3rd Party 或不會修改的 Lib 集合成一個 Jar ,到 SDK 目錄下的 build-tools 使用 Dx 來將這個 Jar 輸出成classes2.dexclasses3.dex...等等,注意一定要照順序。

  4. 把這些 Dex 檔案放入 Src 資料夾中。

  5. 把被打包的 Jar 加到依賴裡面,但是選擇不編譯,就像 android.jar 一樣。

  6. 完成,這樣就突破 65536 的限制了。

後話

其實還有更簡單的方法,

去下載獨立的 Google play service 包,

Google play service#split

這裡提供了 Gradle Build 的分離寫法,

Ant Build 也可以使用,

先用 Android Studio 下載你需要的 Library,

例如我需要 GCM ,就在 Gradle 加入這兩行,

compile 'com.google.android.gms:play-services-base:7.5.0'
compile 'com.google.android.gms:play-services-gcm:7.5.0'

注意play-services-base是一定要引用的,

下載完以後,到你專案下的目錄找檔案,

/app/build/intermediates/exploded-aar/com.google.android.gms/play-services

把 play-services-base 當成 Library Project 依賴到 Ant Build 的專案中,

在把其他用到的 Jar 加入即可,

如果你的其他 Play Service Library 有 Resource ,

要注意 Resource Merge 的問題。

如此以來你就完成了自己精簡版的 Play Service 了,

不過最後還是建議早日換成 Gradle Build ,就不需要花費這麼多功夫在上面了。

參考

  1. http://www.lephones.net/2014/11/27/multi-dex-app/

Comments

comments powered by Disqus