App Bundle 的bundletools

JLin
16 min readJul 24, 2019

--

緣由

因為8/1Google要強制升級到 64bits的支援,所以會造成原本的APP so檔案變大一倍

譬如

原本 dex 2Mb , lib 4Mb,如果變成32bits/64bits,就會變成 4Mb+4Mb

因此就會有要用App Bundle的需求

而產生出來的是一個aab的檔案,簡單來說也是一個zip檔

也可以用Android Studio 來分析

裡面其實跟原本apk差不多,但多了一些定義,*.pb的檔案,而aab無法直接使用

  1. 可以拿來直接上傳到goolge play
  2. 可以透過bundletools 指令產生成 apks 後根據spec device安裝到手機內 (根據ABI)

根據官網

首先可以到github下載

bundletool-all-0.10.2.jar

如果是Windows用戶,記得因為他下載下來是一個jar檔,所以必須在前面加上

java -jar bundletool-all-0.10.2.jar …

跟官網的描述可能會不太一樣,

如何簽名 Bundle

在要把bundle產生成 apks的時候,如果要release必須要簽名

bundletool build-apks --bundle=/MyApp/my_app.aab --output=/MyApp/my_app.apks
--ks=/MyApp/keystore.jks
--ks-pass=file:/MyApp/keystore.pwd
--ks-key-alias=MyKeyAlias
--key-pass=file:/MyApp/key.pwd

如果你是要吃一個pwd檔案,就必須用file: ,如果你直接要打密碼,就輸入pass:

否則會看到這個錯誤

[BT:0.10.2] Error: Passwords must be prefixed with “pass:” or “file:”.

這樣就可以直接將apks裝進你的裝置裡面,

產生Connected-Device Spec Apks

如果你是想產生一個根據你現在adb連接裝置spec的apks怎麼做?

bundletool build-apks --connected-device
--bundle=/MyApp/my_app.aab --output=/MyApp/my_app.apks

會產生一個apks,打開壓縮檔裡面會發現

splits可以看到

然後打開base-master.apk可以看到裡面就只有程式碼跟其他設定資源等等

打開base-arm64_v8a.apk可以看到

就是只包含lib的所需要的ABI檔案

什麼是PB(protocol-buffers)

什麼是APKS ?

// TODO

根據Connected-Device產生Json Files

如果你想要根據你連接的裝置,產生相對應的設定檔,譬如ABI是多少,Density是多少,然後產生出的檔案可以在日後進行套用,不用接著裝置的話該怎麼辦?

在Windows實際指令為

(bundletool-all-0.10.2.jar看你下載的jar版本而定)

java -jar bundletool-all-0.10.2.jar get-device-spec — output=spec_s9.json

注意output如果是想要產生在同一層資料夾,記得不要加上路徑,否則你會不知道產生在哪

可以看到S8產生出來的會是這樣

