Feeds:
Posts
Comments

Archive for the ‘Android development tools’ Category

There is a lot of questions on various forums on how to install a CA certificate on Android device. Some suggest using RealmB web site. While I trust that RealmB authors have had the best intent to help others, I think publishing a security certificate you use to connect to your corporate network on the web is as wise as giving keys to your house to absolute strangers and I personally would not use the tool.

Android 2.2 I have installed on my Motorola Atrix has both Certificate Installer and Certificate Manager.

To find them go Settings-> Location & security, scroll down ->Credential storage-> Install from internal phone storage. You will see a dialog offering you to complete the action using either Certificate Installer or Certificate Manager.

But both are rather enigmatic tools without any documentation and it took me some time to find how one actually can use Certificate Installer (this one is a part of Android; I guess Certificate Manager is Motorola’s product). This is the beauty of Open Source Software (at least for developers) – one can read a code and figure out how “the damn thing works” :)

First of all by default Certificate Installer looks for certificates to install at folder called download in SD card. It considers as certificates files with extensions either .crt or .p12. The latter stands for certificates in PKCS #12 format and the former stands for certificates in Base64 Encoded X.509 format. You should be aware that there are other certificate formats as well, e.g. PKCS #7, but it seems that Certificate Installer can’t handle them. If you are interested to find more about certificate file formats, read this.

[Added on April 20, 2012. Vlad (another Vlad, not I) commented below: "...the certificate should be copied to an internal folder (not external). For some devices, the installer cannot find a certificate on the external SD card."]

So the first way to use Certificate Installer is obvious:

  • Extract SD card from your phone, insert it into your computer
  • Create download folder in its root folder and put there the certificate file or files you want to install; of course make sure that they are in acceptable format and have proper file extensions
  • Insert SD card in your phone and run Certificate Installer as described above

Well, obvious but not co convenient… First of all extracting and inserting SD card is not so simple on certain devices. Also there are devices without SD card. So what to do if you don’t want  or can not use this way? Then you can use the second method:

  • Connect your device to your computer via USB cable. Make whatever you usually do so that the device appears as an external drive in the computer filesystem. If you never connected your Android device to your computer as USB drive and you use Windows computer, the most likely you will need to install a proper USB driver on your computer. Such USB drivers for Android devices are manufacturer-specific and can be found on manufacturers web sites (see the list here). For Ubuntu Linux you will need to add some configuration parameters; no USB driver installation is required. See details here.
  • Copy your certificate file(s) to the device internal storage. Again, make sure that the files have proper format and proper file extensions
  • Open Files application and select (touch) the certificate file. A dialog will appear with details on the certificate and two buttons: Install certificate and Cancel. Click Install certificate. That’s all

Read Full Post »

GUI tool to work with SQLite

There is a number of GUI tools for SQLite available for Linux and Windows. We’ll be using Sqliteman. It is free, available for Linux, Windows, Mac OS X and various versions of Unix. This makes instructions we are going to provide applicable to all those OS.

To install it on Ubuntu 10.10 Linux use the command:

sudo apt-get install sqliteman

Installation instructions and Sqliteman installer for Windows could be found here as well as instructions and distributives for other OSs.

Let’s try to explore an existing database from Contacts application using Sqliteman.

Sqliteman can not access files inside Android AVD or Android device. To make use of Sqliteman we have to copy the database file we want to work with from  Android filesystem to your PC local filesystem. This can be done with adb tool. If you want to work with files inside Android filesystem, use SQLite command line shell described here.

Contacts application uses a database which is stored within contacts2.db file. It is located in /data/data/com.android.providers.contacts/databases/ directory of Android device or AVD. Type this command (I assume you have running AVD or a device attached to your computer):

adb pull /data/data/com.android.providers.contacts/databases/contacts2.db ~/android

Here ~/android is a directory where we want to copy the database file.

If everything is OK, you’ll see something like this:

590 KB/s (109568 bytes in 0.181s)
vlad@vlad-laptop:~/mydroid$

This confirms that the file was copied successfully.

Now let’s start Sqliteman. If you use Ubuntu Linux as I do, go to Applications -> Programming -> Sqliteman. This will launch Sqliteman:

Now we have to open the DB file. Go to File -> Open and select the file you downloaded from Android AVD or device. You will see something like this:

Let’s explore the database. Click on Tables in the left pane. This will show you the list of tables in the database. Click on data table; this will show you a description of the table:

