android-studio - 使用 Android Studio 删除未使用的资源?

标签 android-studio android-resources

我想从我的项目中删除未使用的资源以减小应用程序大小。有没有办法通过有效地使用 Android Studio IDE 来做到这一点?

最佳答案

Android 的 Gradle 构建系统支持 Resource Shrinking :在构建时自动删除打包应用程序中未使用的资源。除了删除项目中在运行时实际上不需要的资源之外,这还会从您所依赖的库中删除资源,如果您的应用程序实际上不需要这些资源。

例如,您的应用程序正在使用 Google Play 服务来访问 Google Drive 功能,而您当前没有使用 Google 登录,那么这将删除登录按钮的各种可绘制资源。

注:资源收缩只能与代码收缩(例如 ProGuard)结合使用。这就是它可以从库中删除未使用资源的方式;通常,库中的所有资源都会被使用,只有当我们删除未使用的代码时,才能清楚地看到剩余代码中引用了哪些资源。

要启用资源缩减,请按如下方式更新您的构建类型:

android {
    ...

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

谷歌最近正式推出了 Android Studio 2.0,现在他们在 IDE 本身中提供了一个选项。

Right click on app --> Refactor --> Remove Unused Resources



会提示

enter image description here

在确认操作之前选中该框,以便您可以摆脱未使用的 @id声明也。
  • 在 APK 优化方面考虑 Selecting a Format事实也是如此。
  • 使用WebP图像提供比 JPEG 或 PNG 更好的压缩。 Android 4.0(API 级别 14)及更高版本支持有损 WebP 图片,Android 4.3(API 级别 18)及更高版本支持无损和透明 WebP 图片。
  • 关于android-studio - 使用 Android Studio 删除未使用的资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36591421/

    相关文章:

    java - Android Studio 无法识别字符串

    android - 如何使用 Android Studio 在真实设备中测试 Android 应用程序?

    android - "Nothing to show"当我尝试在 Android Studio 中插入一个新的 Vector Asset

    c# - Xamarin.Droid : Resource. Designer.cs 更新后损坏

    Android ImageView 根据标签设置图片

    android - Context.getDrawable 中的 NullPointerException

    android - strings.xml 中的 "is translated here but not found in default locale"错误,可翻译 = "false"

    android - 无法将altbeacon库包含到新项目中

    android - 什么时候应该使用 Assets 而不是 Android 中的原始资源?

    安卓动态字符串 : %1d vs %1$d