Android AGP升级后导致APK体积增大

升级Android Gradle Plugin (AGP) 至7.0+后,APK的release包体积可能增加,原因是原生库默认以未压缩形式打包。官方建议此设置能减小安装大小、下载大小并加快加载时间。若要恢复压缩原生库,需在构建脚本中调整相应配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目AGP升级至7.0+后发现打出来release包的体积比之前增加近一倍,经过查询官方文档得知在AGP3.6.0时的行为变更上:默认情况下,原生库以未压缩的形式打包。

在构建应用时,插件现在默认会将 extractNativeLibs 设置为 "false"也就是说,您的原生库将保持页面对齐状态并以未压缩的形式打包。虽然这样会增加上传大小,但您的用户会从以下优势中受益:

  • 减小应用安装大小,因为平台可以直接从已安装的 APK 访问原生库,而无需创建库的副本。
  • 减小下载文件大小,因为在 APK 或 Android App Bundle 中添加未压缩的原生库通常可提高 Play 商店的压缩率。

如果您希望 Android Gradle 插件改为打包压缩后的原生库,请在应用的清单中添加以下代码:

<application
    android:extractNativeLibs="true"
    ... >
</application>

但是从 AGP 4.2.0 开始,extractNativeLibs 清单属性已被 DSL 选项 useLegacyPackaging 取代。 您应该使用应用的 build.gradle 文件中的 useLegacyPackaging(而非清单文件中的 extractNativeLibs)来配置原生库压缩行为:

使用 DSL 打包压缩后的原生库

官方建议以未压缩的形式打包原生库,因为这会减小应用安装大小,缩减应用下载大小,并缩短用户的应用加载时间。不过,如果您希望 Android Gradle 插件在构建应用时打包压缩后的原生库,请在应用的 <

[DEBUG]: Remove directory and subdirectory src/main/assets [DEBUG]: Create directory src/main/assets Compiling '/root/android/.buildozer/android/app/sitecustomize.py'... Compiling '/root/android/.buildozer/android/app/main.py'... [DEBUG]: Remove directory and subdirectory /tmp/p4a-extra-env-w25uqkn9 [DEBUG]: Remove directory and subdirectory src/main/res [INFO]: Detected highest available build tools version to be 36.0.0 [DEBUG]: -> running gradlew clean assembleDebug [DEBUG]: [DEBUG]: > Task :processDebugMainManifest [DEBUG]: /root/android/.buildozer/android/platform/build-arm64-v8a/dists/myapp/src/main/AndroidManifest.xml:59:18-50 Warning: [DEBUG]: android:extractNativeLibs should not be specified in this source AndroidManifest.xml file. See https://d.android.com/guide/topics/manifest/application-element#extractNativeLibs for more information. [DEBUG]: The AGP Upgrade Assistant can remove the attribute from the AndroidManifest.xml file and update the build file accordingly. See https://d.android.com/studio/build/agp-upgrade-assistant for more information. [DEBUG]: [DEBUG]: > Task :compileDebugJavaWithJavac [DEBUG]: Note: Some input files use or override a deprecated API. [DEBUG]: Note: Recompile with -Xlint:deprecation for details. [DEBUG]: [DEBUG]: Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0. [DEBUG]: [DEBUG]: You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins. [DEBUG]: [DEBUG]: See https://docs.gradle.org/8.0.2/userguide/command_line_interface.html#sec:command_line_warnings [DEBUG]: [DEBUG]: BUILD SUCCESSFUL in 14s [DEBUG]: 33 actionable tasks: 33 executed [INFO]: <- directory context /root/android/.buildozer/android/platform/python-for-android [DEBUG]: All possible dists: [<Distribution: name myapp with
03-23
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值