5
5

More than 3 years have passed since last update.

androidのアプリをビルドすると、apkサイズが肥大化する

Last updated at Posted at 2020-01-07

androidのアプリをビルドすると、apkサイズが肥大化する

gitlab ciで、androidアプリをビルドしていたのですが、いつからか「apkサイズが大きくなった気がする」と報告を受けたので、確認すると以前は出ていなかったワーニングが出ていることに気が付いた

android gradle プラグインバージョン:3.5.3
gradle バージョン:5.4.1

コマンドラインでのビルドで出る様になったワーニング

> Task :app:stripProdReleaseDebugSymbols
Compatible side by side NDK version was not found.
Unable to strip library '/builds/jad/jadcenterapp/app/build/intermediates/merged_native_libs/prodRelease/out/lib/armeabi-v7a/libconscrypt_jni.so' due to missing strip tool for ABI 'ARMEABI_V7A'. Packaging it as is.
Unable to strip library '/builds/jad/jadcenterapp/app/build/intermediates/merged_native_libs/prodRelease/out/lib/arm64-v8a/libconscrypt_jni.so' due to missing strip tool for ABI 'ARM64_V8A'. Packaging it as is.
Unable to strip library '/builds/jad/jadcenterapp/app/build/intermediates/merged_native_libs/prodRelease/out/lib/x86_64/libconscrypt_jni.so' due to missing strip tool for ABI 'X86_64'. Packaging it as is.
Unable to strip library '/builds/jad/jadcenterapp/app/build/intermediates/merged_native_libs/prodRelease/out/lib/x86/libconscrypt_jni.so' due to missing strip tool for ABI 'X86'. Packaging it as is.

gitlab ci で、androidアプリをビルドする(ワーニング出る状態)の定義

gitlab-ci.yml
image: openjdk:8-jdk

variables:
  ANDROID_COMPILE_SDK: "28"
  ANDROID_BUILD_TOOLS: "28.0.3"
  ANDROID_SDK_TOOLS:   "4333796"
  ARTIFACTS_NAME: "hoge_center_$CI_PIPELINE_ID"
  ARTIFACTS_DIR: "app/build/outputs"

before_script:
  - apt-get --quiet update --yes
  - apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1
  - wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/sdk-tools-linux-${ANDROID_SDK_TOOLS}.zip
  - unzip -d android-sdk-linux android-sdk.zip
  - echo y | android-sdk-linux/tools/bin/sdkmanager "platforms;android-${ANDROID_COMPILE_SDK}" >/dev/null
  - echo y | android-sdk-linux/tools/bin/sdkmanager "platform-tools" >/dev/null
  - echo y | android-sdk-linux/tools/bin/sdkmanager "build-tools;${ANDROID_BUILD_TOOLS}" >/dev/null
  - export ANDROID_HOME=$PWD/android-sdk-linux
  - export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/
  - chmod +x ./gradlew
  # temporarily disable checking for EPIPE error and use yes to accept all licenses
  - set +o pipefail
  - yes | android-sdk-linux/tools/bin/sdkmanager --licenses
  - set -o pipefail

stages:
  - build

apkCreate:
  stage: build
  script:
    - ./gradlew :app:assembleDevRelease
    - ./gradlew :app:assembleStgRelease
    - ./gradlew :app:assembleProdRelease
  artifacts:
    name: $ARTIFACTS_NAME
    paths:
      - $ARTIFACTS_DIR
  when: manual

ワーニングを確認してみる

1. Compatible side by side NDK version was not found.

「互換性のあるサイドバイサイドNDKバージョンが見つかりませんでした。」(google翻訳)

NDKインストールして無かったから、インストールする様にしてみた。

ndk-bundleのインストールを追加(2行目)

gitlab-ci.yml
  - echo y | android-sdk-linux/tools/bin/sdkmanager "build-tools;${ANDROID_BUILD_TOOLS}" >/dev/null
  - echo y | android-sdk-linux/tools/bin/sdkmanager "ndk-bundle" >/dev/null

すると、、、先ほど出ていたワーニングは出なくなったが、今度は「ARMEABI」が出る様になった

> Task :app:stripProdReleaseDebugSymbols
Unable to strip library '/builds/jad/jadandroid/app/build/intermediates/merged_native_libs/prodRelease/out/lib/armeabi/libAndrJFPDFEMB.so' due to missing strip tool for ABI 'ARMEABI'. Packaging it as is.

ググって、調べてみると
https://stackoverflow.com/questions/53437653/android-studio-missing-strip-tool

「The default installed NDK doesn't seem to have the tools required to strip binaries that have been built with ARMEABI support, so it ends up packaging the whole library, which increases the built file size substantially.

I've found that installing the "NDK (Side by side)" tool from Android Studio -> Tools -> SDK Manager -> SDK Tools takes care of this warning and also reduces the built APK size, especially for React Native projects.」

デフォルトでインストールされたNDKには、ARMEABIサポートを使用してビルドされたバイナリを除去するために必要なツールがないようです。
そのため、ライブラリ全体がパッケージ化され、ビルドされたファイルサイズが大幅に増加します。

これが原因でapkのサイズが肥大化してるぽい

2. ARMEABIはNDK r17で削除されたそうなので、ARMEABIネイティブのsoファイルを除外

app/build.gradle
android {    
    packagingOptions {
        // exclude ARMEABI native so file, ARMEABI has been removed in NDK r17.
        exclude "lib/armeabi/**"
    }
}

ワーニングが出なくなって、サイズも元に戻り、めでたしめでたし

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5