Native executable in the APK

2,482 views
Skip to first unread message

Felipe Monteiro de Carvalho

unread,
Dec 1, 2010, 8:45:04 AM12/1/10
to android-ndk
Hello,

I would like to add my native Pascal executable to the APK and then
run it from the Java application, but I don't know how to do it. More
precisely:

1> If I just create a res/raw folder in my project and add my
executable.mp3 (to fool the compression) it will be automatically
added to the APK or do I need to register that I added this file
somewhere?

2> When my APK is installed, where will the resource files be placed?
I think that if they are placed somewhere without running permissions
I won't be able to run my file.

3> Can I somehow change the APK installation to process to copy my
executable to /usr/bin or somewhere else with execution permissions
during installation?

4> Which kind of ARM is used in the available phones? Can I run either
THUMB or ARMv5? Is it endian little? Is it EABI4 or 5?

thanks!

alan

unread,
Dec 1, 2010, 12:32:53 PM12/1/10
to android-ndk
you might be better off trying to compile your pascal code into a
library using gcc then using it as normal

On Dec 1, 1:45 pm, Felipe Monteiro de Carvalho

Onur Cinar

unread,
Dec 1, 2010, 12:43:08 PM12/1/10
to andro...@googlegroups.com

Hi Felipe,

The resource approach won't work, since they are not extracted from the APK file after the installation. However, you can do the following:

Rename your pascal executable as "libpascal.so",  and copy it to libs/armeabi directory in your project.

Do a "ant debug" to generate the apk file.  With "unzip -t" check that the libpascal.so is in the libs/armeabi directory in the APK file as well.  Then you can install the APK to the device.

To double check that your pascal application is working, do the following:

adb shell run-as com.my.package lb/libpascal.so

This should run your pascal executable on the device, and you should see some output if everything goes well.

Regards,

-onur


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.

---
www.zdo.com

Chris Stratton

unread,
Dec 1, 2010, 3:14:55 PM12/1/10
to android-ndk
On Dec 1, 12:43 pm, Onur Cinar <onur.ci...@gmail.com> wrote:

> To double check that your pascal application is working, do the following:
>
> adb shell run-as com.my.package lb/libpascal.so

It's probably worth trying this with a copy of one of the toolbox
tools or something before investing in building a custom application,
as it may not work. My phone seems to lack this "run-as" tool.

What's going to be required is either a way to launch an executable
that doesn't have it's execute bit set, or copying the executable to
someplace that is writeable and not mounted no-exec, and setting the
execute bit on the copy.

This only officially supported method is to compile the program as a
library (rather than an executable named to look like a library) and
invoke it via jni.

Felipe Monteiro de Carvalho

unread,
Dec 2, 2010, 6:44:33 AM12/2/10
to android-ndk
On Dec 1, 9:14 pm, Chris Stratton <cs07...@gmail.com> wrote:
> My phone seems to lack this "run-as" tool.

Which phone do you have?

thanks,

David Turner

unread,
Dec 2, 2010, 7:02:59 AM12/2/10
to andro...@googlegroups.com
On Wed, Dec 1, 2010 at 9:14 PM, Chris Stratton <cs0...@gmail.com> wrote:
On Dec 1, 12:43 pm, Onur Cinar <onur.ci...@gmail.com> wrote:

> To double check that your pascal application is working, do the following:
>
> adb shell run-as com.my.package lb/libpascal.so

It's probably worth trying this with a copy of one of the toolbox
tools or something before investing in building a custom application,
as it may not work.  My phone seems to lack this "run-as" tool.

