title: bundletool build-bundle error: [BT:1.8.2] Error: Version code not found in manifest
body: <p>I am trying to turn an apk into aab, decompile with apktool, and build with aapt2 and bundletool (all in command line, without gradle).</p>
  <p>After decompile, aapt2 compile and link looks good, but when I execute build-bundle with bundletool, I got this error. What went wrong? And How can i fix it?</p>
  <pre class="lang-sh prettyprint-override"><code># 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 &quot;${APPROOT}/temp_dir/compiled_resources&quot;
  
  # 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 &quot;${SDK}/platforms/android-29/android.jar&quot; --manifest input/AndroidManifest.xml -R compiled_resources/*.flat  --auto-add-overlay --java gen
  </code></pre>
  <p>I then generate a zip (base.zip) from a folder with the same structure as described here: <a href="https://developer.android.com/studio/build/building-cmdline#package_pre-compiled_code_and_resources" rel="nofollow noreferrer">https://developer.android.com/studio/build/building-cmdline#package_pre-compiled_code_and_resources</a></p>
  <pre class="lang-sh prettyprint-override"><code># 4. Use bundletool build-bundle command to create aab
  java -jar &quot;${APPROOT}/bundletool.jar&quot; build-bundle --modules=base.zip --output=output.aab
  </code></pre>
  <p>Log:</p>
  <pre><code>[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)
  ...
  </code></pre>
  <p>The AndroidManifest.xml produced by apktool is missing android:versionCode tag as it can be seen here:</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; standalone=&quot;no&quot;?&gt;&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:compileSdkVersion=&quot;30&quot; android:compileSdkVersionCodename=&quot;11&quot; package=&quot;com.hairlight.mediapipe.apps.hairsegmentationgpu&quot; platformBuildVersionCode=&quot;30&quot; platformBuildVersionName=&quot;11&quot;&gt;
      &lt;uses-permission android:name=&quot;android.permission.CAMERA&quot;/&gt;
      &lt;uses-feature android:name=&quot;android.hardware.camera&quot;/&gt;
      &lt;application android:allowBackup=&quot;true&quot; android:appComponentFactory=&quot;androidx.core.app.CoreComponentFactory&quot; android:icon=&quot;@mipmap/ic_launcher&quot; android:label=&quot;@string/app_name&quot; android:roundIcon=&quot;@mipmap/ic_launcher_round&quot; android:supportsRtl=&quot;true&quot; android:theme=&quot;@style/AppTheme&quot;&gt;
          &lt;activity android:exported=&quot;true&quot; android:name=&quot;com.hairlight.mediapipe.apps.basic.MainActivity&quot; android:screenOrientation=&quot;portrait&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;android.intent.action.MAIN&quot;/&gt;
                  &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot;/&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>
  <p>The apktool.yml file has the versionCode</p>
  <pre class="lang-yaml prettyprint-override"><code>!!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
  </code></pre>
  <p>The original AndroidManifest.xml file has the tag as well.
  So, apktool finds the versionCode and then strips it from the AndroidManifest.xml and puts it in a yml file. How do I ensure that apktool leaves the versionCode in AndroidManifest.xml?</p>
  <p>Any help would be appreciated.</p>
  <p>Versions I used are:
  aapt2-7.2.0-alpha07-7984345-linux.jar, bundletool-all-1.8.2.jar, build-tools 29.0.2, platform version 29, javac 1.8.0_312, apktool_2.6.0.jar</p>