How to start android activity from eclipse with custom Intent - android

I would like, for multiple testing purposes, start my android activity from Eclipse with specific data on the intent (e.g. extra data like a file name to load).
Where in the menus can I provide this?
In the run configuration, there are nothing in the 3 tabs to provide any arguments
I could change some parameters in the resources files but I am afraid I might leak resources which will go to the final application.
It is possible to do it in adb: See here but it is not currently associatable with the F11 launch command in Eclipse that is useful for recompiling and relaunching at the same time.

If your still using eclipse you probably need need to create a simple ant script with a custom task to execute the test. ADB shell has a command to start activities where you can also specify extra's
am [start|instrument]
am start [-a <action>] [-d ]
[-t <mime_type>] [-c <category> [-c <category>] ...]
[-e <extra_key> <extra_value>
[-e <extra_key> <extra_value> ...]
[-n <component>] [-D] [<uri>]
am instrument [-e <arg_name> <arg_value>] [-p <prof_file>] [-w] <component>
You would pass them like this:
am start -a android.intent.action.VIEW -c android.intent.category.DEFAULT -e foo bar -e bert ernie -n org.package.name/.MyCustomActivity
P.S. don't forget the dot before the activity.
This can be translated to an ant target which you should put in the ant script.
<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="-e"/>
<arg value="extra_key extra_value"/>
<arg value="-n"/>
<arg value="{package.name}/{activity}"/>
</exec>
</target>
which you can execute like this: ant debug install run
How to run ant files from eclipse see:
Eclipse Help ant
Eclipse Help Application launcher

Related

Ant Build Failed

I am trying to build an Android Project using Apache Ant through Command Line but it is giving me Build Failed with the following error statements:
Java.io.IOException: Cannot run Program. "..\android_sdk\platform-tools\aapt.exe"
D:\MediaSync\build.xml:429: Execute failed: java.io.IOException: Cannot run program "..\android_sdk\platform-tools\aapt.exe"
I am new to this thing so i am not getting any idea how to get over this error. Searched alot about it but still scratching my head..
Anybody any idea about it then please tell me.
EDIT: Here are parts of my Buil.xml file
<!-- General SDK location -->
<property name="sdk-folder" value="D:/sdk" />
<!-- Preset tools location -->
<property name="android-platform-tools" value="${sdk-folder}/platform-tools"/>
<!-- Tools -->
<condition property="aapt" value="${android-platform-tools}/aapt.exe" else="${android-platform-tools}/aapt" >
<os family="windows"/>
</condition>
<!-- Generate the R.java file for this project's resources. -->
<target name="resource-src" depends="init, localization, replace-template-values">
<echo>Generating R.java / Manifest.java from the resources...</echo>
<exec executable="${aapt}" failonerror="true">
<arg value="package" />
<arg value="-m" />
<arg value="-J" />
<arg value="${outdir-r}" />
<arg value="-M" />
<arg value="${outdir.manifest}/AndroidManifest.xml" />
<arg value="-S" />
<arg value="${resource-dir}" />
<arg value="-I" />
<arg value="${android-jar}" />
</exec>
</target>
Make sure aapt.exe is installed at the correct path relative to your build.xml file (../android_sdk/platform-tools). That path is likely relative to your project, but not the build.xml.
I encountered the same problem on a 64-bit fedora platform and finally worked it out following this: http://blog.mx17.net/2012/10/25/android-on-ubuntu-ioexception-on-aapt/
It had failed because of trying to run a 32-bit program on an 64-bit architecture.
Maybe this would help.
Your error message says:
D:\MediaSync\build.xml:429: Execute failed: java.io.IOException: Cannot run program "..\android_sdk\platform-tools\aapt.exe"
while your aapt.exe is located in:
C:\android-sdk-windows\sdk\platform-tools\aapt.exe\
Obviously the build.xml tries to search D:\ for android_sdk\platform-tools\aapt.exe, which is the wrong path.
There should be a property for you to specify the path of aapt.exe, or it reads from system's PATH varibles.
To get a solution, post the lines around line 429 in build.xml. In line 429, there should be a <exec> task, be sure to include the whole content of that task (copy from <exec ...> to </exec>).
There is a bug in the intellij android studio and perhaps other IDEs that points to platform-tools for some executables/libs that now live in build-toos. A workaround for now is to make sim-links fof the necessary files.
So on linux/mac
platform-tools$ ln -s ../build-tools/android-version-blah/bin/appt appt
and
platform-tools$ ln -s ../build-tools/android-version-blah/lib lib
for Windows, install LinkExtensions and replace ln with mklink and read the help from typing mklink with no args. In Windows, the file would be appt.exe
A quick fix you can do is copy the aapt.exe and lib folder from the build-tools to the platform-tools folder.

Executing `android update project` fails in ANT, can't find sdkmanager.jar

When trying to execute an android update on a project i am getting the following error. Using linux.
Here is the ant code (where ${directory} is the directory of an android project):
<exec
executable="/bin/bash"
failonerror="true">
<arg value="android"/>
<arg value="update"/>
<arg value="project"/>
<arg value="-p"/>
<arg value="${directory}"/>
</exec>
Here is the error:
[exec] android: can't find sdkmanager.jar
I have tried changing the permissions of sdkmanager.jar to 777 and it still gives the same error.
When executing the same command from a bash or shell script it works fine. And when running on the terminal it works fine.
What is causing this problem and how can i fix it?
If the script you are calling already contains the !/bin/sh at the top of the script you do not need to call the /bin/bash executable in the ant exec.
Therefore, if i change the code posted in the question to this...
<exec
executable="android"
failonerror="true">
<arg value="update"/>
<arg value="project"/>
<arg value="-p"/>
<arg value="${directory}"/>
</exec>
... it works fine.
Thanks, hope this helps someone in the future.

Android built apk with ant

I want to compile a cm9 theme using ant so I have this code given in the sample theme :
<?xml version="1.0" encoding="UTF-8"?>
<project name="TemplateTheme" default="help">
<!-- ADD YOUR ANDROID SDK PATH TO THIS FILE -->
<loadproperties srcFile="local.properties" />
<!-- The ant.properties file can be created by you. It is only edited by the
'android' tool to add properties to it.
This is the place to change some Ant specific build properties.
Here are some properties you may want to change/update:
source.dir
The name of the source directory. Default is 'src'.
out.dir
The name of the output directory. Default is 'bin'.
For other overridable properties, look at the beginning of the rules
files in the SDK, at tools/ant/build.xml
Properties related to the SDK location or the project target should
be updated using the 'android' tool with the 'update' action.
This file is an integral part of the build system for your
application and should be checked into Version Control Systems.
-->
<property file="ant.properties" />
<!-- DON'T TOUCH THIS ONE -->
<loadproperties srcFile="default.properties" />
<!-- ADD YOUR KEYSTORE PROPERTIES HERE -->
<loadproperties srcFile="key.properties" />
<!-- quick check on sdk.dir -->
<fail
message="sdk.dir is missing. Make sure to generate local.properties using 'android update project'"
unless="sdk.dir"
/>
<!-- Import the actual build file.
To customize existing targets, there are two options:
- Customize only one target:
- copy/paste the target into this file, *before* the
<import> task.
- customize it to your needs.
- Customize the whole content of build.xml
- copy/paste the content of the rules files (minus the top node)
into this file, replacing the <import> task.
- customize to your needs.
***********************
****** IMPORTANT ******
***********************
In all cases you must update the value of version-tag below to read 'custom' instead of an integer,
in order to avoid having your file be overridden by tools such as "android update project"
-->
<!-- version-tag: 1 -->
<import file="${sdk.dir}/tools/ant/build.xml" />
<target name="-package-resources">
<echo>Optimizing theme resources</echo>
<exec executable="./prepare-resources" failonerror="true" />
<echo>Packaging theme resources</echo>
<exec executable="./aapt" failonerror="true">
<arg value="package" />
<arg value="-f" />
<arg value="-M" />
<arg path="AndroidManifest.xml" />
<arg value="-I" />
<arg path="${android.jar}" />
<arg value="-F" />
<arg path="${out.absolute.dir}/${resource.package.file.name}" />
<arg value="-S" />
<arg path="res-final" />
<arg value="-x" />
<arg value="10" />
</exec>
<echo>Cleaning up resources</echo>
<exec executable="./prepare-resources" failonerror="true">
<arg value="clean" />
</exec>
</target>
</project>
but when I execute I get the error :
cannnot run program ".\prepare-resources":Create Process Error=193, %1 is not a valid win32 Application
prepare-resources:
#!/bin/sh
if [ "$*" == "clean" ]; then
rm -rf res-final/
else
rm -rf res-final/
rsync -a res/ res-final/
echo "Themeing quasi-clone packages..."
rsync res-final/xml/com_cyanogenmod_trebuchet.xml res-final/xml/com_anddoes_launcher.xml
rsync res-final/xml/com_cyanogenmod_trebuchet.xml res-final/xml/com_teslacoilsw_launcher.xml
echo "Running pngcrush..."
for png in `find res-final/ -type f -name \*.png`; do
pngcrush -q $png $png.out 2>/dev/null >/dev/null && mv $png.out $png
done
fi
prepare-resources" is a shell script, not an executable. To be able to execute it, install cygwin, a UNIX-like environment that includes lots of software including shells capable of executing the script.
This page describes what packages you need and how to setup yourself.

debugging an app startup with android maven plugin

I'm working with the latest version of android maven plugin. I use android:deploy and android:run to launch my app on my device. When I want to debug, I go into DDMS in eclipse and check the green bug icon for my app. Everything works fine... but I can only debug a running app, ie it has already started. If I put a breakpoint in my application constructor or onCreate, it wont stop (obviously because the application has started and the debugging didn't been activated... yet).
So my question is: how can I start my app in debug mode directly from maven like I would do for an app that I start from eclipse ?
thanks ahead
Thanks to Lukasz, I found the command in Idea that is launched to trigger the debug mode.
Basically, the application is deployed with android:deploy but instead of android:run, I launch a shell "am" command with -D. Here's the usage for anyone interested.
ovh-ybi$ adb shell
# am
usage: am [subcommand] [options]
start an Activity: am start [-D] <INTENT>
-D: enable debugging
send a broadcast Intent: am broadcast <INTENT>
start an Instrumentation: am instrument [flags] <COMPONENT>
-r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT)
-e <NAME> <VALUE>: set argument <NAME> to <VALUE>
-p <FILE>: write profiling data to <FILE>
-w: wait for instrumentation to finish before returning
start profiling: am profile <PROCESS> start <FILE>
stop profiling: am profile <PROCESS> stop
<INTENT> specifications include these flags:
[-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
[-c <CATEGORY> [-c <CATEGORY>] ...]
[-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
[--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
[-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
[-n <COMPONENT>] [-f <FLAGS>] [<URI>]
Instead of android:deploy and android:run you could just launch your application in debug by eclipse and not by maven. You have to have your project configured properly to do this.
You can deploy and start it using this post. To debug I guess and hope you only need to change some parameters.

Use ant (build.xml) to compile jni directory

I'm using ant to build my android app (ant compile, ant install and so on) and I'm adding some NDK sources (inside jni/ directory) and I want to have a rule to recompile the jni/ directory with ant and THEN install my app into my device/emulator, like this:
$ ant jni-and-install
Instead of:
$ make -f jni/Makefile APP=myapp
(jni compilation & installation)
...
$ ant install
I was trying to find the place to put my rules in build.xml but I don't know where.
What should I edit for ant to call make on jni/ directory before installing into device?
You could use the exec ant task :
<target name="install">
<!-- ... -->
</target>
<target name="jni">
<exec executable="make">
<arg line="-f jni/Makefile APP=myapp"/>
</exec>
</target>
<target name="jni-and-install" depends="jni, install"/>
You can use an exec ant task for this. I think putting the following in your -pre-build target in build.xml will do the trick:
<exec executable="make" failonerror="true">
<arg line="-f jni/Makefile APP=myapp"/>
</exec>
Edit: JB's solution allows for things like ant jni to build the jni code without building the rest of your project. You can get the best of both worlds by wrapping the above in a target named "jni" and then adding depends="jni" to your -pre-build target.

Categories

Resources