"run-as" comes with Froyo, it can be used to run a command as a specific application/user ID, *only* if your package is debuggable.
This is very useful for debugging (e.g. running a command through adb shell, or looking at your application's generated files during development).
 
What's going to be required is either a way to launch an executable
that doesn't have it's execute bit set, or copying the executable to
someplace that is writeable and not mounted no-exec, and setting the
execute bit on the copy.

This only officially supported method is to compile the program as a
library (rather than an executable named to look like a library) and
invoke it via jni.

Android does not officially support an application launching an arbitrary process (even under its own userID) at the moment.
What this means is that the system does not really monitor which standalone process is launched by which application.
If the system sees one "unregistered process" running (i.e. one that is not a known system daemon, an application proces, etc...) it may very well decide to terminate / kill it immediately.

The reason why this isn't handled is because it introduces plenty of subtle resource accounting issues, among other things.
 
For now, we still provide applications with the ability to call "system()" or "fork/exec", but there is no guarantee that these will work as easily in the future.

Felipe Monteiro de Carvalho

unread,
Dec 2, 2010, 9:09:49 AM12/2/10
to android-ndk
On Dec 1, 6:32 pm, alan <a...@birtles.org.uk> wrote:
> you might be better off trying to compile your pascal code into a
> library using gcc then using it as normal

I don't think it is possible to compile my applications with GCC, I am
using the Free Pascal compiler.

Converting the application into a library is a solution, but I don't
like it. This requires changing the source code, changing the
initialization code and in general can bring all sords of problems to
debugging and developing.

Plus, my objective here is not to port 1 application, but rather to
port a Pascal framework to Android to allow people to port their
Pascal application to Android. If people have to change that much each
application it becomes something which isn't viable. Think that many
developers work alone in their software, we simply do not have a 20-
people team to maintain a version of the application for each
platform. So how can we write cross-platform applications then? Until
now this has worked perfectly: Simply use a cross-platform framework +
some ifdefs if users require some tweaks for each platform and just
rebuild the application for each platform and ready to go! But if the
application needs substancial changes for each platform it is simply
not viable for most people. It would be really excelent if Android
starts supporting native applications like MeeGo and the iPhone
support.

thanks!

Felipe Monteiro de Carvalho

unread,
Dec 2, 2010, 9:42:15 AM12/2/10
to android-ndk
On Dec 1, 6:43 pm, Onur Cinar <onur.ci...@gmail.com> wrote:
> The resource approach won't work, since they are not extracted from the APK
> file after the installation. However, you can do the following:
>
> Rename your pascal executable as "libpascal.so",  and copy it to
> libs/armeabi directory in your project.

Ok, thanks a lot! This one got me going. Indeed just by placing the
executable in the mentioned folder with the different name it got
included in the apk.

By the way, what is the full path to the library inside the internal
memory? I am searching but I haven't found it yet ...

> adb shell run-as com.my.package lb/libpascal.so

This one doesn't work with:

[felipe@localhost tools]$ ./adb shell run-as com.pascalnotes lb/
libpascalnotes4android.so
run-as: permission denied

But I'm still confident that my Java application should be able to run
the application as soon as I find out where the application is ...

I am googling for "android directory structure" and similar but I
still haven't found documentation explaining what each directory
represents. I suppose it doesn't follow the Linux Standard Base.

thanks,

David Turner

unread,
Dec 2, 2010, 10:16:28 AM12/2/10
to andro...@googlegroups.com
On Thu, Dec 2, 2010 at 3:42 PM, Felipe Monteiro de Carvalho <felipemonte...@gmail.com> wrote:
On Dec 1, 6:43 pm, Onur Cinar <onur.ci...@gmail.com> wrote:
> The resource approach won't work, since they are not extracted from the APK
> file after the installation. However, you can do the following:
>
> Rename your pascal executable as "libpascal.so",  and copy it to
> libs/armeabi directory in your project.

Ok, thanks a lot! This one got me going. Indeed just by placing the
executable in the mentioned folder with the different name it got
included in the apk.

By the way, what is the full path to the library inside the internal
memory? I am searching but I haven't found it yet ...

> adb shell run-as com.my.package lb/libpascal.so

This one doesn't work with:

[felipe@localhost tools]$ ./adb shell run-as com.pascalnotes lb/
libpascalnotes4android.so
run-as: permission denied

You need to make your application debuggable, i.e. set android:debuggable="true" in the <android> element of your AndroidManifest.xml

Also, it is lib/libpascalnotes4android.so instead of lb/libpascalnotes4android.so

But I'm still confident that my Java application should be able to run
the application as soon as I find out where the application is ...

I am googling for "android directory structure" and similar but I
still haven't found documentation explaining what each directory
represents. I suppose it doesn't follow the Linux Standard Base.

thanks,

Felipe Monteiro de Carvalho

unread,
Dec 2, 2010, 10:42:01 AM12/2/10
to android-ndk
On Dec 2, 4:16 pm, David Turner <di...@android.com> wrote:
> android:debuggable="true" in the <android> element of your
> AndroidManifest.xml

I suppose you ment in the <application> element, as there is no
<android> element:

http://developer.android.com/guide/topics/manifest/application-element.html

> Also, it is *lib*/libpascalnotes4android.so instead of *lb*
> /libpascalnotes4android.so

Still no luck:

[felipe@localhost tools]$ ./adb shell run-as com.pascalnotes lib/
libpascalnotes4android.so
run-as: permission denied

Anyway, any ideas where the library is located in the file system?

thanks!

David Turner

unread,
Dec 2, 2010, 11:09:46 AM12/2/10
to andro...@googlegroups.com
On Thu, Dec 2, 2010 at 4:42 PM, Felipe Monteiro de Carvalho <felipemonte...@gmail.com> wrote:
On Dec 2, 4:16 pm, David Turner <di...@android.com> wrote:
> android:debuggable="true" in the <android> element of your
> AndroidManifest.xml

I suppose you ment in the <application> element, as there is no
<android> element:

http://developer.android.com/guide/topics/manifest/application-element.html

Ah yes, sorry :-)
 
