Android

From YobiWiki
Jump to navigation Jump to search

Links

App stores

Alternate views on the official market:

Alternate markets:

User manuals

Nexus S

Versions

physical sticker behind battery

  • Model: GT-I9023
  • FCC ID: A3LGTI9023
  • SSN: -I9023GSMH
  • IMEI: xxxxxxx
  • S/N: xxxxxxx

under fastboot, stock

  • Product name - HERRING
  • HW Version - rev 52
  • Bootloader version - I9020XXKA3
  • Baseband version - I9020XXKB3
  • Carrier info - EUR
  • Serial number - xxxxxxx

under fastboot, after upgrade to 2.3.4

  • Baseband version - I9020XXKD1
  • Carrier info - EUR

under 'About phone' from the settings, stock 2.3.3

  • Android 2.3.3
  • Baseband I9023XXKB3
  • Kernel 2.6.35.7-g1d030a7
  • Build GRI54

under 'About phone' from the settings, after upgrade to 2.3.4

  • Android 2.3.4
  • Baseband I9023XXKD1
  • Kernel 2.6.35.7-ge382d80 android-build@apa28 #1
  • Build GRJ22

USB permissions on the host

On the host, example how to solve permissions:
Create /etc/udev/rules.d/99-android.rules for Nexus phones:

SUBSYSTEMS=="usb", ATTRS{idVendor}=="0bb4", ATTRS{idProduct}=="0fff", MODE="0666", OWNER="<your_account>" # Nexus One Fastboot
SUBSYSTEMS=="usb", ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="4e11", MODE="0666", OWNER="<your_account>" # Nexus One Normal
SUBSYSTEMS=="usb", ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="4e12", MODE="0666", OWNER="<your_account>" # Nexus One Debug/Recovery
SUBSYSTEMS=="usb", ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="4e20", MODE="0666", OWNER="<your_account>" # Nexus S Fastboot
SUBSYSTEMS=="usb", ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="4e21", MODE="0666", OWNER="<your_account>" # Nexus S Normal
SUBSYSTEMS=="usb", ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="4e22", MODE="0666", OWNER="<your_account>" # Nexus S Debug/Recovery
SUBSYSTEMS=="usb", ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="4e23", MODE="0666", OWNER="<your_account>" # ???
SUBSYSTEMS=="usb", ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="4e24", MODE="0666", OWNER="<your_account>" # Nexus S USB tethering
SUBSYSTEMS=="usb", ATTRS{idVendor}=="0bb4", ATTRS{idProduct}=="0c8b", MODE="0666", OWNER="<your_account>" # HTC Normal

Then execute /etc/init.d/udev reload

Restoring factory ROM (2.3.3)

Warning, it will destroy everything, make your backups first!!

  • Get firmware here for a European Nexus S i9023, mine needs the I9023XXKB3 one.
  • Rename tar.md5 as tar
  • Get Odin sw from here, choose i9023
    • e.g. I9003_Odin3_v1.82.rar & SAMSUNG_USB.rar
  • Run Odin (works in a virtualbox if needed), load the 4 files:
    • bootloader: I9023_EUR_GRI54_XXKB3/I9023_EUR_GRI54_XXKB3/Bootloader_I9023XXKA3.tar
    • PDA: I9023_EUR_GRI54_XXKB3/I9023_EUR_GRI54_XXKB3/PDA_SOJU_GRI54_TMO_EUR_MR1_SIGNED.tar
    • Phone: I9023_EUR_GRI54_XXKB3/I9023_EUR_GRI54_XXKB3/MODEM_I9023XXKB3_REV_00_CL912571_SIGNED.tar
    • CSC: I9023_EUR_GRI54_XXKB3/I9023_EUR_GRI54_XXKB3/DGS_I9023_EUR.tar
  • Turn phone off
  • Plug USB
  • Just before battery icon showing, enter download mode by pressing middle of volume up / volume down button for long, you'll get a big yellow warning triangle
  • If using virtualbox, bring USB device (Samsung serial) to virtualbox
  • Now Odin should show a yellow rectangle with COM0
  • Press "start" in Odin

Sources:

Restoring (most of) factory ROM (2.3.3) with fastboot

Ok previous section was about restoring *everything* as genuine but it requires Windows and most of the time all you screwed up was the boot image, the recovery image or the system image so here is how to restore them or part of them provided that you can still enter fastboot:

