In nearly all languages that I've used (Python, C, C++, etc.), it's possible to write a "hello world" application with a text editor only and run it from command-line (interpreted languages) or compile/build it from command-line (compiled languages), e.g. cl.exe helloworld1.cpp
.
On the other hand, every time I'm doing an Android App, I need to use Android Studio (which is slow on my machine), create a new project with the IDE, etc.
Question: What is the smallest number of minimalist Java source code files/project files to produce an .apk Android app? How to build it from command-line? (and never have to open the IDE)
NB: I've read many hello world for Android but all of them involve using the IDE.
NB2: I'm looking for standard apps written in Java, not solutions like Kivy, etc.
NB3: even if an IDE is probably more convenient to program an Android app, I don't see any technical reason for which compiling/building a number of files would absolutely require an IDE / programming with a GUI. Some people (like me) prefer command-line and text editor only, and such an IDE-free solution would be helpful.
NB4: I'm working on Windows platform, I have started a "Hello World without IDE" github repo here based on this answer, but I have a few problems such as this one. On the other hand, the method used there seems to be deprecated...
Only these 2 files are required to build android Apk file
classes.dex
andAndroidManifest.xml
, check out this blog post for complete info .The smallest apk size made till now is 678 bytes.check out this repo
Instead of trying to figure this out, why not just create an empty project from Android Studio. It will have everything you need, and a gradle config that you can build via gradlew. Then save that somewhere on your hard drive and use a copy of that as the start of your new projects.
If you are finding android development too complex and want a simpler solution - I would suggest trying flutter.
You can write apps which are simpler to understand and will run on Android or the IPhone. It uses the language Dart which has many similarities to Java.
Here is a hello world tutorial which uses an IDE and command line in windows to build an android app.
https://www.youtube.com/watch?v=CEPCGXQ7IQg
Yes you can easily do it ALL from the command line (NO
IDE
involved, I promise).This uses the old faithful Apache Ant. It does not use
Gradle
, that takes more work.To Summarize
What you type is (just 2 lines to produce an apk):
(This produces an
Apache Ant
build file calledbuild.xml
file which is like thebuild.gradle
file. Now write some code but TestActivity.java is there already and will compile)Setup
(Note: The "
android.bat
" command is deprecated since Build Tools v26, so use an old one (see link below), deprecated in this case means TOTALLY removed !{naughty Google}).Install Java JDK if not installed already (you can use jdk-8u151-windows-x64.exe for example), and make sure
JAVA_HOME
environment variable is defined e.g.:JAVA_HOME=C:\Program Files\Java\jdk1.8.0_112
JAVA_PATH=C:\Program Files\Java\jre1.8.0_112\bin
JDK is the Java Development Kit.
JRE is the Java Run-time Environment.
Install
Android SDK Tools
(e.g. installer_r24.4.1-windows.exe, see this answer) if not already done, and then in the SDK Manager GUI, deselect everything and choose "Android SDK Build-Tools" (e.g.Android SDK Build-Tools 19.1
) + one (or many) platforms (e.g.Android 4.1.2 (API 16) JELLY_BEAN
). To prove you don't needAndroid Studio
, were not going to download it ! (only the SDK).Download Apache Ant (for example apache-ant-1.9.9-bin.zip)
Detail
To create a project from the command line using
Android SDK
:Decide on a place to put your project:
Run the command:
Here is the directory structure created (and all the files you need to build):
detailed output of create project:
Download Apache Ant from http://ant.apache.org/.
See this tutorial for setup:http://www.vogella.com/tutorials/ApacheAnt/article.html
Also see this tutorial:http://blog.vogella.com/2011/03/16/creating-android-applications-via-the-command-line-ant/
Write your code (Hello world).
Run this command and you get an Android Apk out the other side (called TestActivity-debug.apk):
Hey presto, you got an android apk !
With new structure added:
For a final build :
If your interested in a more extensive example of
Ant build.xml
, orDEX
files, and the deeper workings of Android look hereHow to sign an already compiled apk
See how to sign an already compiled apk and also this
From an answer by @for3st here's a relevant piece of that post:
Manual Process:
Step 1: Generate Keystore (only once)
You need to generate a keystore once and use it to sign your
unsigned
apk. Use thekeytool
provided by the JDK found in%JAVA_HOME%/bin/
Step 2 or 4: Zipalign
zipalign
which is a tool provided by the Android SDK found in e.g.%ANDROID_HOME%/sdk/build-tools/24.0.2/
is a mandatory optimization step if you want to upload the apk to the Play Store.Note: when using the old
jarsigner
you need tozipalign
AFTER signing. When using the newapksigner
method you do it BEFORE signing (confusing, I know). Invokingzipalign
beforeapksigner
works fine becauseapksigner
preservesAPK
alignment and compression (unlikejarsigner
).You can verify the alignment with:
Step 3: Sign & Verify
Using build-tools 24.0.2 and older
Use
jarsigner
which, like the keytool, comes with the JDK distribution found in%JAVA_HOME%/bin/
and use it like so:and can be verified with
Using build-tools 24.0.3 and newer
Android 7.0
introducesAPK Signature Scheme v2
, a new app-signing scheme that offers faster app install times and more protection against unauthorized alterations to APK files (See here and here for more details). Therefore,Google
implemented their ownapk
signer called:apksigner
(duh!) The script file can be found in%ANDROID_HOME%/sdk/build-tools/24.0.3/
(the .jar is in the/lib
subfolder). Use it like this:and can be verified with:
This is a ready-to-use method to build an Android app without having to install/open Android Studio even once (it's a cool IDE, but some people like me just prefer a text editor + command line). You'll get a fully signed/aligned APK at the end, ready to go on the Google Play Store.
Full credit to JonGoodwin's answer, I just summarized it here with a single
createproject.bat
batch that does everything.Prerequisites (see his answer for more details about them): you need to have these tools installed:
Java JDK
Android SDK Tools
Apache Ant
Now just modify the first lines of this
createproject.bat
to set your ownAPPNAME
,VENDORNAME
, and change the path ofadb.exe
,keytool.exe
, etc. according to your system.Then run
createproject.bat
. It will create a new folderhelloworld
. You'll find a few.bat
files there that will build your app!createproject.bat