> Also, it is *lib*/libpascalnotes4android.so instead of *lb*
> /libpascalnotes4android.so

Still no luck:

[felipe@localhost tools]$ ./adb shell run-as com.pascalnotes lib/
libpascalnotes4android.so
run-as: permission denied

 
Anyway, any ideas where the library is located in the file system?


The library is located under /data/data/<package-name>/lib/ , but run-as should cd to /data/data/<package-name>/ automatically for you.
Hmmm, can you do: adb shell run-as com.pascalnotes ls -l lib
and dump the output here?

thanks!

Chris Stratton

unread,
Dec 2, 2010, 4:45:57 PM12/2/10
to android-ndk
On Dec 2, 7:02 am, David Turner <di...@android.com> wrote:

>"run-as" comes with Froyo, it can be used to run a command as a specific
>application/user ID, *only* if your package is debuggable.

It's not actually the launching as the user that I find interesting...
it's how this tool manages to execute a non-executable file.
Presumably something like the 'abuse the dynamic linker' trick from
other linuxes?

Onur Cinar

unread,
Dec 2, 2010, 4:53:10 PM12/2/10
to andro...@googlegroups.com

Hi Chris,

It is doing a setresgid/setresuid to target uid/gid and then execvp the requested process. So you can only run the executables, not the libraries directly.

Here is the source code for run-as, it is very short:
http://android.git.kernel.org/?p=platform/system/core.git;a=blob_plain;f=run-as/run-as.c;hb=HEAD

Regards,

-onur



--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.

---
www.zdo.com

Chris Stratton

unread,
Dec 2, 2010, 5:54:38 PM12/2/10
to android-ndk
execvp will run something that doesn't have its execute bit set ?

