Error trying to generate apk-Ionic v 3.20.1
I'm having the following problem - I'm trying to generate an APK in Ionic using the command below:
npm run generate-apk
After running this command, the following error message appears at the end:
> cordova.cmd build android --release --buildConfig
Using "requireCordovaModule" to load non-cordova module "q" is not supported. Instead, add this module to your dependencies and use regular "require" to load it.
[ERROR] An error occurred while running subprocess cordova.
cordova.cmd build android --release --buildConfig exited with exit code 1.
I'm with newer versions of node.JS, angular, cordova, Ionic and gradle installed on the machine (Windows 10).
Does anyone know how to solve this error?
2 answers
And why not use the Ionic-cli itself to generate the APK?
It informs you of any lack of global dependency, such as missing cordova
or @ionic/app-scripts
, it even helps you install the missing
The command in the project folder is this:
ionic cordova build android
This will generate the apk for debugging, if you want to test on the emulator or phone the command is:
ionic cordova run android -l -c
The -l
is the parameter to do the livereload (you can edit and it updates directly without needing to reinstall the apk in the emulator) and the -c
is to display console.log
and errors in terminal / cmd
Note that ionic cordova build android
will generate the apk for testing / debugging, if you want to publish you need to do a series of steps:
Generate the APK for production
In the project folder run:
ionic cordova build android --prod --release
At the end of the command it will show in the console or cmd where you saved the apk, copy the path
Note: this apk is usually not "installable" as it will need to be signed
Generate a signature
First, if it is windows, the folder "C:\Program Files\Java\jdk1.8.<versao do teu jdk 1.8>\bin\"
must be in the environment variables (of the operating system), otherwise you will have to enter the entire path to access the program keytool
and jarsigner
Then navigate to the project folder and run this:
keytool -genkey -v -keystore minhaassinatura.jks -keyalg RSA -keysize 2048 -validity 10000 -alias nomedoprojetoouqualqueroutracoisa
It will ask to fill in some data and then it will ask for a password (key), choose a key, but remember to write it down somewhere
Signing O APK
Done that your jks is ready, so the next step is to sign the apk, in the example below I wrote: /CAMINHO/AONDE/ESTA/SALVO/O/APK/PRODUCAO.apk
, swap the path where the ionic cordova build
generated the apk
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore minhaassinatura.jks /CAMINHO/AONDE/ESTA/SALVO/O/APK/PRODUCAO.apk nomedoprojetoouqualqueroutracoisa
Compressing
After the signing process you must compress the apk, both the program zipalign
and the program apksigner
are in the Android sdk folder, so this folder must be placed in the environment variables, the user or the operating system, example if it is windows and you want to add via line command / runtime:
set PATH=%PATH%;C:\<localização>\Android\Sdk\build-tools\<versão do sdk>
Or add in the same environment variables. Once this is done, let's compress:
zipalign -v 4 /CAMINHO/AONDE/ESTA/SALVO/O/APK/PRODUCAO.apk /novo/caminho/apk-comprimido.apk
The path /novo/caminho/apk-comprimido.apk
is example only, you should choose a path to save the compressed version of your apk
Checking the signature
To verify the accuracy run:
apksigner verify /novo/caminho/apk-comprimido.apk
If you have done everything correctly your apk should be ready to publish, the rest you should do street:
App bundle vs APK
If you intend to use App bundle instead of apk (it generates apks for different versions of android), then instead of generating the apk with ionic cordova build android --prod --release
just run:
ionic cordova prepare android
And then open the /projeto/platforms/android/
folder in androidstudio and do everything for it, I don't think it has much advantage for ionic or other cordova projects to use this, because the visuals are usually html, images and css, nothing that will really have a difference between android versions, that is, it is likely that the gain is minimal.
Returning an earlier version of Cordova may solve your problem:
Npm install-g [email protected]
Source: