Error while trying to generate apk - Ionic v 3.20.1

Asked

Viewed 603 times

0

I have the following problem - I am 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 the newest versions of Node.js, angular, Cordova, Ionic and Gradle installed on the machine (Windows 10).

Does anyone know how to fix this mistake?

  • Do you have images downloaded from android versions on your PC? You downloaded Android Studio and did this?

  • 1

    I have the versions of Android 6.0 up to 10.0 all installed in my Android Studio. System variables have been configured.

  • I got it, add this info from the android images to your question, and take a look at Guilherme Nascimento’s answer.

2 answers

0

  • So, I had already seen related posts and tried to install previous versions of Cordova and Ionic before posting the question, but still gives the same error...

  • And in this other case it matches your error:Instead, add this module to your dependencies and use regular "require" to load it. Replace requireCordovaModule with require : requireCordovaModule("q") to require("q")

0


And why not use Ionic-cli itself to generate APK?

It informs you any lack of global dependency, as if the cordova or the @ionic/app-scripts, he even helps you install what’s missing

The command in the project folder is this:

ionic cordova build android

This will generate apk for debug, if you want to test in 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 the terminal/cmd


Note that ionic cordova build android will generate apk for testing/debug, if you want to publish you need to do a number of steps:

Generate APK for production

In the project folder run:

ionic cordova build android --prod --release

At the end of the command it will show on 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), because otherwise will have to type all the way 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

He will ask to fill in some data and then will ask for a password (key), choose a key, but remember to write it down somewhere

By signing the APK

done this your jks is ready, then the next step is to sign the apk, in the example below wrote: /CAMINHO/AONDE/ESTA/SALVO/O/APK/PRODUCAO.apk, change 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 regarding the program apksigner are in the android sdk folder, then this folder should be placed in the environment variables, user or operating system, example if it is windows and want to add via line command/ run time:

set PATH=%PATH%;C:\<localização>\Android\Sdk\build-tools\<versão do sdk>

Or add in the same environment variables. Done this, let’s compress:

zipalign -v 4 /CAMINHO/AONDE/ESTA/SALVO/O/APK/PRODUCAO.apk /novo/caminho/apk-comprimido.apk

The way /novo/caminho/apk-comprimido.apk is just example, you should choose a path to save the compressed version of your apk

Verifying the signature

To verify the signature perform:

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 via:


App Bundle vs APK

If you intend to use App Bundle instead of apk (it generates apks for different versions of android), so instead of generating apk with ionic cordova build android --prod --release just run:

ionic cordova prepare android

And then open the folder /projeto/platforms/android/ in androidstudio and do everything for it, I do not believe it has much advantage to Ionic or other projects Ordova use this, because the visual features are usually html, images and css, nothing that will really have difference between android versions, that is it is likely that the gain is minimal.

  • Thank you William, your reply was quite complete! Now I’m having a different error: Exception in thread "main" java.net.Connectexception: Connection timed out: connect . I believe there’s still some dependency missing...

  • @Flaviapino please put the full log in the question, usually errors so are consequences of previous errors.

  • I figured out what it was - it was the Gradle version! I noticed that Gradle was trying to force a download of version 4.10.3 and I was using the latest version (5.6.2). I downloaded this version, and after configuring the variables I ran the following command - .\gradlew

  • @Flaviapino I also manually install Gradle and create the environment variable GRADLE_HOME and in PATH Seto ;%GRADLE_HOME%\bin, then restart (or logoff) the system. I only had headaches with ADT even on the versions of Gradle, but with the "compiler" via command line I had no problems.

  • 1

    That! That’s exactly what I did. Your answer was really very helpful! Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.