Android 由于 lib 目录,APK 文件太大

Android APK file too big because of lib directory

我正在构建一个 Android 应用程序,其 APK 文件大小约为 4MB。从几周前开始,在构建签名应用程序时,生成的 APK 文件约为 17MB。

在调查了为什么会发生这种情况后,我发现新的 APK 存档包含 /lib 目录,而该目录在大小为 4MB 的旧 APK 中不存在。有谁知道为什么这个 lib 目录突然出现在 APK 存档中,有没有办法删除它?

APK 存档中 /lib 目录的结构是:

/lib
    /arm64-v8a
    /armeabi
    /armeabi-v7a
    /mips
    /x86
    /x86_64

我最近将 Android Studio 更新到 2.0,并且还升级了 gradle。这会是个问题吗?是否有一些配置参数可以解决这个问题?

我的 gradle 文件如下所示:

buildscript {
    repositories {
        mavenCentral()
        maven { url 'https://maven.fabric.io/public' }

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0'
        classpath 'io.fabric.tools:gradle:1.+'

    }
}

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories {
    mavenCentral()
    maven { url 'https://maven.fabric.io/public' }
    flatDir { dirs 'aars' }
}

android {
    // when changing this, YOU MUST change C:\AndroidADT\sdk\build-tools\xx.yy.zz\dx.bat to have -> set java_exe=C:\Windows\System32\java.exe
    compileSdkVersion 21
    buildToolsVersion "21.1.1"

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 21
    }
    def homeDir = System.getenv('HOMEDRIVE') + System.getenv('HOMEPATH');

    signingConfigs {
        cinema {
            storeFile = file("keystore\cinema.keystore.jks")
            storePassword = "cinema"
            keyAlias = "cinema"
            keyPassword = "cinema"
        }
        dev {
            storeFile = file("keystore\development.keystore.jks")
            storePassword = "development"
            keyAlias = "development"
            keyPassword = "development"
        }
    }

    buildTypes {
        debug {
            applicationIdSuffix ".debug"
        }
        cinema {
            debuggable false
            signingConfig signingConfigs.cinema
            jniDebuggable false
            applicationIdSuffix ".cinema"
        }
        dev {
            debuggable true
            signingConfig signingConfigs.dev
            jniDebuggable true
            applicationIdSuffix ".dev"
        }
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src', 'src-gen']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']

            assets.srcDirs = ['assets']
        }
        debug {
        }
        dev {
            res.srcDirs = ['res_dev']
        }
        cinema {
            res.srcDirs = ['res_cinema']
        }
        androidTest.setRoot('tests')
    }


    packagingOptions {
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE.txt'
    }
}

dependencies {
    compile 'joda-time:joda-time:2.3'
    compile 'com.android.support:support-v4:21.0.0'
    compile 'com.android.support:appcompat-v7:21.0.0'
    compile 'com.google.android.gms:play-services-plus:8.3.0'
    compile 'com.google.android.gms:play-services-auth:8.3.0'
    compile 'com.google.android.gms:play-services-gcm:8.3.0'
    compile 'com.facebook.android:facebook-android-sdk:4.5.0'
    compile 'com.markupartist.android.widget:pulltorefresh:1.0@aar'
    compile 'com.paypal.sdk:paypal-android-sdk:2.13.3'
    compile('com.crashlytics.sdk.android:crashlytics:2.2.0@aar') {
        transitive = true;
    }
    compile files('libs/gson-2.2.4.jar')
    compile files('libs/twitter4j-core-4.0.2.jar')
    compile files('libs/core-3.1.0.jar')
    compile files('libs/estimote-sdk-preview.jar')

    compile files('libs/commons-codec-1.10.jar')
    compile files('libs/commons-lang-2.6.jar')
    compile files('libs/FastPaySDK_pro.jar')
}

试试这个从发布版本中排除 SO 文件

android {
buildTypes {
    release {
        ndk {
            abiFilters "armeabi-v7a", "armeabi" // includes ARM SO files only, so no x86 SO file
        }
    }
  }
}

没有测试过,或许你可以试试:abiFilters "" 排除所有.SO文件

问题是由包含 card.io 库的 PayPal SDK 产生的。我发现我的问题的解决方案是 disable card.io card scanning:

packagingOptions {
    exclude 'lib/arm64-v8a/libcardioDecider.so'
    exclude 'lib/arm64-v8a/libcardioRecognizer.so'
    exclude 'lib/arm64-v8a/libcardioRecognizer_tegra2.so'
    exclude 'lib/arm64-v8a/libopencv_core.so'
    exclude 'lib/arm64-v8a/libopencv_imgproc.so'
    exclude 'lib/armeabi/libcardioDecider.so'
    exclude 'lib/armeabi-v7a/libcardioDecider.so'
    exclude 'lib/armeabi-v7a/libcardioRecognizer.so'
    exclude 'lib/armeabi-v7a/libcardioRecognizer_tegra2.so'
    exclude 'lib/armeabi-v7a/libopencv_core.so'
    exclude 'lib/armeabi-v7a/libopencv_imgproc.so'
    exclude 'lib/mips/libcardioDecider.so'
    exclude 'lib/x86/libcardioDecider.so'
    exclude 'lib/x86/libcardioRecognizer.so'
    exclude 'lib/x86/libcardioRecognizer_tegra2.so'
    exclude 'lib/x86/libopencv_core.so'
    exclude 'lib/x86/libopencv_imgproc.so'
    exclude 'lib/x86_64/libcardioDecider.so'
    exclude 'lib/x86_64/libcardioRecognizer.so'
    exclude 'lib/x86_64/libcardioRecognizer_tegra2.so'
    exclude 'lib/x86_64/libopencv_core.so'
    exclude 'lib/x86_64/libopencv_imgproc.so'
}

或完全排除card.io库:

dependencies {
    compile('com.paypal.sdk:paypal-android-sdk:2.14.1') {
        exclude group: 'io.card'
    }
}

我希望这会对某人有所帮助。