Monthly Archives: June 2010

Reading a file from an Android Test Project

Don’t you just feel like a fool when you can’t even set up your test project to use to read a file?  It’s a common approach, you use a local file instead of a remote resource to test your processing of the data.

Well, I had to do a bit of hunting so I thought that I would share it with you.  The more posts on this topic, the quicker it will be for the next person who gets stuck on this to find the answer.

So, I was testing a POJO and felt that all I needed was to extend the AndroidTestCase but this assumption was incorrect.  In order to access the test project’s own resources you need to extend the InstrumentationTestCase

In my case I stored my test data in res\raw so that I could use a stream reader to convert the data to a simple string.  Here’s how I did it:

protected void setUp() throws Exception {
Resources res = getInstrumentation().getContext().getResources();
InputStream instream = res.openRawResource(R.raw.uk_atom);


}catch(Exception e){
Log.d(LOGTAG, e.getMessage());


Now you are not restricted to using the raw resource there are plenty of other types of resources, take a look at the documentation here: andriod developers

Happy coding!


Java Model Exception when cleaning android project

Android woes

I thought that I would add this little note just in case there is anyone out there who has also suffered this problem. It can occur if you remove an android project from your workspace and then decide to add it again as a new project. In my case I had exported the project from subversion and then deleted the original (for reasons of sanity in my repository).

So the problem occurs when you add the project back to your workspace. I’m presuming that some of the settings get really confused when you try to add a project that used to be part of the workspace. I’m not really concerned with what but more interested in the fix. Here is the error:

Java Model Status [gen [in HelloActivity] does not exist]

It occurs when you try to clean your project and you just get a red cross on your project folder and nowhere else; very irritating!

So the fix

You may only need to remove the following files:
.DS_Store (if it exists)
and then clean the project

If this doesn’t work try this:

  1. backup workspace .metadata file
  2. Restart eclipse
  3. In eclipse, from windows menu – preferences re-enter the android SDK location
  4. Now add your project as an existing android project

You might need to re-import any dependencies and mess about with the ordering after that but otherwise it should have done the job.