How to create/build *.APK file for Android from Ionic project

hi friends,
pls help me - I cannot crete *.APK file for Android device from my Ionic project. What’s wrong?

I created new Ionic app:

ionic start Test006 blank --type=angular

app is created, works fine …
Then I continued by add platform:

ionic cordova platform add android

20230113-001a

everything seems OK in this point, but … I try build android package, start command:

ionic cordova build android

and get following:

How can I build my ionic project and create file for Android device (*.apk)?
What commands and what prerequisities I need?

Edit:
If I run command:

ng build

but I don’t know how to create/build for platform android, please HELP.

This guide is for Capacitor, but I need to use Cordova (in my Ionic 5 app).

According to guide for Ionic 5 (use Cordova) (https://ionicframework.com/docs/v5/deployment/play-store)
I got the same error on first command:

ionic cordova build android --prod --release

I think you need to downgrade your node/npm version since your on ionic 5. I’m not sure of what is the highest version ionic 5 is allowed to use.

Also remove the platforms and plugins folders and re-add them. This can be the issue too.

1 Like

I have these versions:
20230113-005a

Thanks for the great share ! Really Helpful article.

abhisheksharma29502, have any tip for me?

use node 16 or 18, angular doesn’t like even versions as they are not LTS

also use ionic 6.x, 5.4 is very old and deprecated

hi friends,
1.) need install “Android Studio” (01/2023 file “android-studio-2022.1.1.19-windows.exe”)

2.) in “Android Studio” run “SDK Manger” and in tab “SDK Platform” check and install necessary Android device versions:

3.) in tab “SDK Tools” check and install “Android SDK Build-Tools” (need 30.0.3 version):

4.) in Ionic application folder - need install:

npm install @ionic/cordova-builders

5.) add to Ionic (ver.5) project “angular.json” next lines:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "app": {
      "projectType": "application",
      "schematics": {},
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
     // ... NEED ADD FOLLOWING LINES: 
        "ionic-cordova-build": {
          "builder": "@ionic/cordova-builders:cordova-build",
          "options": {
            "browserTarget": "app:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "app:build:production"
            }
          }
        },
        "ionic-cordova-serve": {
          "builder": "@ionic/cordova-builders:cordova-serve",
          "options": {
            "cordovaBuildTarget": "app:ionic-cordova-build",
            "devServerTarget": "app:serve"
          },
          "configurations": {
            "production": {
              "cordovaBuildTarget": "app:ionic-cordova-build:production",
              "devServerTarget": "app:serve:production"
            }
          }
        }

6.) check settings path to Java in “Environment Variables”:

7.) and in “System variables” add “JAVA_HOME” and “ANDROID*” to “Path”:

8.) in Ionic project folder check Cordova requirements by command:

cordova requirements
20230117-007a

9.) run (again):

ionic cordova build android


10.) “*.apk” file is (finally) created:
20230117-009a

So, I believe to help someone.

1 Like