How to build an Android APK file without using Android Studio in a Capacitor project?

I recently came across Capacitor and i tried it, I find it better than Cordova.
But the only thing is I could not find a command to generate .apk or archive.
In Cordova, I was able to generate .apk without even opening Android Studio.
I am looking for capacitor equivalent commands for ionic cordova build android and ionic cordova build ios

Hello,

Give this a try:
"ios": "sudo ionic build && npx cap sync ios && npx cap open ios", "android": "sudo ionic build && npx cap sync android && npx cap open android",

You can add there to your package json and when you want a new build just go npm run [platform]. You can also try running it without sudo
Alex.

Hey, type npx cap open android, this will open the android studio automatically. Wait till the gradle downloads successful and click on the play button (that’s if you have an android device connected to the pc) else click on Build -- >Build Bundle /Apk and you are good to go

1 Like

Hey, did you read the question?

without using Android Studio

Looking for same

2 Likes

I have a current project in Cordova and it is integrated with Team City CI / CD.
We generate .apk and Xcode archives by running Cordova build whenever we push something to the master branch.
I like Capacitor but didn’t find the capacitor build commands for my scenario.

You can’t. Capacitor just opens the IDEs that you need in order to build Platform projects.

1 Like

I would suggest following/participating in this issue.

I was having this issue and the way I worked out to do this:

You need to add android first:

ionic capacitor add android 
ionic capacitor copy android && cd android && ./gradlew assembleDebug && cd ..

Then your apk will be at:

android/app/build/outputs/apk/debug/app-debug.apk

If you want to run on device directly from command line:

ionic capacitor copy android && cd android && ./gradlew assembleDebug && ./gradlew installDebug && cd ..

Note: It doesn’t work without entering the android directory

I hope that helps!

16 Likes

Awesome dude!

It work’s. What about the release and production apk?

I have not done this yet, please share your solution if you do it.

Here is how to production build for Android:

cd android && 
./gradlew assembleRelease && 
cd app/build/outputs/apk/release &&
jarsigner -keystore YOUR_KEYSTORE_PATH -storepass YOUR_KEYSTORE_PASS app-release-unsigned.apk YOUR_KEYSTORE_ALIAS &&
zipalign 4 app-release-unsigned.apk app-release.apk

This will generate app-release.apk which should be good to go the play store (see android/app/build/outputs/apk/release folder).

Here is the jarsigner command manual.

7 Likes

Thanks, worked fine! Saved my day :slight_smile:

1 Like

Thanks, this worked for me and saved me a bunch of time!

1 Like

What should YOUR_KEYSTORE_ALIAS be?

You can find your keystore alias by running keytool -v -list -keystore YOUR_KEYSTORE_PATH

After spending a whole night trying to configure Android studio, which wouldn’t let a simple ionic app build properly, and then trying to fix issues for cordova, this post of yours gave me my spirits back! You’re really awesome man!!

1 Like

Just a trick to know all the targets -

cd android 
gradlew app:tasks --all

This will out you all the tasks which can be called by gradlew without opening android studio.
Example for building app bundle

./gradlew bundleRelease

Hope it helps :slight_smile:

2 Likes

Could continue with my Gitlab CI config because of you :slight_smile:
Thank you so much!

1 Like

Thank u bro :heart_eyes: :smiling_face_with_three_hearts: :tada: :tada: :tada: :tada: :confetti_ball: :confetti_ball: :1st_place_medal: :1st_place_medal: :1st_place_medal: :1st_place_medal:

1 Like

In case someone on a Mac Big Sur is still having issues, run this (same as the chosen answer, but with ‘sudo’ added)…

sudo ionic capacitor copy android && cd android && sudo ./gradlew assembleDebug && cd ..