{
“supportedAbis”: [“arm64-v8a”, “armeabi-v7a”, “armeabi”],
“supportedLocales”: [“zh-TW”, “en-US”, “ja-JP”],
“deviceFeatures”: [“reqGlEsVersion=0x30002”, “android.hardware.audio.output”, “android.hardware.bluetooth”, “android.hardware.bluetooth_le”, “android.hardware.camera”, “android.hardware.camera.any”, “android.hardware.camera.autofocus”, “android.hardware.camera.capability.manual_post_processing”, “android.hardware.camera.capability.manual_sensor”, “android.hardware.camera.capability.raw”, “android.hardware.camera.flash”, “android.hardware.camera.front”, “android.hardware.camera.level.full”, “android.hardware.faketouch”, “android.hardware.fingerprint”, “android.hardware.location”, “android.hardware.location.gps”, “android.hardware.location.network”, “android.hardware.microphone”, “android.hardware.nfc”, “android.hardware.nfc.any”, “android.hardware.nfc.hce”, “android.hardware.nfc.hcef”, “android.hardware.opengles.aep”, “android.hardware.ram.normal”, “android.hardware.screen.landscape”, “android.hardware.screen.portrait”, “android.hardware.sensor.accelerometer”, “android.hardware.sensor.barometer”, “android.hardware.sensor.compass”, “android.hardware.sensor.gyroscope”, “android.hardware.sensor.heartrate”, “android.hardware.sensor.hifi_sensors”, “android.hardware.sensor.light”, “android.hardware.sensor.proximity”, “android.hardware.sensor.stepcounter”, “android.hardware.sensor.stepdetector”, “android.hardware.telephony”, “android.hardware.telephony.gsm”, “android.hardware.touchscreen”, “android.hardware.touchscreen.multitouch”, “android.hardware.touchscreen.multitouch.distinct”, “android.hardware.touchscreen.multitouch.jazzhand”, “android.hardware.usb.accessory”, “android.hardware.usb.host”, “android.hardware.vr.high_performance”, “android.hardware.vulkan.compute”, “android.hardware.vulkan.level=1”, “android.hardware.vulkan.version=4194307”, “android.hardware.wifi”, “android.hardware.wifi.direct”, “android.software.activities_on_secondary_displays”, “android.software.app_widgets”, “android.software.autofill”, “android.software.backup”, “android.software.cant_save_state”, “android.software.companion_device_setup”, “android.software.connectionservice”, “android.software.cts”, “android.software.device_admin”, “android.software.freeform_window_management”, “android.software.home_screen”, “android.software.input_methods”, “android.software.live_wallpaper”, “android.software.managed_users”, “android.software.midi”, “android.software.picture_in_picture”, “android.software.print”, “android.software.sip”, “android.software.sip.voip”, “android.software.verified_boot”, “android.software.voice_recognizers”, “android.software.vr.mode”, “android.software.webview”, “com.google.android.feature.TURBO_PRELOAD”, “com.nxp.mifare”, “com.samsung.android.api.version.2402”, “com.samsung.android.api.version.2403”, “com.samsung.android.api.version.2501”, “com.samsung.android.api.version.2502”, “com.samsung.android.api.version.2601”, “com.samsung.android.api.version.2701”, “com.samsung.android.api.version.2801”, “com.samsung.android.authfw”, “com.samsung.android.bio.face”, “com.samsung.android.camera.iris”, “com.samsung.android.knox.knoxsdk”, “com.samsung.android.sdk.camera.processor”, “com.samsung.android.sdk.camera.processor.dof”, “com.samsung.android.sdk.camera.processor.effect”, “com.samsung.android.sdk.camera.processor.gif”, “com.samsung.android.sdk.camera.processor.haze”, “com.samsung.android.sdk.camera.processor.hdr”, “com.samsung.android.sdk.camera.processor.lls”, “com.samsung.feature.aodservice_v06”, “com.samsung.feature.device_category_phone”, “com.samsung.feature.device_category_phone_high_end”, “com.samsung.feature.galaxyfinder_v7”, “com.samsung.feature.hmt”, “com.samsung.feature.ipsgeofence=1”, “com.samsung.feature.mirrorlink_fw=8”, “com.samsung.feature.samsung_experience_mobile”, “com.samsung.feature.samsungpositioning”, “com.sec.android.mdm”, “com.sec.android.secimaging”, “com.sec.android.secimaging.faceAR”, “com.sec.android.smartface.smart_stay”, “com.sec.feature.barcode_emulator”, “com.sec.feature.cocktailpanel”, “com.sec.feature.cover”, “com.sec.feature.cover.clearcover”, “com.sec.feature.cover.flip”, “com.sec.feature.cover.nfcledcover”, “com.sec.feature.cover.sview”, “com.sec.feature.edge_v03”, “com.sec.feature.findo”, “com.sec.feature.fingerprint_manager_service”, “com.sec.feature.motionrecognition_service”, “com.sec.feature.nfc_authentication”, “com.sec.feature.nfc_authentication_cover”, “com.sec.feature.nsflp=351”, “com.sec.feature.overlaymagnifier”, “com.sec.feature.people_edge_notification”, “com.sec.feature.saccessorymanager”, “com.sec.feature.sensorhub=25”, “com.sec.feature.slocation=3”, “com.sec.feature.spo2”, “com.sec.feature.support_mst”, “com.sec.feature.usb_authentication”],
“screenDensity”: 480,
“sdkVersion”: 28
}

想根據裝置取得測量的apk大小

bundletool get-size total --apks=/MyApp/my_app.apks

會得到這樣的訊息

MIN,MAX
45703894,47464756

也就是你的apk會產生出來的大小

--

--

JLin
JLin

Written by JLin

台中 / JAVA / Android /Kotlin / Kotlin Native 對於Kotlin衍生的JVM等技術 Compose for web / desktop / Ktor Server,生成式AI (Gemini/OpenAI)各式應用, 都有小興趣

No responses yet