Hello world using the Android SDK (no IDE)

My goals are as follows:

  • Test basic development tools on a simple program
  • Deploy the program to a useful application

I prefer working with small independent tools, rather than with the IDE. I prefer to code a procedural or imperative style (plain old Java) rather than declarative (XML).

I installed the standalone Android SDK according to the instructions . I have the required minimum of other tools (text editor, shell, and JDK). But the only initial instructions I can find are tied to Android Studio, Eclipse, or other IDEs. I can not follow them.

How can I write a Java program with my text editor to display "Hello world" on an Android device? How can I test it using the SDK emulator? Please give me instructions.

+11
android android-build
Mar 21 '15 at 2:22
source share
3 answers

These are the instructions that ultimately worked for me. I got them by deconstructing the Google Ant script that Rob's answer is based on.




The following content from “Android programming without an IDE” from the documentation ( archived here ); copyright 2017 by geekygenius , Michael Allan , cascal , Doron Bejar , mnoronha and AndroidMechanic ; licensed under CC BY-SA 3.0. Full archive The contents of the documentation can be found on archive.org, where the example is indexed by its topic ID: 85, for example: 9496.

This is a minimalist Hello World example that uses only the most basic tools for Android.

Requirements and Assumptions

This example assumes Linux. You may need to configure the syntax for your own platform.

Android SDK setup

After unpacking the SDK version:

  • Install additional packages using the SDK manager. Do not use android update sdk --no-ui , as indicated in Readme.txt; it downloads about 30 GB of junk files. Instead, use the android sdk interactive SDK manager to get the recommended minimum packages.

  • Attach the following JDK and SDK directories to your PATH. This is optional, but the instructions below suggest.

    • Jdk / bin
    • SDK / platform tools
    • SDK / tools
    • SDK / build-tools / LATEST (as installed in step 1)
  • Create an Android virtual device. Use the interactive AVD Manager ( android avd ). You may need to play a little and find advice; on-site instructions are not always helpful.

    (You can also use your own device)

  • Launch the device:

     emulator -avd DEVICE 
  • If the device’s screen is locked, swipe the screen to unlock it.

    Leave it turned on while programming the application.

Application coding

  1. Change to the empty working directory.

  2. Make the source file:

     mkdir --parents src/dom/domain touch src/dom/domain/SayingHello.java 

    Content:

     package dom.domain; import android.widget.TextView; public final class SayingHello extends android.app.Activity { protected @Override void onCreate( final android.os.Bundle activityState ) { super.onCreate( activityState ); final TextView textV = new TextView( SayingHello.this ); textV.setText( "Hello world" ); setContentView( textV ); } } 
  3. Add manifest:

     touch AndroidManifest.xml 

    Content:

     <?xml version='1.0'?> <manifest xmlns:a='http://schemas.android.com/apk/res/android' package='dom.domain' a:versionCode='0' a:versionName='0'> <application a:label='Saying hello'> <activity a:name='dom.domain.SayingHello'> <intent-filter> <category a:name='android.intent.category.LAUNCHER'/> <action a:name='android.intent.action.MAIN'/> </intent-filter> </activity> </application> </manifest> 
  4. Create a subdirectory for the declared resources:

     mkdir res 

    Leave it blank.