fastboot oem unlock                  # if needed
fastboot flash recovery recovery.img # from Samsung PDA_SOJU_GRI54_TMO_EUR_MR1_SIGNED.tar
fastboot flash system system.img     # from Samsung PDA_SOJU_GRI54_TMO_EUR_MR1_SIGNED.tar
fastboot flash boot boot.img         # from Samsung PDA_SOJU_GRI54_TMO_EUR_MR1_SIGNED.tar

Warning: I got /sdcard content erased, maybe because I tried to enter the native recovery, but anyway, make backups first before trying anything you risk regretting!!!

E.g. to make a backup with clockwordmod:

  • Put phone into fastboot mode
  • Enter recovery
  • Choose backup & restore / backup
  • Choose mount / mount USB storage
  • From host: copy clockwordmod/backup/*

Entering fastboot mode

  • Power off phone
  • Press Volume up and power button together for long
  • You've entered fastboot
  • Alternatively, fastboot can be triggererd from adb: adb reboot-bootloader

For getting fastboot on the host, see further below

Rooting 2.3.3

!! Don't touch boot image or try CW 3.0.0.0, one of them caused the phone to not start android anymore, I had to perform the full factory restore with Odin !!

  • enter recovery mode from fastboot
    • You can make a backup now
    • install zip from sdcard -> choose -> su-2.3.6.1-ef-signed.zip
    • reboot

Sources:

Upgrading to 2.3.4

A new version was announced (fr) two days ago.
Official way is apparently to type "*#*#2432546#*#*" while using Wi-Fi but all I got was a "checkin succeeded" notification. Anyway patched won't apply cleanly on my rooted phone so better to do it manually.
For GRI54, update.zip is available here.
I tried to apply it but there are several caveats given the previous hacks:

  • boot.img: to be able to patch it I've to restore the original boot.img, loosing temporarily the ro.secure=0 setting (cf "adb as root" paragraph). And failing to patch it would probably mean non-bootable as we would have missed replacing the kernel!
  • radio.img: hash checksum failed, it seems to indicate that expected radio.img is not the one I have.
  • recovery.img: we want to keep the clockworkmod one, so we just skip it for now

To apply those change this means:

  • fastboot flash boot boot.img (from PDA_SOJU_GRI54_TMO_EUR_MR1_SIGNED.tar)
  • edit update.zip to remove radio.img, recovery/ and edit META-INF/com/google/android/updater-script
    • remove all commands about radio & recovery patch
    • add following line to keep rooted: set_perm(0, 0, 6755, "/system/bin/su");
    • if that line is not added, patched phone will not be rooted anymore, which can be easily fixed by applying the su..zip again
  • upload update.zip to /sdcard and apply zip via clockworkmod recovery, it'll skip signature verification by default

Ok now we got a system & boot images upgraded to 2.3.4
We can again modify boot.img to restore ro.secure=0:

  • Extract patched 2.3.4 boot.img (cf below, or use clockworkmod), modify it & flash it back

I also wanted to patch the stock recovery image, just to get a 2.3.4 stock recovery in case of.

  • Install the 2 files from update.zip#recovery/ into /system and chmod 755 /system/etc/install-recovery-sh
  • Restore the stock 2.3.3 recovery.img from PDA_SOJU_GRI54_TMO_EUR_MR1_SIGNED.tar: adb push recovery.img /sdcard/
  • If you've flash_image on the phone you can try: flash_image recovery /sdcard/recovery.img
  • Else:
fastboot erase recovery
fastboot flash recovery recovery.img
  • Reboot the phone & start Android
  • It should be done, recovery should have been patched, you can remove the 2 files we've put in /system and extract the patched recovery image manually (cp /dev/mtd/mtd3 /sdcard/recovery.img). Hash changed so I assume it's properly patched
  • Restore clockworkmod recovery image

What's new?
I didn't mention it but with 2.3.3 I had two problems I was still busy trying to solve: GPS never fixing, zero satellite! And no way to see I get copy-protected applications (and that's not because of rooting the phone, on a stock phone it failed too).
Now GPS fixed quite fast with 2.3.4 and I could for the first time see & download copy-protected apps \o/ (even when the phone was rooted again).

UPDATE
here is the full ROM update, apparently with a new radio which should be ok for all phones...
Seen in this thread.

Upgrading to 2.3.6

I saw there was also newer versions of ClockworkMod, probably better than the preview release I was still using.
Latest ClockworkMod recoveries are here.
For Nexus S, look for "crespo" img

  • Go to fastboot (vol-up + power)
  • Go to recovery
  • Backup & restore / Backup
  • Mount USB
  • Copy all /sdcard content to PC
  • Reboot -> enter fastboot again
  • On PC: fastboot flash recovery recovery-clockwork-5.0.2.0-crespo.img

For GRJ22, upgrade.zip is available here

  • fastboot flash boot boot.img (from stock GRJ22)
  • edit update.zip to remove recovery/ and edit META-INF/com/google/android/updater-script
    • remove all commands about recovery
    • add following line to keep rooted: set_perm(0, 0, 6755, "/system/bin/su");
    • because I've mangled cacerts.bks to add CACert stuff, I had also to remove stuffs about cacerts.bks in the script, then make a separate zip where I first restore the previous stock cacerts.bks from GRJ22 then apply the patch. And finally mangle the new cacerts.bks again.
  • In updater-script, check also which radio version it's able to patch. Look for line similar to this one:
apply_patch("MTD:radio:12583040:2ea138c96cc213b2662a4ae1ddee2d5c6bbcc958:12583040:213c2022516ba651f62064e4379487af1e8499a2",
           "-", 213c2022516ba651f62064e4379487af1e8499a2, 12583040,
           2ea138c96cc213b2662a4ae1ddee2d5c6bbcc958, package_extract_file("radio.img.p"));

Here it expects a radio.img from GRJ22 with SHA1 = 2ea138c96cc213b2662a4ae1ddee2d5c6bbcc958, ok
In case you don't have the right radio img in place, or don want to patch it, remove radio.img from the zip and all commands about radio in updater-script.

  • upload update.zip to /sdcard and "apply update from sdcard" via clockworkmod recovery, it'll skip signature verification by default
  • Backup & Restore / Backup
  • Mount USB
  • Copy new backup to PC

Ok now we got a system & boot images upgraded to 2.3.4
We can again modify boot.img to restore ro.secure=0:

  • Extract patched 2.3.6 boot.img (cf below, or use clockworkmod), modify it & flash it back

Extracting manually images from phone

On root shell on the phone:

# cat /proc/mtd
dev:    size   erasesize  name
mtd0: 00200000 00040000 "bootloader"
mtd1: 00140000 00040000 "misc"
mtd2: 00800000 00040000 "boot"
mtd3: 00800000 00040000 "recovery"
mtd4: 1d580000 00040000 "cache"
mtd5: 00d80000 00040000 "radio"
mtd6: 006c0000 00040000 "efs"
# cat /dev/mtd/mtd5 > /sdcard/radio.img

etc

Android 2.3

Getting fastboot & Android sources

There are several binaries around but I wanted to build my own. Maybe not the shortest way... I downloaded all android sources...
cf http://source.android.com/source/downloading.html
Some missing deps on my 64-but debian when I tried to compile everything: gperf, libc6-dev-i386, lib32ncurses5-dev ia32-libs g++-multilib lib32z1-dev lib32readline6-dev

. build/envsetup.sh
lunch crespo-eng
make -j2

Maybe we can just compile adb & fastboot:

make adb
make fastboot

cf http://www.cduce.org/~abate/build-android-adb-debian-sid-amd64

Getting adb & Android SDK

Get it from http://developer.android.com/sdk/index.html
Run tools/android -> in installed packages make sure to have "Android SDK Tools", latest revision and "Android SDK Platform-tools", latest revision
It brings also adb but not fastboot
If you want to update SDK: tools/android update sdk

adb

You need to activate USB debugging:

  • Settings > Applications > Development > USB debugging

Some examples:

  • adb devices
  • adb shell
  • adb logcat

adb as root

Once the phone is rooted, you can, from a shell with e.g. adb shell invoke "su" & get root.
But to get immediately into root, the file /default.prop needs to contain ro.secure=0
But that file is restored from boot.img at each boot so you need to modify that one
See http://android-dls.com/wiki/index.php?title=HOWTO:_Unpack%2C_Edit%2C_and_Re-Pack_Boot_Images#Alternative_Method
but there is also abootimg in debian:

  • Taking the boot.img from Samsung (see above in I9023_EUR_GRI54_XXKB3/I9023_EUR_GRI54_XXKB3/PDA_SOJU_GRI54_TMO_EUR_MR1_SIGNED.tar).
abootimg -x boot.img
mkdir ramdisk
cd ramdisk
gzip -dc ../initrd.img | cpio -i
sed -i default.prop 's/ro.secure=1/ro.secure=0/'
find . -print|cpio -o -Hnewc|gzip > ../initrd.img2
cd ..
abootimg -u boot.img -r initrd.img2
fastboot flash boot boot.img

For the last command, the phone needs of course to be in fastboot mode
Reboot phone
I had to re-enable USB debugging but now adb shell brings me immediately a root shell :)
Note that link mentioned above proposes an alternative way to flash the boot partition, directly from adb shell as root

Without this setup, it's a bit cumbersome to automate root commands from host, it looks like

adb shell su -c "netcfg usb0 dhcp"

and the SuperUser app prompts you for confirmation on the phone for each new command

Got a failure when trying to flash back a 8Mb boot.img?
From example above I started from a boot.img smaller than the full boot partition but if you create a new boot.img or start from an image of the full partition taken manually or with clockworkmod, boot.img will be 8Mb-large (8388608) and fastboot fails with "FAILED exceed blocks 0x00000020 > 0x0000001e".
I'm not really sure about what size the file should be but as it's filled with zeroes till reaching 8Mb, I decided to cut it:
0x00000020 => 0x0000001e means for me 8388608 / 0x20 * 0x1e = 7864320, so I did:

dd if=boot.img of=boot2.img bs=262144 count=30 
fastboot flash boot boot2.img

And it worked!

Images structure

You may want to explore .img content (from backups, stock ROMs etc).
Actually all .img are not the same

bootloader.img

/dev/mtd/mtd0
Unknown format

radio.img

/dev/mtd/mtd5
Unknown format, for baseband

recovery.img

/dev/mtd/mtd3
Unknown format

boot.img

/dev/mtd/mtd2
See here for details on the structure, and abootimg on Debian

system.img

Yaffs2 image, can be unpacked with unyaffs
Note that unyaffs failed unpacking stock system.img 2.3.3 & userdata.img but works fine on clockworkmod backups

data.img

Yaffs2 image, can be unpacked with unyaffs

cache.img

/dev/mtd/mtd4
Yaffs2 image, can be unpacked with unyaffs

misc

/dev/mtd/mtd1
Not backuped by clockworkmod

efs

/dev/mtd/mtd6
Yaffs2 image, can be unpacked with unyaffs
Not backuped by clockworkmod
Contains stuffs linked to baseband & bluetooth

Screenshots

Run ddms (from SDK) -> Tools / Device / Screen capture

USB tethering

Plug phone & PC via USB
Activate USB tethering (Settings / Wireless & networks / Tethering / USB Tethering)
It works OOB on Debian, nothing to do

Getting busybox

Need rooted phone, see above
Google's stripped busybox, called toolbox, is far from enough once you get a shell on the phone

Examples to use busybox versions instead of toolbox versions when the command exists twice:

# busybox mount -o remount,rw /system
# /system/xbin/mount -o remount,rw /system

Wi-Fi & client certs

To be able to authenticate to a Wi-Fi network using client certificates via TLS:
If needed, export certificate from IE in Pkcs#12 PFX, *with* private key, *with* all certs, *without* strong enc, *without* deletion of private key.
Rename .pfx file as .p12
(source: http://www.google.com/support/mobile/bin/answer.py?answer=168466&topic=27214#1086573)
Copy pkcs#12 certificate to root of USB storage.
File must end with .p12, not .pfx!
One single file with key+cert+cacerts is ok
Wi-Fi params: 802.1x EAP / TLS / phase2: None / CA cert: cf previous import / user cert: idem / Identity: DOMAIN\user... / Anonymous id: empty / password:...

Note that after each reboot, you'll have to select manually one of the protected networks to unlock the secure storage of personal certificates or open manually the certificates storage:
Settings > Location & Security > Use secure credentials
See also Keystore Unlocker

Importing certs

I could only manage it via a webserver & crafted headers:

<?php
header("Content-Type: application/x-x509-ca-cert");
?>
-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----

You may try this free service: http://www.realmb.com/droidCert/ which seems to do the same.
But even if imported they seem not to be used e.g. for IMAP TLS.

Importing CA certs in /system

Source: CACert wiki.
You don't need the full Android SDK, just adb binary.
I'm not sure if it's really the proper way but to get recognized the BouncyCastle lib which was already on my system (apt-get install libbcprov-java) I did

sudo ln -s /usr/share/java/bcprov.jar /usr/lib/jvm/java-6-sun/jre/lib/ext/


Adding CACert certificates:

adb pull /system/etc/security/cacerts.bks
wget http://www.cacert.org/certs/root.crt
wget http://www.cacert.org/certs/class3.crt
keytool -keystore cacerts.bks -storetype BKS -provider org.bouncycastle.jce.provider.BouncyCastleProvider -storepass changeit -importcert -trustcacerts -alias CACERT -file root.crt
keytool -keystore cacerts.bks -storetype BKS -provider org.bouncycastle.jce.provider.BouncyCastleProvider -storepass changeit -importcert -trustcacerts -alias CACERT3 -file class3.crt 
adb shell busybox mount -o remount,rw /system
adb push cacerts.bks /system/etc/security
adb shell busybox mount -o remount,ro /system

Now my IMAP TLS which is using a CACert-signed certificate works with strict TLS setting on the phone, cool!

WARNING this has broken proper upgrades and I had to mangle the update.zip to restore first the original cacert.bks file then get it patched.
Before I patched it again, my imap server gave me a lot of "couriertls: read: error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number" errors before I realized it was because I didn't' have the CACert certificates anymore...

Applications

Here is a list of potentially useful applications

Only for root

Those apps require to run on a rooted phone

With root support

Those apps benefit from running on a rooted phone

  • Root Explorer, nice file explorer with file editor etc
  • Ghost Commander
    • Nostalgic of Norton Commander? Using Midnight Commander? It's for you!
    • Root support: access system files, remount system as RW, etc
    • There is also an addon for SMB support
  • SSHDroid, ssh server
    • First time: ssh-copy-id root@192.168.1.4 (default owd is admin) then go to options to start automatically, disable pwd auth and enable shared keys auth. Displaying authorized keys from GUI fails but auth works properly.

NFC-related

Belgium-related

Brussels

Belgian Transportations

Belgian Media & Culture

Belgian telephony

Belgian misc

  • Belgacom Apps Guide : Provides a list of nice Belgian apps, that's how I discovered some of those on this list
  • Brut-Net BE : Compute net wage from gross pay
  • myShopi : Shopping list
  • Parcels : Delivery tracking, supports many providers including Belgian ones
  • PharmaMob : Localize closest all-night drugstore
  • ping.ping : payment application
  • Suivi par Droïd : Delivery tracking, supports many providers including Belgian ones
  • Redbox : postal offices & boxes locations in Belgium & France

Network

IM / Chat / Email

Browsers & web clients

Google

References

Sensors

Tools

System tools

GPS

Dictionaries

Open Source applications

GPS with offline maps

Commercial

  • Copilot Live (59€ for Europe)
  • Tomtom planned for next HTC phones, with free map of the country of purchase

Free

From this post:
Create your own maps with Mobile Atlas Creator (Java-based) & use them e.g. with RMaps, AndNav, Maverick, TrekBuddy,... (bitmaps so ok only for small areas such as cities)

  • Get e.g. RMaps from Android Market
  • Get latest release of Mobile Atlas Creator & unzip it
  • Get latest SqliteJDBC library and copy it inside th eMobile Atlas Creator directory
  • Run it (start.sh)
  • Choose your source, e.g. OpenStreetMaps, Google Maps,...
  • Select area: Right click to Drag, Scroll to zoom In/Out, Left click to select downloadable area
  • Select one or more zoom levels, e.g. from 12 to 18 for all levels of details of a city
  • Do “add selection” with a name of your choice
  • Select “BigPlanet SQLite” from “Atlas Settings” (for RMaps, otherwise Maverick Atlas Format for Maverick etc)
  • "Create Atlas"
  • Download the .sqlitedb map into the phone SDcard/rmaps/maps
  • Disconnect the phone
  • Open RMaps and Menu > More > Settings > User defined maps > BigPlanet_maps.sqlitedb > Enabled Maps
  • Return to main maps screen and then Menu > Maps > BigPlanet_maps.sqlitedb.

Free Navit - OSM

  • Install Navit from here
  • Install preprocessed OSM map from here and put it as /sdcard/navitmap.bin

Applications development

Getting Android SDK

Instructions below are for linux, if you're running Windows please check the provided resource links for variants of the instructions.
Make sure you've Java Development Kit installed

apt-get install sun-java6-jdk

As explained above, get Android SDK from http://developer.android.com/sdk/index.html
Run tools/android -> in installed packages make sure to have:

  • "Android SDK Tools", latest revision
  • "Android SDK Platform-tools", latest revision
  • a platform SDK, e.g. "Android 2.3.3"/"SDK Platform"


As we will develop from the command-line, no need for the Eclipse plugin
If you want to update SDK

  • tools/android update sdk
  • Then restart tools/android

Setting environment


We first define a number of things:

JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.26/
ANDROID_HOME=/home/phil/devel/00android/android-sdk-linux_x86/
PACKAGE=com.foo.mytest1
PACKAGE_SLASH=${PACKAGE//.//}
DEV_HOME=$(pwd)/mytest

We need also to define the target. To know what are the available targets:

$ ../../android-sdk-linux_x86/tools/android list target
Available Android targets:
----------
id: 1 or "android-10"
    Name: Android 2.3.3
    Type: Platform
    API level: 10
    Revision: 2
    Skins: QVGA, WVGA854, HVGA, WQVGA432, WVGA800 (default), WQVGA400
    ABIs : armeabi

So here we'll define:

TARGET=android-10

Preparing and using emulator

Here is how to create a basic emulator instance:

$ANDROID_HOME/tools/android --verbose create avd --name MyNexusS --target $TARGET --sdcard 1024M

Later, to launch it just do:

$ANDROID_HOME/tools/emulator -wipe-data -avd MyNexusS &

If needed to delete it:

$ANDROID_HOME/tools/android --verbose delete avd --name MyNexusS

Android application in command-line

See http://geosoft.no/development/android.html
Preparing the working directory (based on the environment variables defined above):

rm -rf $DEV_HOME
mkdir -p $DEV_HOME/src/$PACKAGE_SLASH/
mkdir -p $DEV_HOME/res/drawable/
mkdir -p $DEV_HOME/res/layout/
mkdir -p $DEV_HOME/res/values/
mkdir -p $DEV_HOME/obj/
mkdir -p $DEV_HOME/lib/
mkdir -p $DEV_HOME/bin/
mkdir -p $DEV_HOME/docs/

Create a dummy keystore:

$JAVA_HOME/bin/keytool -genkeypair \
                -validity 10000 \
                -dname "CN=company name,
                        OU=organisational unit,
                        O=organisation,
                        L=location,
                        S=state,
                        C=country code" \
                -keystore $DEV_HOME/AndroidTest.keystore \
                -storepass password \
                -keypass password \
                -alias AndroidTestKey \
                -keyalg RSA \
                -v

Create a Manifest file, here with some examples of permissions:

cat << EOF > $DEV_HOME/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="$PACKAGE"
      android:versionCode="1"
      android:versionName="1.0">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <uses-sdk android:minSdkVersion="2"/>

    <application android:icon="@drawable/mylogo"
                 android:label="@string/myApplicationName">
        <activity android:name="$PACKAGE.HelloAndroid"
                  android:label="@string/myApplicationName">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
EOF

Provide source code of an app, here a simple HelloWorld, using an icon file some_icon.png:

cat << EOF > $DEV_HOME/src/$PACKAGE_SLASH/HelloAndroid.java
package $PACKAGE;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TextView;

public class HelloAndroid extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TextView textView = new TextView(this);

    String text = getResources().getString(R.string.helloText);
    textView.setText(text);

    setContentView(textView);
  }
}
EOF

cat << EOF > $DEV_HOME/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="myApplicationName">Android Test Program</string>
    <string name="helloText">Hello, world!</string>
</resources>
EOF
cp some_icon.png $DEV_HOME/res/drawable/mylogo.png

Create R.java:

$ANDROID_HOME/platform-tools/aapt package -v -f -m \
    -S $DEV_HOME/res -J $DEV_HOME/src -M $DEV_HOME/AndroidManifest.xml \
    -I $ANDROID_HOME/platforms/$TARGET/android.jar || exit 1

Compile Java:

$JAVA_HOME/bin/javac -verbose -d $DEV_HOME/obj \
    -classpath "$ANDROID_HOME/platforms/$TARGET/android.jar:$DEV_HOME/obj" \
    -sourcepath $DEV_HOME/src \
    $DEV_HOME/src/$PACKAGE_SLASH/*.java || exit 1

Create DEX:

$ANDROID_HOME/platform-tools/dx --dex --verbose \
    --output=$DEV_HOME/bin/classes.dex \
    $DEV_HOME/obj $DEV_HOME/lib || exit 1

Create APK:

$ANDROID_HOME/platform-tools/aapt package -v -f \
    -S $DEV_HOME/res -M $DEV_HOME/AndroidManifest.xml \
    -I $ANDROID_HOME/platforms/$TARGET/android.jar \
    -F $DEV_HOME/bin/AndroidTest.unsigned.apk \
    $DEV_HOME/bin || exit 1

Sign APK:

$JAVA_HOME/bin/jarsigner -verbose \
    -keystore $DEV_HOME/AndroidTest.keystore \
    -storepass password \
    -keypass password \
    -signedjar $DEV_HOME/bin/AndroidTest.signed.apk \
    $DEV_HOME/bin/AndroidTest.unsigned.apk \
    AndroidTestKey || exit 1

Zip-align APK:

$ANDROID_HOME/tools/zipalign -v -f 4 \
    $DEV_HOME/bin/AndroidTest.signed.apk \
    $DEV_HOME/bin/AndroidTest.apk || exit 1

Generate documentation, if you wish:

$JAVA_HOME/bin/javadoc -verbose -d $DEV_HOME/docs -sourcepath $DEV_HOME/src \
    -classpath "$ANDROID_HOME/platforms/$TARGET/android.jar:$DEV_HOME/obj" \
    -author -package -use -splitIndex -version \
    -windowtitle 'AndroidTest' -doctitle 'AndroidTest' \
    $DEV_HOME/src/$PACKAGE_SLASH/*.java

To install the resulting application in the emulator (see above how to launch the emulator):

$ANDROID_HOME/platform-tools/adb -e install $DEV_HOME/bin/AndroidTest.apk

It's even possible to launch & control application from the PC.
See http://learnandroid.blogspot.com/2008/01/run-android-application-from-command.html

$ANDROID_HOME/platform-tools/adb -e shell am start -a android.intent.action.MAIN \
    -n $PACKAGE/$PACKAGE.HelloAndroid

To remove it:

$ANDROID_HOME/platform-tools/adb -e uninstall $PACKAGE

To do the same on a real device rather than on the emulator, make sure the phone is connected by USB and running in debug mode as explained above, then simple use -d (device) instead of -e (emulator), so previous instructions become:

$ANDROID_HOME/platform-tools/adb -d install $DEV_HOME/bin/AndroidTest.apk
$ANDROID_HOME/platform-tools/adb -d shell am start -a android.intent.action.MAIN \
    -n $PACKAGE/$PACKAGE.HelloAndroid
$ANDROID_HOME/platform-tools/adb -d uninstall $PACKAGE

Simple java in command-line

See https://davanum.wordpress.com/2007/12/04/command-line-java-on-dalvikvm/
Preparing the working directory (based on the environment variables defined above):

rm -rf $DEV_HOME
mkdir -p $DEV_HOME/src/$PACKAGE_SLASH/
mkdir -p $DEV_HOME/obj/
mkdir -p $DEV_HOME/lib/
mkdir -p $DEV_HOME/bin/

Provide source code of an app, here a simple HelloWorld:

cat << EOF > $DEV_HOME/src/$PACKAGE_SLASH/HelloWorld.java
package $PACKAGE;

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}
EOF

Compile Java:

$JAVA_HOME/bin/javac -verbose -d $DEV_HOME/obj \
    -g $DEV_HOME/src/$PACKAGE_SLASH/*.java || exit 1

Create DEX:

$ANDROID_HOME/platform-tools/dx --dex --verbose \
    --output=$DEV_HOME/bin/classes.dex \
    $DEV_HOME/obj $DEV_HOME/lib || exit 1

Create JAR:

$ANDROID_HOME/platform-tools/aapt add $DEV_HOME/CmdLine.jar \
    $DEV_HOME/bin/classes.dex || exit 1

To install the resulting application in the emulator (see above how to launch the emulator):

$ANDROID_HOME/platform-tools/adb -e push $DEV_HOME/CmdLine.jar /sdcard/

To execute it from the PC.

$ANDROID_HOME/platform-tools/adb -e shell \
    /system/bin/dalvikvm -Xbootclasspath:/system/framework/core.jar -classpath /sdcard/CmdLine.jar $PACKAGE.HelloWorld

To do the same on a real device rather than on the emulator, make sure the phone is connected by USB and running in debug mode as explained above, then simple use -d (device) instead of -e (emulator), so previous instructions become:

$ANDROID_HOME/platform-tools/adb -d push $DEV_HOME/CmdLine.jar /sdcard/
$ANDROID_HOME/platform-tools/adb -d shell \
    /system/bin/dalvikvm -Xbootclasspath:/system/framework/core.jar -classpath /sdcard/CmdLine.jar $PACKAGE.HelloWorld

BouncyCastle library

Depending on the creation of a real Android app or a simple java code and depending on the fact you want to use the internal crippled version or the full fledged version, different approaches are needed.
Internal library API is explained here
Some RSA examples are given here and a tuto (fr) here
To get a list of supported algorithms, see here. This illustrates e.g. the differences between the crippled internal version of the library and the complete one.

Using internal BouncyCastle library from an android application

That's the normal way.
Here is an example, mixing the HelloAndroid shown above with an example from http://www.java2s.com/Tutorial/Java/0490__Security/RSASignatureGeneration.htm

cat << EOF > $DEV_HOME/src/$PACKAGE_SLASH/HelloAndroid.java
package $PACKAGE;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.Security;
import java.security.Signature;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TextView;

public class HelloAndroid extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TextView textView = new TextView(this);

//    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    try {
      KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "BC");
      keyGen.initialize(512, new SecureRandom());

      KeyPair keyPair = keyGen.generateKeyPair();
      Signature signature = Signature.getInstance("SHA1withRSA", "BC");

      signature.initSign(keyPair.getPrivate(), new SecureRandom());

      byte[] message = "abc".getBytes();
      signature.update(message);

      byte[] sigBytes = signature.sign();
      signature.initVerify(keyPair.getPublic());
      signature.update(message);
      if (signature.verify(sigBytes))
        textView.setText("true");
      else
        textView.setText("false");
      setContentView(textView);
    } catch (java.security.NoSuchAlgorithmException e) {
      textView.setText("NoSuchAlgorithmException");
      setContentView(textView);
      return;
    } catch (java.security.NoSuchProviderException e) {
      textView.setText("NoSuchProviderException");
      setContentView(textView);
      return;
    } catch (java.security.InvalidKeyException e) {
      textView.setText("InvalidKeyException");
      setContentView(textView);
      return;
    } catch (java.security.SignatureException e) {
      textView.setText("SignatureException");
      setContentView(textView);
      return;
    }

  }
}
EOF

Using external BouncyCastle library from an android application

Not tested. The problem is that it's impossible to get an external library with the exact same API as the internal one (see bugreport), so different approaches are possible:

Using internal BouncyCastle library from a simple java code


Here is an example, mixing the HelloWorld shown above with an example from http://www.java2s.com/Tutorial/Java/0490__Security/RSASignatureGeneration.htm

cat << EOF > $DEV_HOME/src/$PACKAGE_SLASH/HelloWorld.java
package $PACKAGE;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.Security;
import java.security.Signature;

public class HelloWorld {

  public static void main(String[] args) throws Exception {
    //Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "BC");

    keyGen.initialize(512, new SecureRandom());

    KeyPair keyPair = keyGen.generateKeyPair();
    Signature signature = Signature.getInstance("SHA1withRSA", "BC");

    signature.initSign(keyPair.getPrivate(), new SecureRandom());

    byte[] message = "abc".getBytes();
    signature.update(message);

    byte[] sigBytes = signature.sign();
    signature.initVerify(keyPair.getPublic());
    signature.update(message);
    System.out.println(signature.verify(sigBytes));
  }

}
EOF

There is a little trick when you will run the example on the phone or emulator: you've to provide explicitly the path to internal BouncyCastle:

$ANDROID_HOME/platform-tools/adb -d shell \
    /system/bin/dalvikvm -Xbootclasspath:/system/framework/core.jar:/system/framework/bouncycastle.jar -classpath /sdcard/CmdLine.jar $PACKAGE.HelloWorld

Using external BouncyCastle library from a simple java code


You can use the same code as above but you need this time to provide the external library:

# From http://www.bouncycastle.org/latest_releases.html
cp -a bcprov-jdk16-146.jar $DEV_HOME/lib

To run the example, don't provide any path to the internal lib of course:

$ANDROID_HOME/platform-tools/adb -d shell \
    /system/bin/dalvikvm -Xbootclasspath:/system/framework/core.jar -classpath /sdcard/CmdLine.jar $PACKAGE.HelloWorld

Normally this works as such. In case of error no "BC" provider found, you can uncomment the line

Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider())