28

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

Basj
  • 41,386
  • 99
  • 383
  • 673
  • How about this? https://blukat29.github.io/2016/03/building-android-app-without-an-ide/ – Nabin Bhandari Nov 10 '17 at 07:00
  • Thanks but when I tried to port some Linux code (like the link you shared) to windows, I had lots of small issues (more details about this in the question). – Basj Nov 10 '17 at 08:49

5 Answers5

23

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

    android create project --target "android-16" --path basj --activity TestActivity --package com.android.basj 

(This produces an Apache Ant build file called build.xml file which is like the build.gradle file. Now write some code but TestActivity.java is there already and will compile)

    ant debug

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}).

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

  1. 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 need Android Studio, were not going to download it ! (only the SDK).

  2. 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:

cd c:\android
mkdir antTest
cd antTest

Run the command:

C:\Android\sdk1\tools\android create project --target "android-16" --path basj --activity TestActivity --package com.android.basj 
              ^
              |
--------------+ (here's where I keep an old version of tools (version 25 in my case)

Here is the directory structure created (and all the files you need to build):

C:.
+---basj
    +---bin
    +---libs
    +---res
    ¦   +---drawable-hdpi
    ¦   +---drawable-ldpi
    ¦   +---drawable-mdpi
    ¦   +---drawable-xhdpi
    ¦   +---layout
    ¦   +---values
    +---src
        +---com
            +---android
                +---basj

detailed output of create project:

Created project directory: C:\Android\antTest\basj
Created directory C:\Android\antTest\basj\src\com\android\basj
Added file C:\Android\antTest\basj\src\com\android\basj\TestActivity.java
Created directory C:\Android\antTest\basj\res
Created directory C:\Android\antTest\basj\bin
Created directory C:\Android\antTest\basj\libs
Created directory C:\Android\antTest\basj\res\values
Added file C:\Android\antTest\basj\res\values\strings.xml
Created directory C:\Android\antTest\basj\res\layout
Added file C:\Android\antTest\basj\res\layout\main.xml
Created directory C:\Android\antTest\basj\res\drawable-xhdpi
Created directory C:\Android\antTest\basj\res\drawable-hdpi
Created directory C:\Android\antTest\basj\res\drawable-mdpi
Created directory C:\Android\antTest\basj\res\drawable-ldpi
Added file C:\Android\antTest\basj\AndroidManifest.xml
Added file C:\Android\antTest\basj\build.xml
Added file C:\Android\antTest\basj\proguard-project.txt

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

ant debug

Hey presto, you got an android apk !
With new structure added:

C:.
├───bin
│   ├───classes
│   │   └───com
│   │       └───android
│   │           └───basj
│   ├───dexedLibs
│   └───res
│       ├───drawable-hdpi
│       ├───drawable-ldpi
│       ├───drawable-mdpi
│       └───drawable-xhdpi
├───gen
│   └───com
│       └───android
│           └───basj

For a final build :

ant release

If your interested in a more extensive example of Ant build.xml, or DEX files, and the deeper workings of Android look here

How 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 the keytool provided by the JDK found in %JAVA_HOME%/bin/

keytool -genkey -v -keystore my.keystore -keyalg RSA -keysize 2048 -validity 10000 -alias app

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.

zipalign -p 4 my.apk my-aligned.apk

Note: when using the old jarsigner you need to zipalign AFTER signing. When using the new apksigner method you do it BEFORE signing (confusing, I know). Invoking zipalign before apksigner works fine because apksigner preserves APK alignment and compression (unlike jarsigner).

You can verify the alignment with:

zipalign -c 4 my-aligned.apk

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:

jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my.keystore my-app.apk my_alias_name

and can be verified with

jarsigner -verify -verbose my_application.apk

Using build-tools 24.0.3 and newer

Android 7.0 introduces APK 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 own apk 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:

apksigner sign --ks my.keystore my-app.apk --ks-key-alias alias_name

and can be verified with:

apksigner verify my-app.apk
Jon Goodwin
  • 9,053
  • 5
  • 35
  • 54
  • Wonderful, I'm going to test this right now! Since I didn't know Apache Ant, here is the answer to "What is Apache Ant?" from its FAQ: *Ant is a Java-based build tool. In theory, it is kind of like Make, without Make's wrinkles and with the full portability of pure Java code.* – Basj Nov 12 '17 at 17:22
  • 1
    I built it just now to make sure it works, let me know if you have problems with paths, but it's quite straight forward. – Jon Goodwin Nov 12 '17 at 17:23
  • Wonderful! A small last issues (just in case you have an idea @JonGoodwin): 1) Debug works on phone, but when doing release and putting `TestActivity-release-unsigned.apk` on phone, I get "There is a problem parsing the package" ("Unknown sources" is already enabled in my phone's settings). 2) Is it also possible to sign the package from command-line in one or two lines? (ready to go to GooglePlay store). 3) Even if `AndroidManifest.xml` and no file in TestActivity doesn't mention it, it requires "Read phone status and identity" by default (maybe `READ_PHONE_STATE`?), how to remove it? – Basj Nov 13 '17 at 10:13
  • 2
    Morning. I'm not sure how you are "sideloading" the apk, but if you delete debug on the phone first, then the release should work (I'll check it later). It takes about six commands to sign the package from my memory, I'll find the commands (and the tools later), but you can search for them with "android manual signing". "Read phone status and identity", I will check. But you built the apk, well done. Hmmm you have +8 and -2 votes on your question, how strange, it's a good question. – Jon Goodwin Nov 13 '17 at 10:33
  • 1
    @JonGoodwin, you win the internet, sir. –  Dec 22 '17 at 17:17
  • 1
    Thanks once again @JonGoodwin for your wonderful answer. I created a `createproject.bat` batch that does everything [in this answer](https://stackoverflow.com/a/49959609/1422096). – Basj Apr 21 '18 at 19:49
  • Damn, google really had to cripple the ecosystem – Rainb Dec 25 '22 at 20:56
6

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 own APPNAME, VENDORNAME, and change the path of adb.exe, keytool.exe, etc. according to your system.

Then run createproject.bat. It will create a new folder helloworld. You'll find a few .bat files there that will build your app!


createproject.bat

@echo off
SET APPNAME=helloworld
SET VENDORNAME=demo
SET SIGNKEYPASSWORD=abcdef12
SET ANTBATPATH="..\..\apache-ant-1.9.9-bin\bin\ant.bat"
SET ADBPATH="C:\Users\User\AppData\Local\Android\android-sdk\platform-tools\adb.exe"
SET KEYTOOLPATH="C:\Program Files\Java\jdk1.8.0_151\bin\keytool.exe"
SET JARSIGNERPATH="C:\Program Files\Java\jdk1.8.0_151\bin\jarsigner.exe"
SET ZIPALIGNPATH="C:\Users\User\AppData\Local\Android\android-sdk\build-tools\19.1.0\zipalign.exe"
SET ANDROIDBATPATH="C:\Users\User\AppData\Local\Android\android-sdk\tools\android.bat"

call %ANDROIDBATPATH% create project --target "android-16" --path %APPNAME% --activity %APPNAME% --package com.%VENDORNAME%.%APPNAME%

(
echo call %ANTBATPATH% debug
echo copy bin\*debug.apk .
) > %APPNAME%\1makedebug.bat


echo %ADBPATH% logcat > %APPNAME%\2adb_logcat.bat
echo %ADBPATH% devices > %APPNAME%\2adb_devices.bat
echo %ADBPATH% install -r %APPNAME%-debug.apk > %APPNAME%\2adb_install.bat
echo %ADBPATH% shell > %APPNAME%\2adb_shell.bat

(
echo call %ANTBATPATH% release
echo %JARSIGNERPATH% -sigalg SHA1withRSA -digestalg SHA1 -keystore my.keystore "bin\%APPNAME%-release-unsigned.apk" app -storepass %SIGNKEYPASSWORD%
echo %ZIPALIGNPATH% 4 "bin\%APPNAME%-release-unsigned.apk" "%APPNAME%-release.apk"
echo pause
) > %APPNAME%\3makerelease_signed.bat

%KEYTOOLPATH% -genkey -keystore %APPNAME%\my.keystore -keyalg RSA -keysize 2048 -validity 10000 -alias app -dname "cn=Unknown, ou=Unknown, o=Unknown, c=Unknown" -storepass %SIGNKEYPASSWORD% -keypass %SIGNKEYPASSWORD%

echo Finished.
pause
Community
  • 1
  • 1
Basj
  • 41,386
  • 99
  • 383
  • 673
2

What is the smallest number of minimalist Java source code files/project files to produce an .apk Android app?

Only these 2 files are required to build android Apk file classes.dex and AndroidManifest.xml , check out this blog post for complete info .

The smallest apk size made till now is 678 bytes.check out this repo

Manohar
  • 22,116
  • 9
  • 108
  • 144
  • 3
    Thanks @Redman. In fact, the question is not only "how many" but can you show such a minimalist example? How to build it via command-line (without IDE)? – Basj Nov 14 '17 at 13:36
  • I have not built it , but in the blog post there is a section "Where we’re going, we don’t need IDEs" , check it out. – Manohar Nov 14 '17 at 13:46
  • Would be cool if you can put a working reproducible method in a few steps, in this answer: not all this article is needed (we don't need all the optimization for small apk), but only how to go from java + manifest to an APK. It would be a valuable info for many people :) – Basj Nov 14 '17 at 13:51
1

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.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • 2
    Not what I expected :') – Joachim Huet Nov 07 '17 at 21:41
  • @JoachimHuet Consider it the pragmatist approach. Technically, an Android app only requires a manifest and a single Java file (for an activity, assuming you want to actually have it print hello world. Technically the activity is optional). But building it is so much easier leveraging the system than figuring out how to do all the stuff gradle and the android build system do for you. – Gabe Sechan Nov 07 '17 at 21:43
  • @GabeSechan how would it be possible to have just manifest.xml and main.java and build an apk from them in command line? – Basj Nov 07 '17 at 21:55
  • @Basj the instant you need a single non-Java resource like a xml layout file or an image/icon your are going to need to start packaging resources, see: https://developer.android.com/studio/build/index.html, as well as the gradle console in AS for all of the tasks run. In the past you could have created a empty Android project from the command line but that capability has been removed and now only from Android Studio can you create a empty Android project (gradle setup, directory setup, placeholder resources, etc.) – Morrison Chang Nov 08 '17 at 05:39
  • 2
    I see your point @MorrisonChang, but still, there is no *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/ – Basj Nov 08 '17 at 08:48
  • I completely agree with @GabeSechan answer. If you go the old school way, you would need to install each and every tool needed to build you app. Checkout this link which teaches you how to build the app from command line: https://developer.android.com/studio/build/building-cmdline.html – cyberrspiritt Nov 11 '17 at 07:33
-2

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

Rockvole
  • 4,422
  • 3
  • 21
  • 27