You can drill down to find more details… But for the purpose of the post let’s try to run an SQL statement. Type

select * from data

in the top right pane, then click on a green triangle above it. This will display the content of the table data in the pane below the one where you typed the SQL statement:

After we finished your work with the database using Sqliteman, we can copy the database file to the device or AVD. For this we should use adb push command. In our case this will be:

adb push ~/android/contacts2.db /data/data/com.android.providers.contacts/databases/contacts2.db

That’s all for now.

Read Full Post »

Today we are going to look at how to work with Android application database

Android OS 2.X uses SQLite relational database for storing data. SQLite website states:

SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. SQLite is the most widely deployed SQL database engine in the world. The source code for SQLite is in the public domain.

Basically, there are two main ways to explore or to change Android SQLite database:

  • We can use SQLite command line client to access files containing SQLite database tables inside Android device (a phone, a tab) or inside Android emulator
  • Alternatively we can copy the files from the device or emulator to our workstation, work with them there using a handy GUI tools and copy them back to the device or emulator after we done

Both methods have advantages and disadvantages, therefor we need to get familiar with both.

I assume you have already installed Android development environment. If not, instructions for Windows are here, for Linux they are here.

The instructions below work for both Windows and Linux.

Command-line interface

Android provides command line shell access to its SQLite databases through a standard SQLite command line shell. You can find a good manual on it here. Just keep in mind, that Android OS 2.3 contains SQLite version 3.6.22 while the documentation has been written for a slightly earlier version (3.6.11) which accounts for small discrepancies. Otherwise Android team implemented a complete command line access to SQLite databases (unlike they did with ash shell; Android provides a reduced set of ash shell commands).

We can start SQLite command line shell while running adb. adb stands for Android Debug Bridge, a tool similar to SSH or telnet which allows accessing either AVD (Android Virtual Device, essentially Android emulator) or a real Android device connected to our computer.

I highly recommend experimenting with AVDs instead of real devices until you feel confident with what you are doing.

So let’s start AVD. Type:

emulator @TestAVD

Here TestAVD is a name of AVD we are going to work with (of course, your AVD may have a different name).

If you get a message saying that emulator not found, you may have forgotten to add tools and platform-tools directories of Android SDK to your system PATH. If all goes fine, we’ll see Android emulator GUI in a few seconds. Give it a time to start Android OS; it may take up to a minute or so.

Now we have to open another command line console window. Type there:

 adb devices

This command should give us a list of all AVDs or Android devices running. You’ll see something like this:

vlad@vlad-laptop:~$ adb devices
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
List of devices attached
emulator-5554	offline

Please note, it says that emulator-5554 is offline. The most likely it is since the AVD went to a sleeping mode after it started:

Click on Menu button on AVD to “wake” it up. Type:

adb shell

