4

This is mostly Duplicate of build fail for android but no answer is yet available and my scenario is little different so putting it again.

In my my case I am able to build apk properly and I want to create a app bundle after successful of command

ionic cordova build android --prod --release by running this cmd I am able to generate the apk.

After that I am going to /platform/android and running ./gradlew bundle and here I am getting this error

The Task.leftShift(Closure) method has been deprecated and is scheduled to be removed in Gradle 5.0. Please use Task.doLast(Action) instead.


FAILURE: Build failed with an exception.

* What went wrong:
Task 'bundle' is ambiguous in root project 'android'. Candidates are: 'bundleAppClassesDebug', 'bundleAppClassesDebugAndroidTest', 'bundleAppClassesDebugUnitTest', 'bundleAppClassesRelease', 'bundleAppClassesReleaseUnitTest', 'bundleDebug', 'bundleRelease'

How to fix it?

Ragesh Pikalmunde
  • 1,333
  • 1
  • 20
  • 44

7 Answers7

8

For those that end up here and find that the --packageType=bundle flag doesn't seem to work, pay close attention to the note here:

Note: You should use double -- to indicate that these are platform-specific arguments, for example:

cordova run android --release -- --keystore=../my-release-key.keystore --storePassword=password --alias=alias_name --password=password --packageType=bundle.

Notice the empty -- after --release. The below assumes you already have your keystore and password configuration in build.json:

cordova build android --packageType=bundle // flag is silently ignored, generates apk
cordova build android -- --packageType=bundle // flag works, generates aab
adamdport
  • 11,687
  • 14
  • 69
  • 91
5

Ionic has built-in command line to generate the .aab format.

AAB:

ionic cordova build android --prod --release -- -- --packageType=bundle
        

It will generate automatically the .aab at android/app/build/outputs/bundle/release

APK:

ionic cordova build android --prod --release 

May it helps someone. Thanks

Rehum
  • 534
  • 5
  • 14
3

In other answers it seems that you can run:

./gradlew bundleRelease

In my case it's not working. I solved upgrading android platform version for ionic.

ionic cordova platform rm android
ionic cordova platform add android@8
ionic cordova build android --prod --release

After that, if you go in platforms\android folder you are able to do

./gradlew bundle

I didn't check gradle versions for both android versions but I suppose it's changed.

Regards,Vincenzo

Vincenzo
  • 130
  • 1
  • 3
  • 12
  • After performing these procedures worked for me, when inserting the command cordova build android --prod --release -- --packageType=bundle was generated the .aab file instead of the .apk :) – Ronald Calazans Mar 09 '23 at 13:54
2

My requirements were different to the original question but this still might be useful for some. After upgrading Cordova the build command below was producing an .aab file where prior to update it was producing an .apk. Our build pipeline was set up for signing an APK file so I wanted to make APK the default.

$ ionic cordova build android --prod --release

When I upgraded from cordova 8.x.x to 10.x.x This command defaulted to an .aap file being produced

adding the -- --packageType=apk did not work.

I had to create a build.json in the root cordova app directory with the following:

{
    "android": {
        "debug": {
            "packageType": "apk"
        },
        "release": {
            "packageType": "apk"
        }
    }
}

This worked perfect.

jtrk
  • 21
  • 1
1

I got it to build successfully by updating build.json and running the command cordova run android --release!

What I did is:

  1. Upgrade Gradle form v6.7.1 to v7.3.3 (Windows version, download .zip and setup "Path").
  2. build.json add ,"packageType": "apk".

Build app-release.apk successful:

enter image description here

Tomislav Stankovic
  • 3,080
  • 17
  • 35
  • 42
李文豪
  • 11
  • 2
0

This works for me, keys are already generated.

cordova build android --prod --release -- --packageType=bundle
Irfan Ashraf
  • 2,430
  • 21
  • 20
0

ionic cordova build android --prod --release -- -- --packageType=bundle

This is the right answer

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 26 '22 at 01:49