On Dec 2, 4:53 pm, Onur Cinar <onur.ci...@gmail.com> wrote:
> Hi Chris,
>
> It is doing a setresgid/setresuid to target uid/gid and then execvp the
> requested process. So you can only run the executables, not the libraries
> directly.
>
> Here is the source code for run-as, it is very short:http://android.git.kernel.org/?p=platform/system/core.git;a=blob_plai...
>
> Regards,
>
> -onur
>
>
>
> On Thu, Dec 2, 2010 at 1:45 PM, Chris Stratton <cs07...@gmail.com> wrote:
> > On Dec 2, 7:02 am, David Turner <di...@android.com> wrote:
>
> > >"run-as" comes with Froyo, it can be used to run a command as a specific
> > >application/user ID, *only* if your package is debuggable.
>
> > It's not actually the launching as the user that I find interesting...
> > it's how this tool manages to execute a non-executable file.
> > Presumably something like the 'abuse the dynamic linker' trick from
> > other linuxes?
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "android-ndk" group.
> > To post to this group, send email to andro...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > android-ndk...@googlegroups.com<android-ndk%2Bunsu...@googlegroups.com>
> > .

Onur Cinar

unread,
Dec 2, 2010, 6:00:52 PM12/2/10
to andro...@googlegroups.com

Hi Chris,

I don't think so. It should fail and set the errno to EACCES in that case if the file does not have the execute permission set.

Regards,

-onur



To unsubscribe from this group, send email to android-ndk...@googlegroups.com.

Chris Stratton

unread,
Dec 2, 2010, 6:06:32 PM12/2/10
to android-ndk
Yes, that's what I would expect to.

And this seems important, because (at least in cupcake where I last
looked into this) an executable deployed by pretending to be a library
doesn't get it's execute bit set. And the automatically deployed copy
is not writable by the application, so that can't be changed.

This would seem to indicate that one must manually copy the executable
someplace else, or use some "trick" to start it such as the old
dynamic linker abuse trick of many dekstop linuxes.

On Dec 2, 6:00 pm, Onur Cinar <onur.ci...@gmail.com> wrote:
> Hi Chris,
>
> I don't think so. It should fail and set the errno to EACCES in that case if
> the file does not have the execute permission set.
>
> Regards,
>
> -onur
>
> > <android-ndk%2Bunsu...@googlegroups.com<android-ndk%252Buns...@googlegroups.com>

Felipe Monteiro de Carvalho

unread,
Dec 3, 2010, 4:03:45 AM12/3/10
to android-ndk
On Dec 2, 5:09 pm, David Turner <di...@android.com> wrote:
> The library is located under /data/data/<package-name>/lib/ , but run-as
> should cd to /data/data/<package-name>/ automatically for you.
> Hmmm, can you do: adb shell run-as com.pascalnotes ls -l lib
> and dump the output here?

I think that the keyword here is Froyo ( 2.2, correct? There names
instead of numbers always confuse me ... and each platform comes with
dozens of them...)

I am using HTC Wildfire = Android 2.1, so I suppose that run-as
doesn't exist there.

