Hello world using the Android SDK alone (no IDE) -
my aims to:
- test basic development tools on simple program
- expand program useful app
i prefer work small, independent tools opposed ides. prefer code in procedural or imperative style (plain old java) opposed declarative (xml).
i installed stand-alone android sdk as instructed. have necessary minimum of other tools (text editor, command shell , jdk). starting instructions can find tied android studio, eclipse or other ides. can't follow them.
how can write java program text editor display "hello world" on android device? how can test using sdk emulator? please give me instructions.
these instructions worked me. got them deconstructing google's ant script, on rob's answer based.
the following content "android programming without ide" stack overflow documentation (archived here); copyright 2017 geekygenius, michael allan, cascal, doron behar, mnoronha, , androidmechanic; licensed under cc by-sa 3.0. archive of full stack overflow documentation content can found @ archive.org, in example indexed topic id: 85, example: 9496.
this minimalist hello world example uses basic android tools.
requirements , assumptions
- oracle jdk 1.7 or later
- android sdk tools (just command line tools)
this example assumes linux. may have adjust syntax own platform.
setting android sdk
after unpacking sdk release:
install additional packages using sdk manager. don't use
android update sdk --no-ui
instructed in bundled readme.txt; downloads 30 gb of unnecessary files. instead use interactive sdk managerandroid sdk
recommended minimum of packages.append following jdk , sdk directories execution path. optional, instructions below assume it.
- jdk/bin
- sdk/platform-tools
- sdk/tools
- sdk/build-tools/latest (as installed in step 1)
create android virtual device. use interactive avd manager (
android avd
). might have fiddle bit , search advice; on-site instructions aren't helpful.(you can use own device)
run device:
emulator -avd device
if device screen appears locked, swipe unlock it.
leave running while code app.
coding app
change empty working directory.
make 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 ); } }
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>
make sub-directory declared resources:
mkdir res
leave empty now.
building code
generate source resource declarations. substitute here correct path sdk, , installed api build against (e.g. "android-23"):
aapt package -f \ -i sdk/platforms/android-api/android.jar \ -j src -m \ -m androidmanifest.xml -s res -v
resource declarations (described further below) optional. meantime above call nothing if res/ still empty.
compile source code java bytecode (.java → .class):
javac \ -bootclasspath sdk/platforms/android-api/android.jar \ -classpath src -source 1.7 -target 1.7 \ src/dom/domain/*.java
translate bytecode java android (.class → .dex):
first using 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 called "dalvik executable code", , "dex".
you replace steps 11 , 12 single call jack if like; can compile directly java source (.java → .dex). there advantages compiling
javac
. it's better known, better documented , more applicable tool.package resource files, including manifest:
aapt package -f \ -f app.apkpart \ -i sdk/platforms/android-api/android.jar \ -m androidmanifest.xml -s res -v
that results in partial apk file (android application package).
make full apk using
apkbuilder
tool:java -classpath sdk/tools/lib/sdklib.jar \ com.android.sdklib.build.apkbuildermain \ app.apkunalign \ -d -f classes.dex -v -z app.apkpart
it warns, "this tool deprecated. see --help more information." if
--help
failsarrayindexoutofboundsexception
, instead pass no arguments:java -classpath sdk/tools/lib/sdklib.jar \ com.android.sdklib.build.apkbuildermain
it explains cli (
apkbuildermain
) deprecated in favour of directly calling java api (apkbuilder
). (if know how command line, please update example.)optimize data alignment of apk (recommended practice):
zipalign -f -v 4 app.apkunalign app.apk
installing , running
install app android device:
adb install -r app.apk
start app:
adb shell start -n dom.domain/.sayinghello
it should run , hello.
that's all. that's takes hello using basic android tools.
declaring resource
this section optional. resource declarations aren't required simple "hello world" app. if aren't required app either, streamline build omitting step 10, , removing reference res/ directory step 13.
otherwise, here's brief example of how declare resource, , how reference it.
add resource file:
mkdir res/values touch res/values/values.xml
content:
<?xml version='1.0'?> <resources> <string name='applabel'>saying hello</string> </resources>
reference resource xml manifest. declarative style of reference:
<!-- <application a:label='saying hello'> --> <application a:label='@string/applabel'>
reference same resource java source. imperative reference:
// v.settext( "hello world" ); v.settext( "this app called " + getresources().getstring( r.string.applabel ));
test above modifications rebuilding, reinstalling , re-running app (steps 10-17).
it should restart , say, "this app called saying hello".
uninstalling app
adb uninstall dom.domain
see also
- working example - working build script uses above commands
Comments
Post a Comment