bundletool build-bundle error: [BT:1.8.2] Error: Version code not found in manifest

bundletool build-bundle error: [BT:1.8.2] Error: Version code not found in manifest

我正在尝试将 apk 转换为 aab,使用 apktool 反编译,然后使用 aapt2 和 bundletool 构建(全部在命令行中,没有 gradle)。

反编译后,aapt2 编译,link 看起来不错,但是当我用 bundletool 执行 build-bundle 时,我得到了这个错误。什么地方出了错?我该如何解决?

# 1. Decompile apk and copy res[folder] and Androidmanifest.xml into $HOME/apk2aa/temp_dir
apktool d input.apk

# 2. “Compile” all resources using aapt2
aapt2 compile  --dir input/res -o "${APPROOT}/temp_dir/compiled_resources"

# 3. “Link” the resources into a temporary APK, generating the R.java file along the way and converting the resources into protobuf format:
aapt2 link --proto-format -o temporary.apk -I "${SDK}/platforms/android-29/android.jar" --manifest input/AndroidManifest.xml -R compiled_resources/*.flat  --auto-add-overlay --java gen

然后我从具有与此处所述相同结构的文件夹生成一个 zip (base.zip):https://developer.android.com/studio/build/building-cmdline#package_pre-compiled_code_and_resources

# 4. Use bundletool build-bundle command to create aab
java -jar "${APPROOT}/bundletool.jar" build-bundle --modules=base.zip --output=output.aab

日志:

[BT:1.8.2] Error: Version code not found in manifest.
com.android.tools.build.bundletool.model.exceptions.InvalidVersionCodeException: Version code not found in manifest.
        at com.android.tools.build.bundletool.model.exceptions.InvalidVersionCodeException.createMissingVersionCodeException(InvalidVersionCodeException.java:29)
...

apktool 生成的 AndroidManifest.xml 缺少 android:versionCode 标签,如下所示:

<?xml version="1.0" encoding="utf-8" standalone="no"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" android:compileSdkVersion="30" android:compileSdkVersionCodename="11" package="com.hairlight.mediapipe.apps.hairsegmentationgpu" platformBuildVersionCode="30" platformBuildVersionName="11">
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-feature android:name="android.hardware.camera"/>
    <application android:allowBackup="true" android:appComponentFactory="androidx.core.app.CoreComponentFactory" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
        <activity android:exported="true" android:name="com.hairlight.mediapipe.apps.basic.MainActivity" android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

apktool.yml 文件具有版本代码

!!brut.androlib.meta.MetaInfo
apkFileName: input.apk
compressionType: false
doNotCompress:
- resources.arsc
- png
- assets/empty_asset_generated_by_bazel~
isFrameworkApk: false
packageInfo:
  forcedPackageId: '127'
  renameManifestPackage: null
sdkInfo:
  minSdkVersion: '21'
  targetSdkVersion: '30'
sharedLibrary: false
sparseResources: false
unknownFiles: {}
usesFramework:
  ids:
  - 1
  tag: null
version: 2.6.0
versionInfo:
  versionCode: '8'
  versionName: 1.0.0

原始 AndroidManifest.xml 文件也有标签。 因此,apktool 找到 versionCode,然后将其从 AndroidManifest.xml 中剥离,并将其放入 yml 文件中。如何确保 apktool 将 versionCode 保留在 AndroidManifest.xml?

如有任何帮助,我们将不胜感激。

我使用的版本是: aapt2-7.2.0-alpha07-7984345-linux.jar、bundletool-all-1.8.2.jar、构建工具 29.0.2、平台版本 29、javac 1.8.0_312、apktool_2.6.0.jar

我找到了解决我自己问题的方法。我可以提供 -m 标志来反编译这样的代码:

apktool d -m input.apk 

此标志将确保匹配原始文件并避免从 AndroidManifest.xml

中剥离 versionCode 和 versionName