首页 > 解决方案 > 无法从命令行使用 bundletool 构建 android app bundle

问题描述

我正在尝试按照https://developer.android.com/studio/build/building-cmdline#bundletool-build中描述的说明android app bundle从命令行构建bundletool

一个基本从 Android Studio 创建的简单 android 项目用于调查在 Android Studio 上成功编译的项目。

编译资源的步骤是可以的,但是在与 aapt2 的链接步骤中失败了。经测试aapt2-3.4.0-5326820-windows

命令

set ANDROID_TOOL=%ANDROID_SDK%/platforms/android-26/android.jar
%AAPT2% link --proto-format -o output.apk -I %ANDROID_TOOL% --manifest %CURRENT_DIR%/app/src/main/AndroidManifest.xml -R @compiled_resources.txt --auto-add-overlay -v

compiled_resources.txt包含我之前处理过的 .flat 编译资源列表。

错误

error: resource style/Theme.AppCompat.Light.DarkActionBar (aka com.example.doandominh.simpleaab:style/Theme.AppCompat.Light.DarkActionBar) not found.
I:\RnD\AAB\SimpleAAB\\app\src\main\res\values\styles.xml:6: error: style attribute 'attr/colorPrimary (aka com.example.doandominh.simpleaab:attr/colorPrimary)' not found.
I:\RnD\AAB\SimpleAAB\\app\src\main\res\values\styles.xml:7: error: style attribute 'attr/colorPrimaryDark (aka com.example.doandominh.simpleaab:attr/colorPrimaryDark)' not found.
I:\RnD\AAB\SimpleAAB\\app\src\main\res\values\styles.xml:8: error: style attribute 'attr/colorAccent (aka com.example.doandominh.simpleaab:attr/colorAccent)' not found.
error: failed linking references.

我认为这取决于类似 'com.android.support:appcompat-v7:26.1.0' 但我不知道如何将它们添加到 aapt2 链接参数。

有没有人遇到过这个问题?任何评论都非常感谢。

标签: androidandroid-app-bundle

解决方案


tldr:您还需要编译 AppCompat 资源,因为您正在手动构建所有这些资源,并将它们传递给与本地资源相同的链接方式(顺序很重要,以保持正确的覆盖/覆盖)。

Android Gradle 插件在应用级别处理资源的方式是:

  • 合并所有资源(本地应用程序、本地库、远程库、额外资源源集等)
  • 使用“aapt2 compile”编译所有内容(有或没有 PNG 处理)
  • 将所有内容链接到 APK

推荐阅读