How can I build an Android apk without Gradle on the command line?
Solution 1
Use the following steps to build your apk manually, if you don't want use ant/gralde to build. But you must have Android SDK installed at least.
create
R.java
fromaapt
use
javac
to compile all java source to*.class
use
dx
to convert all*.class
todex
file, e.g output isclasses.dex
create initial version of APK from assets, resources and
AndroidManfiest.mk
, e.g output isMyApplication.apk.unaligned
use
aapt
to addclasses.dex
generated in step 3 toMyApplication.apk.unaligned
use jarsigner to sign
MyApplication.apk.unaligned
with debug or release keyuse
zipalign
to align the final APK, e.g output isMyApplication-debug.apk
orMyApplication-release.apk
if signing with release keyDone
I have created a sample script to do all the stuffs above, see here
Actually, Some articles have discussed this topic, see the following links.
https://www.apriorit.com/dev-blog/233-how-to-build-apk-file-from-command-line
https://spin.atomicobject.com/2011/08/22/building-android-application-bundles-apks-by-hand/
Solution 2
Try this for building apps with support libraries from command line. https://github.com/HemanthJabalpuri/AndroidExplorer
Solution 3
alijandro gave a perfect answer. I managed to write simple ANT script that builds production APK with AdMob and without gradle usage. A couple useful comments:
If you want to obfuscate classes you have to jar the compiled classes (between
javac
anddx
steps) and runproguard
on it-
For
AdMob
you have to extract the following jars from zip archives (likeC:\Users\<User>\AppData\Local\Android\sdk\extras\google\m2repository\com\google\android\gms\play-services-ads\10.2.6\play-services-ads-10.2.6.aar
):- play-services-ads-10.2.6.jar
- play-services-ads-lite-10.2.6.jar
- play-services-base-10.2.6.jar
- play-services-basement-10.2.6.jar
- play-services-clearcut-10.2.6.jar
- play-services-gass-10.2.6.jar
- play-services-tasks-10.2.6.jar
These archives should be passed in javac
and dx
- For
AdMob
there are several additional simple config steps as well
Gradle
does a lot of mess with android projects, so own script looks like a singular solution for projects that are going to go into production
Solution 4
A while back I stumbled across this thread after getting frustrated with both Android Studio and Gradle. Inspired by the answer from alijandro and this template from authmane512, I wrote a series of scripts to compile an Android app (including with dependencies/packages) in Java or Kotlin without any external build system.
Link: https://github.com/jbendtsen/tiny-android-template
There is a little bit of DIY involved here, but given that it's the sort of stuff that something like Gradle would do for you, I would argue that it's useful to know. Besides, it's like wayyyy less slow, and you have a lot more control how your app gets assembled.
Related videos on Youtube
aoeu
Updated on June 11, 2022Comments
-
aoeu about 2 years
I wrote an Android app that uses no dependencies or modules, has a single activity, and has a single layout file.
How can I build an apk file of my app on the command line without using Gradle (or other "build systems" or "dependency management" software)?
-
CommonsWare over 7 yearsIf you don't like Gradle, you can look into another build system (e.g., Maven, Buck). If that's not the problem... what is the reason for avoiding Gradle?
-
aoeu over 7 yearsI don't want a build system. I want to build.
-
CommonsWare over 7 yearsBuilding an Android app involves lots of work. The diagram on that page will help get you started in terms of the different tools and pieces that are involved.
-
Ciro Santilli OurBigBook.com over 7 yearsrelated: just the dex: stackoverflow.com/questions/10199863/…
-
ChuckZHB almost 3 yearsI'm an iOS developer, after downloading Xcode, user could use it to create app directly. Almost all the gears are on belt once download is complete. But each time I use Android Studio, I was very confused. After download it, you has to wait that endless gradle sync. Why Google can't ship those gradle stuff with Android Studio release directly.
-
-
aoeu over 7 yearsThanks, that is exactly what I wanted to know! Nice shell scripting and thanks for the articles, too.
-
Leo supports Monica Cellio about 4 yearsWould be nice to see that ANT script, if you still have it around.
-
user3700562 over 3 yearsI did this and it works, however when I run the app in the emulator I always get a warning: "This app was built for an older version of Android and may not work properly." I made sure it's using the correct build-tools directory and the android.jar from the correct platform version folder. What else do I need to do?
-
user2347921 almost 3 years@user3700562, make sure your AndroidManifest.xml has a correct
android:minSdkVersion
specified. Right now, you need to specify version 23 or higher for the warning to not show up.