This will launch Android ash shell and you will see Android shell prompt (#). Note, this will not work if you have several AVD or devices running; in that case you need to type

adb -s emulator-5554 shell

explicitly describing what AVD or device you want to connect to. Anyway, after we got the shell prompt (#), we can start SQLite shell. Type:

sqlite3

Now you should see SQLite shell prompt:

sqlite>

SQLite command line shell accepts SQL statements and so-called “dot” commands, i.e. commands starting from a dot. Those commands unlike SQL are SQLite-specific.

To see the list of “dot” commands type .help (note the period) after you get SQLite shell prompt (sqlite>):

# sqlite3
sqlite3
SQLite version 3.6.22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .help
.help
.backup ?DB? FILE      Backup DB (default "main") to FILE
.bail ON|OFF           Stop after hitting an error.  Default OFF
.databases             List names and files of attached databases
.dump ?TABLE? ...      Dump the database in an SQL text format
If TABLE specified, only dump tables matching
LIKE pattern TABLE.
.echo ON|OFF           Turn command echo on or off
.exit                  Exit this program
.explain ?ON|OFF?      Turn output mode suitable for EXPLAIN on or off.
With no args, it turns EXPLAIN on.
.genfkey ?OPTIONS?     Options are:
--no-drop: Do not drop old fkey triggers.
--ignore-errors: Ignore tables with fkey errors
--exec: Execute generated SQL immediately
See file tool/genfkey.README in the source
distribution for further information.
.header(s) ON|OFF      Turn display of headers on or off
.help                  Show this message
.import FILE TABLE     Import data from FILE into TABLE
.indices ?TABLE?       Show names of all indices
If TABLE specified, only show indices for tables
matching LIKE pattern TABLE.
.load FILE ?ENTRY?     Load an extension library
.log FILE|off          Turn logging on or off.  FILE can be stderr/stdout
.mode MODE ?TABLE?     Set output mode where MODE is one of:
csv      Comma-separated values
column   Left-aligned columns.  (See .width)
html     HTML <table> code
insert   SQL insert statements for TABLE
line     One value per line
list     Values delimited by .separator string
tabs     Tab-separated values
tcl      TCL list elements
.nullvalue STRING      Print STRING in place of NULL values
.output FILENAME       Send output to FILENAME
.output stdout         Send output to the screen
.prompt MAIN CONTINUE  Replace the standard prompts
.quit                  Exit this program
.read FILENAME         Execute SQL in FILENAME
.restore ?DB? FILE     Restore content of DB (default "main") from FILE
.schema ?TABLE?        Show the CREATE statements
If TABLE specified, only show tables matching
LIKE pattern TABLE.
.separator STRING      Change separator used by output mode and .import
.show                  Show the current values for various settings
.tables ?TABLE?        List names of tables
If TABLE specified, only list tables matching
LIKE pattern TABLE.
.timeout MS            Try opening locked tables for MS milliseconds
.width NUM1 NUM2 ...   Set column widths for "column" mode
.timer ON|OFF          Turn the CPU timer measurement on or off
sqlite>

To exit from SQLite shell type .exit (note the dot) or Ctrl-D. To interrupt a long running SQL statement type Ctrl-C.

Until now SQLite shell hasn’t been connected to any database. To make the shell connected to a particular DB, launch it with DB file as a parameter (SQLite contains the whole DB in one single file which is very handy). But where to find those files?

Each time a new package is installed in Android OS, the OS creates a sort of “home directory” for it. All those “home directories” are located in /data/data directory of the device or AVD. The names of the directories correspond to names of the packages. All package data including its DB or DBs is stored within the directories.

Type following while you are in Android command line shell (not SQLite command line shell! You should see # as a prompt):

ls -l /data/data

You will see something like this:

drwxr-x--x app_0    app_0             2011-01-04 15:36 com.android.sdksetup
drwxr-x--x app_2    app_2             2011-01-04 15:36 com.android.calculator2
drwxr-x--x app_4    app_4             2011-01-04 15:36 com.android.packageinstaller
drwxr-x--x app_6    app_6             2011-01-04 15:36 com.android.development
...........
#

Those are package “home directories”. You can either dig down into those directories to find database files (they have .db extension), or you can use this command which will list all database files:

cd /data/data
ls -R

This will show you all directories and files in /data/data directory (unfortunately Android command line shell does not support find command). You’ll see something like this:

.............
./com.android.providers.userdictionary:
lib
databases

./com.android.providers.userdictionary/lib:

./com.android.providers.userdictionary/databases:
user_dict.db
............

Let’s try to see what is inside EmailProvider.db database:

# sqlite3 /data/data/com.android.email/databases/EmailProvider.db
SQLite version 3.6.22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
Account           HostAuth          Message           Message_Updates
Attachment        Mailbox           Message_Deletes   android_metadata
sqlite> select * from Account;
sqlite>

This shows that EmailProvider.db contains a number of tables: Account, HostAuth etc. Output of SQL statement  select * from Account shows that table Account is empty.

We are not going to discuss here all capabilities of SQLite command line shell. If you are interested, take a look here.

While it is cool to be able to access SQLite database on a real Android device with so powerful tool, I think the use of SQLite command shell in this case is limited to debugging and research purposes. Indeed, I would not design a database inside a live device. Designing a database makes sense as a part of an overall application development which in Android case is done on a computer running Eclipse IDE and Android SDK. We’ll discuss tools more suitable for SQLite development in one of the next posts. There are such tools. They provide a handy GUI.

Read Full Post »

In previous post we were looking at various command-line tools that come up with Android SDK (android, emulator, adb). We mentioned that abd shell command provides a shell access to Android device. Let’s explore this further.

But before we start on this, let’s talk a bit about what Android OS is.

A good short history of Android OS can be found here. If you read the article, you’ll find that Android OS is actually modified stripped down Linux + certain additional native libraries + Dalvik JVM which runs “customer facing” applications written in Java. Just a bit more facts:

  1. Android OS 1.6, 2.0. 2.1 are based on Linux Kernel 2.6.29
  2. Android OS 2.2 is based on Linux Kernel 2.6.32
  3. Android OS 2.3 is based on Linux 2.6.35

Which means Ubuntu Linux 10.4 Natty Narwhal and running on my laptop and Android 2.3 are sort of  sisters :) since they share the same Linux Kernel 2.6.35. Interesting…

The shell used by Android is actually Unix ash shell. It is somewhat stripped down, so such commands like grep are not available, but it still has around 100 commands which is very impressive. You can check what commands are available:

adb shell ls /system/bin

More on adb and shell commands can be found here.

You can look at certain examples of shell usage here. Don’t try them on a real device unless you fully understand what you are doing!

It is no surprise that certain developers started porting Linux applications to Android. For example look here for how to add bash shell to Android OS.

I will be not surprised to find ports of bigger Linux applications to Android. For example, Gnome is already allegedly running on Android. But why to be surprised…. Android devices have more CPU power and pixels on screens that best PCs had 10-15 years ago, right?

Read Full Post »

Let’s examine what is available in tools and platform-tools directories of your Android SDK. I will be using Linux machine for this blog, but adjustements for Win environment are minor ones and rather obvious.

Before you start, make sure that both directories have been added to your computer PATH.

There is a number of scripts and executables in this directory (there are directories, too, but we are not looking at them now).

Today we’ll examine a script named android. If you launch it with –help option (android –help), you will see something like this:

android script provided us with a list of options and so-called “verbs” it understands. Verbs actually mean certain actions that can be performed by the script. Those actions fall into 3 groups:

  1. AVD (Android Virtual Device) management (create, move, delete AVD etc)
  2. Android project management (create, update an Android project)
  3. ADB (Android Debug Bridge) and Android SDK update

Basically, these are functions of SDK Manager and android is the main script which launches SDK Manager. You can easily verify this by executing the script without parameters: in such case it will launch SDK Manager GUI which we already have seen while installing Android development environment.

You can use android script for all those functions, but, frankly, SDK Manager GUI is handier, so I would not use the script in command-line mode unless I want to write certain automation scripts that require its functions.

Next tool which we are going to look at is emulator executable. This is actually an interpreter which runs AVDs. This executable is used by Eclipse IDE under the cover to launch Android device emulator. But you can launch it from command line as well. If you followed me in installing Android development environment, you already should have had at least one AVD available. You can verify this by using android script. Type this in your command shell:

android list avd

This will list you all AVD you have on your computer with certain details on them. If you have AVD indeed, you can launch emulator to execute the AVD.  If you don’t, launch SDK Manager by executing android script without any parameters. This will provide you with SDK Manager GUI and there is a tab for creating AVDs. Create AVD and proceed.

There are two equal ways of launching emulator to run AVD. Type in your command shell:

emulator -avd NameOfYourAVD

or this:

emulator @NameOfYourAVD

Of course, use a name of your existing AVD instead of NameOfYourAVD. Both commands will launch the AVD and you’ll see the emulator GUI:

Next we are going to look at adb command-line tool. adb stands for Android Debug Bridge. This is an executable which allows other applications to connect to either Android emulator or a real device if such is connected to your computer.

While the emulator is still running, type this in the command-line shell:

adb devices

You will see something like this as adb output (it may take several seconds to start adb, so be patient):

List of devices attached
emulator-5554    offline

This output shows that adb is aware of emulator instances running and their state. If there is no emulator/device running, adb returns no device.

Unlike android script, adb has a lot of functions. You can see a list of them if you type

adb help

If you just want to do a basic development, you probably don’t need to know much about adb; it will be operating “behind the scene” for you. However, if you consider yourself as an advanced developer, you may want to learn more about adb here.

To give you examples, here are just some of adb functions:

  1. Copy Android application (apk file) to an emulator or a device and install it there (install command)
  2. Copy arbitrary files to and from an emulator or a device (push and pull commands)

What I personally like most among adb functions, is a real Unix/Linux shell. Type this:

adb shell

and adb will present you with a shell prompt (# in my case). To make sure it is the shell indeed, type ls (Unix/Linux analog of dir command on Windows) and you will see filesystems mounted on your running AVD:

We’ll talk about shell access to Android devices in the next blog post.

Read Full Post »

Follow

Get every new post delivered to your Inbox.

Join 55 other followers