Sunday, June 13, 2010

Using adb to install apk packages

Once you have found your attached android device with adb list devices install the apk to the device


$> adb -s HT93LGZ06222 install /path/to/the/apk/thepackagename.apk
651 KB/s (5584634 bytes in 8.365s)
pkg: /data/local/tmp/thepackagename.apk

Success

Using adb to find attached devices

This command will show you the attached android devices.

$> adb devices
List of devices attached
HT93LGZ06222 device

Wednesday, May 5, 2010

Adndrod development from any text editor in Lunux

I prefer to learn how to create a simple android app bare-bones, from scratch. Eclipse and the ADT plugin are easy but don't give you a good sense of the tools necessary to compile and deploy an android app. By building an app manually, at least once, you become better acquainted with what is happening behind the ADT plugin and will be better prepared to trouble shoot problems.

Luckily, all the tools you need to build an android app with a simple text editor come with the SDK and reside in the tools directory.

For this sample application, I am on a Linux box. You can probably follow these steps, almost exactly on a windows box but you will have to account for path differences and put '.exe' on the end of calls to executables.

You will first need to download the SDK and set up the emulator. I covered that in the privious post. Let get going...


Step 1. Create an android project
# I am in the tools directory of the SDK and I run the following command. The target
# was chosen from the output of 'android list targets' discussed in the previous post.

$> ./android create project --target 3 --name textEditorHelloWorld --path ~/android_projects/textEditorHelloWorld --activity tehello --package com.osilabs.android.projects.tehello

# The following is the output

Created project directory: /home/myuser/android_projects/textEditorHelloWorld
Created directory /home/myuser/android_projects/textEditorHelloWorld/src/com/osilabs/android/projects/tehello
Added file /home/myuser/android_projects/textEditorHelloWorld/src/com/osilabs/android/projects/tehello/tehello.java
Created directory /home/myuser/android_projects/textEditorHelloWorld/res
Created directory /home/myuser/android_projects/textEditorHelloWorld/bin
Created directory /home/myuser/android_projects/textEditorHelloWorld/libs
Created directory /home/myuser/android_projects/textEditorHelloWorld/res/values
Added file /home/myuser/android_projects/textEditorHelloWorld/res/values/strings.xml
Created directory /home/myuser/android_projects/textEditorHelloWorld/res/layout
Added file /home/myuser/android_projects/textEditorHelloWorld/res/layout/main.xml
Added file /home/myuser/android_projects/textEditorHelloWorld/AndroidManifest.xml
Added file /home/myuser/android_projects/textEditorHelloWorld/build.xml

# These directories were created and populated with the following...

$> ls ~/android_projects/textEditorHelloWorld/
AndroidManifest.xml bin build.properties build.xml default.properties libs local.properties res src

Step 2. Create your hello world code
# Move to the newly created directory which has the sourcecode.

$> cd ~/android_projects/textEditorHelloWorld/src/com/osilabs/android/projects/tehello

# Here we see that creating the project also created the java file we will simply modify
# to create our hello world.

$> ls
tehello.java

# It looks like this

$> cat tehello.java
package com.osilabs.android.projects.tehello;

import android.app.Activity;
import android.os.Bundle;

public class tehello extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

# Let's edit it so our hello world app says something. I use gedit, you use
# whatever you want.

$> gedit tehello.java

# I altered the code to the following. I have highlighted in red
# the lines I changed.

$> cat tehello.java
package com.osilabs.android.projects.tehello;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class tehello extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Any program is only as good as it is useful. -L. Torvalds");
setContentView(tv);
}
}

# IMPORTANT: open jdk doesn't work. You must get the sun jdk (or oracle
# jdk, or whoever owns it by the time you read this). I downloaded it
# will probably change with new versions so search for it, find it,
# install it. If you don't, and try to use openjdk, you may be getting errors
# about "java.lang.NoClassDefFoundError: com.android.jarutils.DebugKeyProvider

# After you install the sun jdk, you need to set JAVA_HOME.

$> export JAVA_HOME="/usr/java/latest"

# Better yet, put this in your login script. I use bash so I put it in my ~/.bash_profile
# along with putting it in my path. Something like

PATH=$PATH:/usr/java/latest/bin
export PATH
export JAVA_HOME="/usr/java/latest"

# Yes, /usr/java/latest exists after the install, it is a symbolic link to the most
# recent jdk you have ... I think. I am not a big java guy so this is probably
# true ... probably. :)

# Now compile it in debug mode. This is easier to start with because
# we don't have to worry about signing it yet. Move to the root of the
# project and use ant to compile it. If you don't have ant, get it first.

$> cd ~/android_projects/textEditorHelloWorld
$> ant debug

# Stuff scrolls by, but in the end it says "BUILD SUCCESSFUL", you hope. This
# has created the file you will install, the apk file. I found it here:
# ~/android_projects/textEditorHelloWorld/bin/textEditorHelloWorld-debug.apk


Step 3. Launch the emulator
# If you don't still have it open from previously getting it working, launch it. Give
# time to boot up. First cd back to the root of the android sdk directory, where
# the tools directory is.

