How to run an android app on the device with ant-ThrowExceptions

Exception or error:

I would like to be able to launch my app after installation with ant just as happens when pressing the run button in eclipse.

Is there an existing ant task after creating a project on the command line or is there a command I could execute with ant?

How to solve:

executing adb shell am start -a android.intent.action.MAIN -n <Package_name>/.<Activity> from ant should launch your app.ofcourse you need to build and install the app trying to start the app…you can use android build xml to build and use “adb install ” to install the app…

###

Using the command provided by Navin I was able to create this ant target:

<target name="run">
    <exec executable="adb">
        <arg value="shell"/>
        <arg value="am"/>
        <arg value="start"/>
        <arg value="-a"/>
        <arg value="android.intent.action.MAIN"/>
        <arg value="-n"/>
        <arg value="{package.name}/{activity}"/>
    </exec>
</target>

On the command line I execute:

ant debug install run

And it all works swimmingly.

EDIT

As WarrenFaith helpfully pointed out in the comments {activity} should be the class name of main activity with a . prefix.

So a complete example of the value of the last arg would be

org.package.name/.MyCustomActivity

###

Generally, copy following target to your build.xml or custom_rules.xml. Note that in custom_rules.xml (if it doesn’t yet exist) you need to wrap this in a element.

<target name="start">
    <xpath input="AndroidManifest.xml"
           expression="/manifest/@package"
           output="manifest.package" />
    <xpath input="AndroidManifest.xml"
           expression="/manifest/application/activity[intent-filter/action/@android:name='android.intent.action.MAIN']/@android:name"
           output="manifest.main" />
    <echo level="info">Restart main activity ${manifest.package}/.${manifest.main}</echo>
    <exec executable="${android.platform.tools.dir}/adb">
        <arg value="shell"/>
        <arg value="am"/>
        <arg value="start"/>
        <arg value="-S"/>
        <arg value="-a"/>
        <arg value="android.intent.action.MAIN"/>
        <arg value="-n"/>
        <arg value="${manifest.package}/.${manifest.main}"/>
    </exec>
</target>

###

<target name="run">
    <exec executable="adb">
        <arg value="shell"/>
        <arg value="am"/>
        <arg value="start"/>
        <arg value="-a"/>
        <arg value="android.intent.action.MAIN"/>
        <arg value="-n"/>
        <arg value="{package.name}/{activity}"/>
    </exec>
</target>

I just want to say that {package.name} should be equal to <manifest>‘s package and {activity} should be the full qualified name of the main activity (i.e. with its package name, e.g. com.example.activty.MainActivty).

Leave a Reply

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