Android application "Hello world" with a minimum number of files, no IDE and text editor

In almost all the languages ​​that I used (Python, C, C ++, etc.), you can write the hello world application only with a text editor and run it from the command line (interpreted languages) or compile / build from the command strings (compiled languages), for example cl.exe helloworld1.cpp .

On the other hand, every time I make an Android application, I need to use Android Studio (which is slow on my machine), create a new project with an IDE, etc.

Question: What is the minimum number of minimalistic Java source code files / project files for creating an .apk application for Android? How to create it from the command line? (and never need to open an IDE)

NB: I read a lot of hello world for Android , but they are all related to using the IDE.

NB2: I'm looking for standard Java applications, not solutions like Kivy, etc.

NB3: even if the IDE is probably more convenient for programming an Android application, I see no technical reasons why compiling / creating multiple files will require developing an IDE / programming using a graphical interface. Some people (like me) prefer just a command and text editor, and such a solution without an IDE would be useful.

NB4: I work on the Windows platform, I started here "Hello, a world without an IDE" here, based on this answer , but I have a few problems, like this one . On the other hand, the method used there seems outdated ...

+18
java android windows android-studio android-gradle
Nov 07 2018-11-17T00:
source share
5 answers

Yes, you can easily do it ALL from the command line (I do not agree with the IDE proposal).
It uses the old faithful Apache Ant . It uses no Gradle , which requires more work.

To summarize

What you type ( total 2 lines to create apk):

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

(This creates an Apache Ant build file called build.xml , which is similar to build.gradle . Now write some code, but TestActivity.java already exists and will be compiled)

  ant debug 

Customization

( Note : the <<27> command has been deprecated since Build Tools v26 , so use the old one (see link below), deprecated in this case means TOTALLY deleted! { Mischievous Google}).

  • Install the Java JDK if it is not already installed (for example, you can use jdk-8u151-windows-x64.exe), and make sure the JAVA_HOME environment variable is defined , for example:

    JAVA_HOME = C: \ Program Files \ Java \ jdk1.8.0_112
    JAVA_PATH = C: \ Program Files \ Java \ jre1.8.0_112 \ bin

JDK is a Java Development Kit.
JRE is a Java runtime.

  1. Install the Android SDK Tools (for example, installer_r24.4.1-windows.exe , see this answer ) if it is not already done, then deselect in the SDK Manager GUI and select β€œAndroid SDK Build-Tools” (for example, Android SDK Build-Tools 19.1 ) + one (or many) platforms (e.g. Android 4.1.2 (API 16) JELLY_BEAN ). To prove that you do not need Android Studio , you are not going to download it! (SDK only).

  2. Download Apache Ant (e.g. apache-ant-1.9.9-bin.zip )

Detail

To create a project from the command line using the Android SDK :

Determine the location for 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 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 collect):

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

detailed output of the creation 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 customization: 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 will get Android Apk on the other hand (called TestActivity-debug.apk):

 ant debug 

Hey presto, you have android apk!
New structure added:

 C:. β”œβ”€β”€β”€bin β”‚ β”œβ”€β”€β”€classes β”‚ β”‚ └───com β”‚ β”‚ └───android β”‚ β”‚ └───basj β”‚ β”œβ”€β”€β”€dexedLibs β”‚ └───res β”‚ β”œβ”€β”€β”€drawable-hdpi β”‚ β”œβ”€β”€β”€drawable-ldpi β”‚ β”œβ”€β”€β”€drawable-mdpi β”‚ └───drawable-xhdpi β”œβ”€β”€β”€gen β”‚ └───com β”‚ └───android β”‚ └───basj 

For final assembly:

 ant release 

If you are interested in a more extensive example of Ant build.xml or DEX files, and deeper Android work look here

How to sign an already compiled apk

See how to sign an already compiled apk , as well as this
From @ for3st's answer here you can find the relevant part of this post:

Manual process:

Step 1: Generate Keystore (only once)

You need to create a keystore once and use it to sign 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 in particular %ANDROID_HOME%/sdk/build-tools/24.0.2/ is a mandatory optimization step if you want to download 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 apksigner you do this before you sign up (confusing, I know). Calling zipalign before apksigner works fine because apksigner preserves APK alignment and compression (unlike jarsigner ).

You can check the alignment with:

 zipalign -c 4 my-aligned.apk 

Step 3: Sign and Confirm

Using built-in tools 24.0.2 and later

Use jarsigner , which, like keytool, comes with the JDK distribution found in %JAVA_HOME%/bin/ and use it like this:

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

and can be checked with

 jarsigner -verify -verbose my_application.apk 

Using built-in tools 24.0.3 and newer

Android 7.0 introduces APK Signature Scheme v2 new application subscription scheme that offers faster application installation time and greater protection against unauthorized changes in APK files (see here and here for more details). Therefore, Google implemented its own APK subscriber: apksigner (duh!) The script file can be found in %ANDROID_HOME%/sdk/build-tools/24.0.3/ (.jar is located in the /lib subfolder). Use it like this:

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

and can be checked with:

 apksigner verify my-app.apk 
+16
Nov 12 '17 at 17:16
source share

What is the minimum amount of minimalist Java source / project files for creating an .apk application for Android?

To create the Android file Apk classes.dex and AndroidManifest.xml only these 2 files are required, check this blog post for full details.

The smallest apk size made so far is 678 bytes. Bookmark this repo

+2
Nov 14 '17 at 13:19
source share

This is a ready-to-use method for creating an Android application without having to install / open Android Studio at least once (this is a cool IDE, but some people like me just prefer a text editor + command line). You will receive a fully signed / aligned APK at the end, ready to go to the Google Play Store.

Full answer to JonGoodwin's answer , I just summarized it here with one createproject.bat which does everything.

Prerequisites (see His answer for more information about them): you need to install these tools:

  • Java JDK

  • Android SDK Tools

  • Apache ant

Now just change the first lines of this createproject.bat to set your own APPNAME , VENDORNAME and change the path to adb.exe , keytool.exe , etc. According to your system.

Then run createproject.bat . He will create a new helloworld folder. Here you will find several .bat files that will create your application!




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 
+2
Apr 21 '18 at 19:44
source share

Instead of trying to figure it out, why not just create an empty project from Android Studio. It will have everything you need and a gradle configuration that you can build with gradlew. Then save it somewhere on your hard drive and use a copy of it as the start of your new projects.

+1
Nov 07 '17 at 21:40
source share

If you find that Android development is too complicated and requires a simpler solution, I would suggest trying a thrill.

You can write applications that are easier to understand and will run on Android or iPhone. It uses the Dart language, which has a lot in common with Java.

Here's a welcome world that uses the IDE and command line in Windows to create an Android app.

https://www.youtube.com/watch?v=CEPCGXQ7IQg

0
Apr 21 '18 at 22:13
source share



All Articles