【问题标题】:App size increased in Release build compared to debug build与调试版本相比,发布版本中的应用程序大小增加
【发布时间】:2017-06-11 18:34:58
【问题描述】:

在我之前的所有项目中,调试版本总是更大,发布版本更小。

但是我遇到了奇怪的问题。 当我构建调试 APK 时,它有 1085KB

如果我构建发布版 APK,它有 2432KB

任何人都给我这个问题的解决方案。

我在 build.gradle 中的依赖项:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "ranjih.kotlinandroid"
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 5
        versionName "1.3"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.google.firebase:firebase-config:10.2.1'
    compile 'com.google.firebase:firebase-messaging:10.2.1'
    compile 'com.google.android.gms:play-services-ads:10.2.1'
    compile 'com.android.support:support-v4:25.3.1'
    compile('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') {
        transitive = true;
    }
    testCompile 'junit:junit:4.12'
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    compile project(path: ':lib')
}

apply plugin: 'com.google.gms.google-services'
repositories {
    maven {
        url "https://jitpack.io"
    }
    mavenCentral()
    maven { url 'https://maven.fabric.io/public' }
}

apply plugin: 'io.fabric'

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

    dependencies {
        // These docs use an open ended version so that our plugin
        // can be updated quickly in response to Android tooling updates

        // We recommend changing it to the latest version from our changelog:
        // https://docs.fabric.io/android/changelog.html#fabric-gradle-plugin
        classpath 'io.fabric.tools:gradle:1.+'
    }
}

【问题讨论】:

  • 你的应用中有很多图片资源吗? Android Studio 可能不会在调试版本中打包所有分辨率。
  • @Jonathan727 我没有很多图片。但是我在资产文件夹中有更多的 HTML 文件。我的疑问是应用程序兼容性依赖。这种依赖性会增加应用程序的大小吗?
  • 我建议按照@Moonbloom 的建议进行测试,并生成两个发布版 APK,一个带压缩包,一个不带压缩包。 buildTypes { release { minifyEnabled false} releaseMinify { minifiyEnabled true } }
  • 另外,请为您的应用发布整个 build.gradle 文件
  • @Jonathan727 如果我给 { release { minifyEnabled false}} 它会生成 3764KB。但我不知道为什么调试构建需要更少的大小。添加了完整的 gradle 文件。

标签: android android-studio android-gradle-plugin


【解决方案1】:

您在没有调试版本和发布版本的情况下进行测试,这简直是不行。

使用相同的构建变体进行测试。

在启用 minify 和 shrinkResources 时,我的 APK 大小总是相同或更低。

库可以根据构建类型定义不同的资源,因此其中一个库在构建发布时可能需要占用比调试时更多的空间。

否则尝试使用 Android Studio 的内置工具反编译 APK,看看是什么占用了所有空间。

【讨论】:

  • 是的,当我调试版本大于发布版本时。这个问题的原因我没有构建apk。只需运行该项目,它就会自动生成 APK(对此进行了测试)并感到困惑 :)
【解决方案2】:

如果存在较大的 png 文件,请尝试使用下一个:

android {
    …
    buildTypes {
        release {
            crunchPngs true // or false
        }
    }
}

【讨论】:

    【解决方案3】:

    如果您只是在尝试在设备/模拟器中运行或调试应用程序时获得由 Android Studio 生成的调试 apk,那么此 apk 似乎只打包了该特定设备规格的资源/依赖项。

    尝试使用 Build > Build APK,您可能会注意到(假定)相同的调试 apk 会更大,甚至可能比发布版本更大。

    我还 answered a similar question 提供了有关为什么会发生这种情况的更多详细信息。

    【讨论】:

      猜你喜欢
      • 2010-10-27
      • 2020-08-02
      • 1970-01-01
      • 2012-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多