Reusing a running emulator

4 views
Skip to first unread message

brian.schimmel

unread,
Mar 27, 2009, 11:32:39 AM3/27/09
to Auto-android
Hi there,

I'm currently using andoid-ant and positron for building and unit
testing. The buidl.xml from the notepad example is set up in a way
that starts an emulator, installs the app, runs the test cases, and
lets the emulator die instantly.

I think that's fine for an automated testing on a dedicated build
machine, but for interactive work on my developement machine it's
tedious to watch the emulator boot and die all over again. It takes
some time to boot, much more time than the actual unit test.

I'd want the emulator to be killed only when I command it to, either
with <android:stop-emulator/> or manually by clicking the X in the
upper right. But as soon as the ant-target is finished, the emulator
is gone, too.

Even if I had long-lived emulators, targets like "install" or "run-
stories" must be sure that there is an emulator running. I think i'd
need to tell <android:start-emulator> to do nothing if an emulator is
already running, and only start one if there currently is none.

Are there ways to do this right now by changing my build.xml, or are
there missing features that should be implemented into android-ant, or
do you think I totally missed the point and there is no sense at all
in reusing the emulator?

best regards,
Brian Schimmel

phil.h.smith

unread,
Mar 27, 2009, 1:03:31 PM3/27/09
to Auto-android
Hi, Brian

I've just committed a small change (rev 127) to the notepad build, see
if this works for you: grab the that project from trunk (
http://autoandroid.googlecode.com/svn/trunk/samples/notepad ). In one
terminal, run 'ant demo'. After that spins up (do not close it by
hitting enter), start a new terminal and run 'ant run-stories'. You
should see the example stories fly by in the existing emulator,
leaving it alive when they finish.

This use case is pretty much the same as the one for eclipse users,
but we've taken eclipse out of the equation. To cater to that
scenario, when positron fires up (being called as a junit suite), it
immediately reinstalls your app before running the stories. For
details, see
http://code.google.com/p/autoandroid/source/browse/trunk/positron/src/main/java/com/googlecode/autoandroid/positron/Start.java
, which is called from the junit3 and junit4 superclasses.

I hope this demonstrates what you're looking for. I should note that
there's nothing magical about the 'demo' target: it should still work
if you manually fire up an emulator and just leave it running. The
one thing we can't currently do is start an emulator from within ant
and have it survive ant, since it requires OS process trickery that
doesn't port too cleanly across platforms. Given the other methods
available, this is hopefully fine.

There is a gotcha running around in here. The android emulator is not
very good about declaring itself to adb, which must happen if adb is
to control it. After firing up your emulator, you may want to run
'adb devices' and make sure it shows up. Otherwise various adb
commands may block, waiting for a (presumably booting) emulator that
will just never arrive.

Hope it helps! (And all feedback is welcome)
--phil--


On Mar 27, 8:32 am, "brian.schimmel" <brian.schim...@googlemail.com>
wrote:

brian.schimmel

unread,
Mar 30, 2009, 9:37:47 AM3/30/09
to Auto-android
Hi Phil,
thanks for your help!

Your commit reads "run-stories target no longer depends on install". I
don't think there is any problem with installing the app again, at
least I have no problem with it. The real problem seems to be that
"install" depends on "start-emulator", so whenever I want to install
the current version, the emulator is restarted.

The solution that works best for me now:
Removing depends="start-emulator" from all targets, so that I don't
end up with multiple emulators running at the same time. Of course, if
I forget to start an emulator before, and then I run e.g. "install",
it fails, but that's ok for me. I have to start an emulator manually,
if I want it to survive, and I can do this by "ant demo" or from
within Eclipse or whatever method I like. I do this once in the
morning when I start working, and if the emulator doesn't crash, I
reuse it all day long.

The nicest thing would be, of course, an Ant task that checks if an
emulator is already running, if not, starts one, and lets it live
forever. Then I would not have to remember starting the emulator once
a day.

> The
> one thing we can't currently do is start an emulator from within ant
> and have it survive ant, since it requires OS process trickery that
> doesn't port too cleanly across platforms.

Ahh, that's the problem here. If it's not possible to do it the clean
way, then I understand it would be wrong to include it in auto-
android. The current solution works for me.

best regards,
Brian Schimmel

Phil Smith

unread,
Mar 30, 2009, 11:56:57 AM3/30/09
to autoa...@googlegroups.com
Hi, Brian

Glad it helps!

On Mon, Mar 30, 2009 at 6:37 AM, brian.schimmel
<brian.s...@googlemail.com> wrote:
>
> Hi Phil,
> thanks for your help!
>
> Your commit reads "run-stories target no longer depends on install". I
> don't think there is any problem with installing the app again, at
> least I have no problem with it. The real problem seems to be that
> "install" depends on "start-emulator", so whenever I want to install
> the current version, the emulator is restarted.
>
> The solution that works best for me now:
> Removing depends="start-emulator" from all targets, so that I don't
> end up with multiple emulators running at the same time. Of course, if
> I forget to start an emulator before, and then I run e.g. "install",
> it fails, but that's ok for me. I have to start an emulator manually,
> if I want it to survive, and I can do this by "ant demo" or from
> within Eclipse or whatever method I like. I do this once in the
> morning when I start working, and if the emulator doesn't crash, I
> reuse it all day long.

I broke the dependency at 'install' since positron also inherently
re-installs your app. Meaning, if I had replaced "install" with
"start-emulator", the app itself would still find it's way into the
phone, when the tests themselves start. Given that redundancy, it
made sense to cut it there.

>
> The nicest thing would be, of course, an Ant task that checks if an
> emulator is already running, if not, starts one, and lets it live
> forever. Then I would not have to remember starting the emulator once
> a day.

Now that I think about it, we may be able to construct this effect:
'adb devices' should report any existing emulator (though it is
sometimes buggy.) Start emulator could be wrapped in a conditional
that tests 'adb devices' for emptiness. I'll see if I can't cook up a
simple impl later tonight.

phil.h.smith

unread,
Apr 1, 2009, 2:49:40 AM4/1/09
to Auto-android
Take a peek at http://autoandroid.googlecode.com/svn/trunk/samples/notepad/lib/android-ant.jar
and how I call start-emulator in notepad's demo target:
(at http://code.google.com/p/autoandroid/source/browse/trunk/samples/notepad/build.xml
)

<android:start-emulator only-if-not-running="true">
<arg line="-skin 320x480 -no-boot-anim"/>
</android:start-emulator>

That invocation will first check `adb devices` for any existing
entries, and will only start the emulator if it comes up empty-
handed. The default is false for back-compat.
Reply all
Reply to author
Forward
0 new messages