生成Google Play .aab文件时aapt2报错,资源缺失如何解决?
解决AAPT2链接阶段资源找不到的问题
问题重现
执行aapt2链接命令:
aapt2.exe link --proto-format -o base.zip -I android.jar --manifest decompile_apk\AndroidManifest.xml
触发以下资源缺失错误:
decompile_apk\AndroidManifest.xml:3: error: resource mipmap/launcher_icon (aka com.xyz.dbdb:mipmap/launcher_icon) not found. decompile_apk\AndroidManifest.xml:4: error: resource style/LaunchTheme (aka com.xyz.dbdb:style/LaunchTheme) not found. decompile_apk\AndroidManifest.xml:5: error: resource style/NormalTheme (aka com.xyz.dbdb:style/NormalTheme) not found. error: failed processing manifest.
现有资源文件:
D:\myproject\android\app\src\main\res\drawable\launch_background.xml D:\myproject\android\app\src\main\res\drawable-v21\launch_background.xml D:\myproject\android\app\src\main\res\mipmap-hdpi\launcher_icon.png D:\myproject\android\app\src\main\res\mipmap-mdpi\launcher_icon.png D:\myproject\android\app\src\main\res\mipmap-xhdpi\launcher_icon.png D:\myproject\android\app\src\main\res\mipmap-xxhdpi\launcher_icon.png D:\myproject\android\app\src\main\res\mipmap-xxxhdpi\launcher_icon.png D:\myproject\android\app\src\main\res\values\styles.xml D:\myproject\android\app\src\main\res\values-night\styles.xml
AndroidManifest.xml内容:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <application android:label="DBDB" android:name="${applicationName}" android:icon="@mipmap/launcher_icon"> <activity android:name=".MainActivity" android:exported="true" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize"> <!-- Specifies an Android theme to apply to this Activity as soon as the Android process has started. This theme is visible to the user while the Flutter UI initializes. After that, this theme continues to determine the Window background behind the Flutter UI. --> <meta-data android:name="io.flutter.embedding.android.NormalTheme" android:resource="@style/NormalTheme" /> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <!-- Don't delete the meta-data below. This is used by the Flutter tool to generate GeneratedPluginRegistrant.java --> <meta-data android:name="flutterEmbedding" android:value="2" /> </application> <uses-permission android:name="android.permission.INTERNET"/> </manifest>
原因
你的aapt2 link命令未指定资源目录或编译后的资源包,导致AAPT2无法定位Manifest中引用的图标、主题等资源。
解决方案
AAPT2的工作分为编译和链接两个阶段,必须先编译资源再进行链接:
1. 编译资源目录
先将res目录下的所有资源编译成AAPT2可识别的中间格式:
aapt2.exe compile --dir D:\myproject\android\app\src\main\res -o compiled_res.zip
--dir:指定你的项目res目录绝对路径-o:输出编译后的资源压缩包
2. 执行链接命令(加入编译后的资源包)
修改原来的链接命令,添加编译好的compiled_res.zip作为参数:
aapt2.exe link --proto-format -o base.zip -I android.jar --manifest decompile_apk\AndroidManifest.xml compiled_res.zip
可选:指定应用ID(确保包名一致)
如果资源引用的包名(com.xyz.dbdb)需要明确指定,可添加以下参数:
aapt2.exe link --proto-format -o base.zip -I android.jar --package-id 0x7f --rename-manifest-package com.xyz.dbdb --manifest decompile_apk\AndroidManifest.xml compiled_res.zip
--package-id:资源包ID,通常固定为0x7f--rename-manifest-package:强制将Manifest的包名修改为指定值,确保和资源引用的包名匹配
内容的提问来源于stack exchange,提问作者AnR
相关产品推荐
相关产品推荐