$> tools/emulator -avd tools/emulator -avd avd_v1.6_id3 &

# The '&' at the end is optional, I put it there so I could get my terminal back.

Step 4. Install your new app
# Install your cool new app. I first tried without the -s emulator flag and it failed.
# I got the emulator identified '5554' from the top window bar of the running
# emulator. Yours may be the same, may be different.

$> tools/adb -s emulator-5554 install ~/android_projects/textEditorHelloWorld/bin/textEditorHelloWorld-debug.apk

88 KB/s (4544 bytes in 0.050s)
pkg: /data/local/tmp/textEditorHelloWorld-debug.apk
Success

# Now, on the emulator, I pull up the tab with all the applications, and there it is,
# at the bottom of the list, I had to scroll. It is called 'tehello'. Click it to run it.
# I won't tell you what happens, I don't want to spoil the surprise.





Tuesday, May 4, 2010

Create an android development ADV (Android Virtual Device) in Linux

1. Download the android SDK.  Install it somewhere like '~/android/'

2. Create virtual device for emulator

 // cd into the root of the sdk you just downloaded after unzipping it
 $> cd android_sdk_directory

 // Runs GUI to install platforms. You need them to set different 
 //  virtual devices for the different android versions.
 $> tools/android

 // A GUI will come up, go to 'Installed Packages' click 'Update All'.
 //  This should start downloading the SDK Platforms you need.

3.  Get the target ID you would like to use for the emulator.
 //  This shows which target id to use to get the emulator
 //  to simulate different android versions. The target ID is the
 //  number by each 'id:'.
 $>tools/android list targets

4. Create your virtual device for the target ID from above
 // For me, id 3 was for android version 1.6. For you, it may be different.
 $> tools/android create avd -n avd_v1.6_id3 -t 3

 // Just for fun, I will create another virtual device for a different version
 $> tools/android create avd -n avd_v2.1_id6 -t 6

 // It asked me to create a custom hardware profile, I said no. if
 //  I need it later, I wall revisit this, for now, probably fine.

5. Fire up the emulator
 $> tools/emulator -avd avd_v1.6_id3
 // or
 $> tools/emulator -avd avd_v2.1_id6

 // The emulator should fire up and you should be able to take
 //  android for a spin...

Thursday, February 11, 2010

Ignoring Files in Subversion (svn)

I have used the svn:ignore property with propset successfully to tell svn to pay no attention to some files. Here is how:

// Tell subversion to ignore a single item, overwriting
// any existing ignored items.
$> svn propset svn:ignore data .


// Tell svn to set multiple items to be ignored. this
// will open up an editor where you
// will just list the items to ignore, one on each line.
$> svn propedit --editor-cmd vi svn:ignore .


// You can leave off the '--editor-cmd vi' part if you set
// the svn editor in your environment with something like
// this. And vi can be replaced with your favorite editor
$> export SVN_EDITOR='vi'


// To see your properties use
$> svn proplist -vR


// You may also want to delete a property. Use
$> svn propdel svn:ignore data
.
Both of these commands assume I am in the directory where the items I am ignoring exist. That is what the '.' at the end indicates.

Tuesday, January 19, 2010

Launch Tortoise Repo Browser easily

This is a useful trick for tortoise users on Windows but the same technique probably applies just as well to Linux and other platforms that run tortoise. This lets you create a launch icon that directly launches the repo browser. The command to do so looks something like this (your path will be different):

TortoiseProc.exe /command:repobrowser /path:"url:svn://svn.somehost.com" /notempfile

Just take this command and create a quick launch icon or desktop shortcut. Upon clicking it, repo browser! You may need to add the full path to TortoiseProc.exe. On Linux or other platforms, you will have to make adjustments accordingly but the same idea still applies.

Monday, January 18, 2010

Refresh cached javascript and css in Firefox

This morning I found myself struggling to get a simple change to some javascript to show up on a web page I was authoring. I was using that old trick where you hold down shift and refresh the page which, at one time in history, I swear, forced Firefox to re-request the included javascript, css, and possibly other types of externally included files in a web page. Well, it wasn't working and before deciding it would be best to punch myself in the face and go get another cup of coffee, I googled on the topic a little. It didn't take much to find out there is a trick to this technique that, for years, either I never knew, or it is a newer "feature". To the point, you must hold down the shift key the entire time the page is loading. I had been holding shift, clicking the refresh icon, then letting go and allowing the page to load and that wasn't enough to tell firefox to re-request my javascript include file. Once I held shift the entire time the page was loading, I got my changes. Seriously, Mozilla. Seriously? I don't remember this being a problem in the past but maybe I just never noticed either. Good luck!

Sunday, January 17, 2010

Android ssh app

The only ssh client I have used on my android G1 is called connectBot. At the time of this writing it was free. With any luck, it is still free and will remain so.

It works very well. In hitting a red hat box, I had dircolors, bash command line history by rolling the scroll wheel up, and pretty much what I expect from an ssh client.