Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I build an Android apk without Gradle on the command line?

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)?

like image 547
aoeu Avatar asked Dec 14 '16 00:12

aoeu


People also ask

Does Android require gradle?

Gradle is one type of build tool that builds the source code of the program. So it's an important part of Android Studio, and needs to be installed before starting developing your application. We do not have to install it separately, because the Android Studio does it for us, when we make our first project.

Can we build apk without Android Studio?

You can use gradle commands depending on which variant you want. For example, ./gradlew installDebug will make a debug apk and load it onto a connected device (though it won't open it like Android Studio does). gradlew is the Gradle wrapper file present in the root of the project.

Is it possible to build apps without SDK?

It is not possible to build an Android app without using SDK.


2 Answers

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 from aapt

  • use javac to compile all java source to *.class

  • use dx to convert all *.class to dex file, e.g output is classes.dex

  • create initial version of APK from assets, resources and AndroidManfiest.mk, e.g output is MyApplication.apk.unaligned

  • use aapt to add classes.dex generated in step 3 to MyApplication.apk.unaligned

  • use jarsigner to sign MyApplication.apk.unaligned with debug or release key

  • use zipalign to align the final APK, e.g output is MyApplication-debug.apk or MyApplication-release.apk if signing with release key

  • Done

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/

like image 166
alijandro Avatar answered Sep 21 '22 09:09

alijandro


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:

  1. If you want to obfuscate classes you have to jar the compiled classes (between javac and dx steps) and run proguard on it

  2. For AdMob you have to extract the following jars from zip archives (like C:\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

  1. 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

like image 39
rupashka Avatar answered Sep 20 '22 09:09

rupashka