Android on Intellij Idea – run activity on emulator

The official Android plugin is only for Eclipse, but thankfully Android provides Ant support for those of us who prefer other IDEs (http://developer.android.com/guide/developing/other-ide.html).

I use Intellij IDEA, and the main problem for me was that, after building the application using the Ant build script created by activitycreator tool, you have to manually reload the application in the emulator.

This is easily fixed by tweaking the generated Ant build file.
Add the following target to the build.xml –

 <!-- Main activity -->
<property name="main-activity" value="MyMainActivity" />

 <!-- Run the main Activity after reinstall -->
 <target name="run" depends="reinstall">
 <echo>Running ${application-package}.${main-activity} on default emulator...</echo>
 <exec executable="${adb}" failonerror="true">
 <arg value="shell" />
 <arg value="am" />
 <arg value="start" />
 <arg value="-a" />
 <arg value="android.intent.action.MAIN" />
 <arg value="-n" />
 <arg value="${application-package}/${application-package}.${main-activity}" />
 </exec>
 </target>

MyMainActivity is, of course, your main activity. Remember to change the build file if you rename or change the main activity.

I also changed the default target to run, and after loading build.xml as an Ant build file in IDEA, I assigned it a shortcut key.
I also made these changes to ANDROID_SDK/tools/lib/build.template, so from now on, when I create a new Android project, it is already set up this way. If you are editing the template file, use –

 <!-- Main activity -->
<property name="main-activity" value="ACTIVITY_NAME" />

This will automatically pick up the class name you specify on the command line when running activitycreator.

2 comments

  1. Dmitry! Wow! I have known your name for years from all the IntelliJ IDEA announcements and JIRA issues, never thought I’d see you on my blog :)

    I ran into a couple of issues with the Android plugin – one was that it starts up a new instance of emulator each time it is run, and the other was that sometimes it mysteriously fails, without any error messages.

    I will try tomorrow to see if the issues still exist with the latest version of the plugin. If they do, I will report them on the issue tracker.

Leave a Reply

Your email address will not be published. Required fields are marked *


− 4 = four

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>