Code building

  1. Generate source for resource declarations. Replace here the correct path to the SDK , and the installed API for the build against (for example, "android-23"):

     aapt package -f \ -I SDK/platforms/android-API/android.jar \ -J src -m \ -M AndroidManifest.xml -S res -v 

    Resource declarations (described below) are actually optional. At the same time, the above call does nothing if res / is still empty.

  2. Compile the source code into Java bytecode (.java → .class):

     javac \ -bootclasspath SDK/platforms/android-API/android.jar \ -classpath src -source 1.7 -target 1.7 \ src/dom/domain/*.java 
  3. Translate bytecode from Java to Android (.class → .dex):

    First use Jill (.class → .jayce):

     java -jar SDK/build-tools/LATEST/jill.jar \ --output classes.jayce src 

    Then Jack (.jayce → .dex):

     java -jar SDK/build-tools/LATEST/jack.jar \ --import classes.jayce --output-dex . 

    Android bytecode used to be called "Dalvik executable code", and therefore "dex".

    You can replace steps 11 and 12 with one call to Jack if you want; it can compile directly from a Java source (.java → .dex). But there are advantages to compiling with javac . This is a better known, well-documented and more widely used tool.

  4. Package of resource files, including manifest:

     aapt package -f \ -F app.apkPart \ -I SDK/platforms/android-API/android.jar \ -M AndroidManifest.xml -S res -v 

    This results in a partial APK file (Android application package).

  5. Make a full APK with the ApkBuilder tool:

     java -classpath SDK/tools/lib/sdklib.jar \ com.android.sdklib.build.ApkBuilderMain \ app.apkUnalign \ -d -f classes.dex -v -z app.apkPart 

    He warns: "THIS TOOL IS REMOVED. See" Help "for more information." If --help fails with an ArrayIndexOutOfBoundsException , then do not pass arguments instead:

     java -classpath SDK/tools/lib/sdklib.jar \ com.android.sdklib.build.ApkBuilderMain 

    It explains that the CLI ( ApkBuilderMain ) is deprecated in favor of calling the Java API ( ApkBuilder ) ApkBuilder . (If you know how to do this from the command line, update this example.)

  6. Optimize APK Data Alignment ( Recommended Practice ):

     zipalign -f -v 4 app.apkUnalign app.apk 

Installation and launch

  1. Install the application on an Android device:

     adb install -r app.apk 
  2. Launch the application:

     adb shell am start -n dom.domain/.SayingHello 

    He should start up and say hi.

It's all. This is what you need to say hello to the basic Android tools.

Resource declaration

This section is optional. Resource applications are not required for the simple hello world application. If they are not required for your application, then you can simplify the assembly by omitting step 10 and removing the link to the res / directory from step 13.

Otherwise, here is a brief example of how to declare a resource, and how to reference it.

  1. Add resource file:

     mkdir res/values touch res/values/values.xml 

    Content:

     <?xml version='1.0'?> <resources> <string name='appLabel'>Saying hello</string> </resources> 
  2. Resource reference from an XML manifest. This is a declarative link style:

     <!-- <application a:label='Saying hello'> --> <application a:label='@string/appLabel'> 
  3. Link to the same resource from a Java source. This is a strong recommendation:

     // v.setText( "Hello world" ); v.setText( "This app is called " + getResources().getString( R.string.appLabel )); 
  4. Test the above changes by rebuilding, reinstalling, and re-executing the application (steps 10-17).

    He should restart and say: "This application is called Saying hello."

Uninstall application

 adb uninstall dom.domain 

see also

+9
Mar 28 '15 at 3:34
source share

First, do not seriously consider using an emulator. Unless you just want to obey unnecessary torture. For those who do not want IDE baggage, the emulator is 100 times worse. Get a device will be advice on this.

You cannot refuse XML. I understand and appreciate the momentum that I had was similar. However, I eventually fell in love with him. Use styles a lot. I would recommend using Android Studio. It has a great tool for creating code and markup the interface.

Even if you just want to copy the code from the editor, you can use Android Studio to make your project a stub. This is pretty good. In case you did not know this, in the documents there is a command line method for creating a project (without using AS): he documented it here .

+2
Mar 21 '15 at 2:50
source share

Very helpful post. I made a simple brightness installer using your very good instructions. It’s just a pity that I can’t find the arguments for just one Jack. It was possible to place everything in one directory, except for the icon that needs to be found in res \ drawable-hdpi.

 javac -bootclasspath c:\android\SDK/platforms/android-19/android.jar -classpath . *.java java -jar c:\android\SDK/build-tools/24.0.1/jill.jar --output classes.jayce . java -jar c:\android\SDK/build-tools/24.0.1/jack.jar --import classes.jayce --output-dex . aapt package -f -F app.apkPart -I c:\android\SDK/platforms/android-19/android.jar -M AndroidManifest.xml -S res -v java -classpath c:\android\SDK/tools/lib/sdklib.jar com.android.sdklib.build.ApkBuilderMain app.apkUnalign -d -f classes.dex -v -z app.apkPart zipalign -f -v 4 app.apkUnalign brite.apk <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="dom.domain" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission> <application android:allowBackup="true" android:icon="@drawable/slkimage" android:label="brite" > <activity android:name="dom.domain.MainActivity" android:label="brite" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 
+1
Aug 14 '16 at 15:59
source share



All Articles