The more pressing issue now is the /data directory. It seams to be
completely unacessible? Who is the "system" user? Any ideas about how
to get access to /data ? I couldn't read it's contents neither with OI
File Manager nor with adb shell =(

user-188-33-180-18:tools felipe$ ./adb shell run-as com.pascalnotes ls
-l lib
run-as: permission denied
user-188-33-180-18:tools felipe$ ./adb shell
$ ls -l
drwxrwxrwt root root 2010-12-03 09:22
sqlite_stmt_journals
dr-x------ root root 2010-11-21 14:11 config
d---rwxr-x system sdcard_rw 1970-01-01 01:00 sdcard
lrwxrwxrwx root root 2010-11-21 14:11 d -> /sys/
kernel/debug
lrwxrwxrwx root root 2010-11-21 14:11 etc -> /system/
etc
drwxrwx--- system cache 2010-12-02 21:41 cache
drwxr-xr-x root root 2010-11-14 00:52 system
drwxr-xr-x root root 1970-01-01 01:00 sys
drwxr-x--- root root 1970-01-01 01:00 sbin
dr-xr-xr-x root root 1970-01-01 01:00 proc
-rw-r--r-- root root 11336 1970-01-01 01:00 logo.rle
-rwxr-x--- root root 14664 1970-01-01 01:00 init.rc
-rwxr-x--- root root 1677 1970-01-01 01:00
init.goldfish.rc
-rwxr-x--- root root 3608 1970-01-01 01:00 init.buzz.rc
-rwxr-x--- root root 107668 1970-01-01 01:00 init
-rw-r--r-- root root 118 1970-01-01 01:00 default.prop
drwxrwx--x system system 2010-05-27 23:07 data
drwx------ root root 2010-08-10 12:54 root
drwxr-xr-x root root 2010-11-21 14:11 dev
$ cd data
$ ls -l
opendir failed, Permission denied
$

thanks,

Chris Stratton

unread,
Dec 3, 2010, 7:49:04 AM12/3/10
to android-ndk
On Dec 3, 4:03 am, Felipe Monteiro de Carvalho
<felipemonteiro.carva...@gmail.com> wrote:

> The more pressing issue now is the /data directory. It seams to be
> completely unacessible? Who is the "system" user? Any ideas about how
> to get access to /data ? I couldn't read it's contents neither with OI
> File Manager nor with adb shell =(

Much of /data is readable, but not listable. It's apparently a rather
silly "security" measure.

There's nothing you can really do to change that, short of rooting the
phone or installing a modified version of android.

The system user ID is a semi-privileged one used by various components
of the platform - for installing applications and doing some kinds of
hardware access amongst other things. You should not be able to run
code as this user ID, unless you are making a modified version of
android.

Felipe Monteiro de Carvalho

unread,
Dec 3, 2010, 10:15:17 AM12/3/10
to android-ndk
On Dec 3, 1:49 pm, Chris Stratton <cs07...@gmail.com> wrote:
> Much of /data is readable, but not listable.  It's apparently a rather
> silly "security" measure.

Interresting, I didn't even know this is possible as the usual rwx-rwx-
rwx permissions don't have an option for listing. But you are correct,
I can cd /data/data/com.pascalnotes/lib and then do ls inside it.

In fact this directory seamed to be the perfect place-holder for the
executable, but from my Java app I to copy the library, but had no
luck there:

1> cp doesn't seam to exist, so I couldn't try this one
2> FileOutputStream("/data/data/com.pascalnotes/lib/
pascalnotes4android") throws:
FileNotFoundException - if the file exists but is a directory rather
than a regular file, does not exist but cannot be created, or cannot
be opened for any other reason

I assume that I cannot create the file in this directory...

Any ideas of where to store the file inside the internal memory? Maybe
some kind of directory for cache?

thanks,

Dianne Hackborn

unread,
Dec 3, 2010, 12:25:49 PM12/3/10
to andro...@googlegroups.com
Why don't you just put them in your files directory use returned by the various Context APIs like Context.getFilesDir()?

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.




--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Felipe Monteiro de Carvalho

unread,
Dec 6, 2010, 8:33:36 AM12/6/10
to android-ndk
On Dec 3, 6:25 pm, Dianne Hackborn <hack...@android.com> wrote:
> Why don't you just put them in your files directory use returned by the
> various Context APIs like Context.getFilesDir()?

Thank you very much!!! =)

If anyone is interested, the code which succeeds in executing a Pascal
application and getting it's pipes output was placed here:

http://p-tools.svn.sourceforge.net/viewvc/p-tools/PascalNotes4Android/src/com/pascalnotes/PascalNotes4Android.java?view=markup&pathrev=190

The directory which getFilesDir returns is /data/data/com.pascalnotes/
and copying the executable there and running it was successful =)

I still haven't checked how it works for an application which runs
longer, however, so I don't know if David Turner's assertion that the
system might decide to kill it for no reason in fact happens or not.

thanks,

Felipe Monteiro de Carvalho

unread,
Jan 12, 2011, 8:05:35 AM1/12/11
to android-ndk
Hello,

1 month ago I managed to get a basic Pascal application running and
communicating with the Java side via pipes.

Now I started creating a larger application, and it seams that it
works fine, but I keep getting strange message in logcat. Could
someone please confirm if these message mean that Android is trying to
kill my Pascal program? Luckly it seams that it fails.

W/ResourceType( 349): getEntry failing because entryIndex 636 is
beyond type entryCount 109
W/ResourceType( 349): Failure getting entry for 0x7f02027c (t=1
e=636) in package 0: 0x80000001
W/ResourceType( 349): getEntry failing because entryIndex 332 is
beyond type entryCount 109
W/ResourceType( 349): Failure getting entry for 0x7f02014c (t=1
e=332) in package 0: 0x80000001
D/PhoneWindow( 1232): DebugMonitor
class=com.pascal.turbochessclock.TurboChessClock4Android focus=true
I/ActivityManager( 89): Displayed activity
com.pascal.turbochessclock/.TurboChessClock4Android: 1220 ms (total
1220 ms)
D/HtcWidgetScanner( 1225): action -
android.intent.action.PACKAGE_REMOVED
D/HtcWidgetScanner( 1225): packageUid - 10071
W/ResourceType( 349): getEntry failing because entryIndex 636 is
beyond type entryCount 109
W/ResourceType( 349): Failure getting entry for 0x7f02027c (t=1
e=636) in package 0: 0x80000001
W/ResourceType( 349): getEntry failing because entryIndex 332 is
beyond type entryCount 109
W/ResourceType( 349): Failure getting entry for 0x7f02014c (t=1
e=332) in package 0: 0x80000001
D/HtcWidgetScanner( 1225): action -
android.intent.action.PACKAGE_ADDED
D/HtcWidgetScanner( 1225): packageUid - 10071
D/PackageInstallationReceiver( 204): Removing.../data/local/tmp/
com.pascal.turbochessclock.apk
E/PackageInstallationReceiver( 204): Remove /data/local/tmp/
com.pascal.turbochessclock.apk Fail!
W/System.err( 204): java.io.IOException: Error running exec().
Commands: [/system/xbin/su, 0, /system/bin/rm, /data/local/tmp/
com.pascal.turbochessclock.apk] Working Directory: null Environment:
null
W/System.err( 204): at
java.lang.ProcessManager.exec(ProcessManager.java:196)
W/System.err( 204): at java.lang.Runtime.exec(Runtime.java:225)
W/System.err( 204): at java.lang.Runtime.exec(Runtime.java:313)
W/System.err( 204): at java.lang.Runtime.exec(Runtime.java:246)
W/System.err( 204): at
com.htc.android.psclient.PackageInstallationReceiver.removeTempPackageFile(PackageInstallationReceiver.java:
34)
W/System.err( 204): at
com.htc.android.psclient.PackageInstallationReceiver.access
$000(PackageInstallationReceiver.java:8)
W/System.err( 204): at
com.htc.android.psclient.PackageInstallationReceiver
$1.run(PackageInstallationReceiver.java:24)
W/System.err( 204): Caused by: java.io.IOException: No such file or
directory
W/System.err( 204): at java.lang.ProcessManager.exec(Native
Method)
W/System.err( 204): at
java.lang.ProcessManager.exec(ProcessManager.java:194)
W/System.err( 204): ... 6 more
D/vending ( 412): [40] LocalAssetCache.updateOnePackage(): No local
info for com.pascal.turbochessclock
D/vending ( 412): [41] LocalAssetCache.updateOnePackage(): No local
info for com.pascal.turbochessclock

Also, any idea where I could post a feature request to have official
support for running native programs in Android?

thanks!

Felipe Monteiro de Carvalho

On Dec 6 2010, 2:33 pm, Felipe Monteiro de Carvalho
<felipemonteiro.carva...@gmail.com> wrote:
> On Dec 3, 6:25 pm, Dianne Hackborn <hack...@android.com> wrote:
>
> > Why don't you just put them in your files directory use returned by the
> > various Context APIs like Context.getFilesDir()?
>
> Thank you very much!!! =)
>
> If anyone is interested, the code which succeeds in executing a Pascal
> application and getting it's pipes output was placed here:
>
> http://p-tools.svn.sourceforge.net/viewvc/p-tools/PascalNotes4Android...
Reply all
Reply to author
Forward
0 new messages