SWIG and Android Development Help

846 views
Skip to first unread message

Ashish

unread,
Aug 5, 2011, 1:02:42 AM8/5/11
to android-ndk
Hi All,

Wondering if someone can help me. I am going around in circles with no
progress. I am trying to achieve the following:
1. I have lots of code written in C/C++.
2. I use SWIG to generate Java wrapper files.
3. I then compile the C/C++ code to generate library.so.
4. Finally I wish to use this library.so with an Android application.

So far I have been able to compile my C/C++ code using arm-none-linux-
gnueabi-g++ compiler and able to generate a library.so.

Using Eclipse Helios Service Release 2 I write a test JAVA application
with the aim of:
1. Loading the library.so using System.loadLibrary("rary.so")
2. Calling a C/C++ function.

However as per logs from Dalvik Debug Monitor, loading of the library
fails and my android app crashes when I try to call the C/C++
function. The error I get is:
No implementation found for native Loa/as/apiJNI;.swig_module_init ()V

I am not sure if the issue is with generating the library or the way
the library is loaded or the general flow. I have also downloaded
android ndk version r5b which has the arm-eabi-4.4.0 version but to no
success. Note that the new version of NDK (version 6 onwards do not
include the arm-eabi compilers). I am using Eclipse Helios Service
Release 2 for Android development.

Can someone please help me? I have searched a lot on Google and there
are mixed answers and I am finding that I am going around in circles.
If someone can point me to an example or guide me it will be much
appreciated.

My OS(s) are Win XP and Ubuntu 11.04.

Thanks
Ash

David Turner

unread,
Aug 5, 2011, 10:38:20 AM8/5/11
to andro...@googlegroups.com
On Fri, Aug 5, 2011 at 7:02 AM, Ashish <ana...@gmail.com> wrote:
Hi All,

Wondering if someone can help me. I am going around in circles with no
progress. I am trying to achieve the following:
1. I have lots of code written in C/C++.
2. I use SWIG to generate Java wrapper files.
3. I then compile the C/C++ code to generate library.so.
4. Finally I wish to use this library.so with an Android application.

So far I have been able to compile my C/C++ code using arm-none-linux-
gnueabi-g++ compiler and able to generate a library.so.

The "gnueabi" means that the code is generated to work with GNU GLibc, which is not available on an Android system.
I seriously recommend that you use the Android NDK standalone toolchain instead (see docs/STANDALONE-TOOLCHAIN.html in the NDK package).
 
Using Eclipse Helios Service Release 2 I write a test JAVA application
with the aim of:
1. Loading the library.so using System.loadLibrary("rary.so")
2. Calling a C/C++ function.

However as per logs from Dalvik Debug Monitor, loading of the library
fails and my android app crashes when I try to call the C/C++
function. The error I get is:
No implementation found for native Loa/as/apiJNI;.swig_module_init ()V

I am not sure if the issue is with generating the library or the way
the library is loaded or the general flow. I have also downloaded
android ndk version r5b which has the arm-eabi-4.4.0 version but to no
success. Note that the new version of NDK (version 6 onwards do not
include the arm-eabi compilers). I am using Eclipse Helios Service
Release 2 for Android development.

Can someone please help me? I have searched a lot on Google and there
are mixed answers and I am finding that I am going around in circles.
If someone can point me to an example or guide me it will be much
appreciated.

My OS(s) are Win XP and Ubuntu 11.04.

Thanks
Ash

--
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.


Ashish

unread,
Aug 5, 2011, 9:54:00 PM8/5/11
to android-ndk
Hi David,
Thanks for the info. I did try the Android NDK and was getting lots of
errors when compiling. Let me read through the document and see how I
go.

Thanks
Ash

On Aug 6, 12:38 am, David Turner <di...@android.com> wrote:

Ashish

unread,
Aug 8, 2011, 7:00:53 AM8/8/11
to android-ndk
Hi David,

Wondering if you can provide your feedback. Instead of diving into the
deep with our large C/C++ code, I thought I will write a very basic C+
+ class to get started. I created the following:

newtestclass.h
-------------------
#ifndef NEWTESTCLASS_H
#define NEWTESTCLASS_H

class NewTestClass
{
public:
NewTestClass();
~NewTestClass();
void setX(const int x);
int getX() const;
private:
int m_x;
};

#endif // NEWTESTCLASS_H

newtestclass.cpp
----------------------
#include "newtestclass.h"
NewTestClass::NewTestClass() : m_x(0)
{
}

NewTestClass::~NewTestClass()
{
}

void NewTestClass::setX(const int x)
{
m_x = x;
}

int NewTestClass::getX() const
{
return m_x;
}

I followed the instructions in docs/STANDALONE-TOOLCHAIN.html and was
able to successfully compile the newtestclass.cpp. Note that I used
Ubuntu to generate the library and jar.

After which I went ahead and created a SWIG interface file for this
class. The contents is as follows:

newtestclass.i
-------------------
%module newtestclassmodule
%{
#include "newtestclass.h"
%}
%include "newtestclass.h"

Again very simple. On this interface class, I invoke the swig tool as
follows:
swig -c++ -o newtestclass_wrap.cpp -package com.example.newtest -java
newtestclass.i
which generates the file newtestclass_wrap.cpp.

I went ahead and created a library.so and contents.jar file in Ubuntu.
I then opened up Eclipse (in Windows) and created a new project in
which I loaded this library and jar file. I wrote a test java class in
which I try to load the library as follow:

public class testActivity extends Activity
{
private static final String LOGTAG = "LoginActivity";
static
{
try
{
Log.i(LOGTAG, "Trying to load libMult.so");
System.loadLibrary("audioserver.so");
}
catch (UnsatisfiedLinkError ule)
{
Log.e(LOGTAG, "WARNING: Could not load
libaudioserver.so");
}
}

....
....
}

However, when I try to run the application I can see in the Delvik
Debug Monitor that loading of the library failed.
My questions:
1. How can I check whether the library.so that I have created is
compatible with Android. I am using NDK r6 (arm-none-linux-androideabi-
gcc and arm-none-linux-androideabi-g++)
2. Is there any documentation that shows how to load library and jar
in Eclipse project.

Can you see any mistakes? What else can I check?

Thanks
Ash

Philippe Simons

unread,
Aug 8, 2011, 8:23:19 AM8/8/11
to andro...@googlegroups.com
System.loadLibrary take as arguments the undecorated name of your library
if your library file is libaudioserver.so, you need to pass "audioserver" to loadLibrary()

Ashish

unread,
Aug 8, 2011, 4:23:08 PM8/8/11
to android-ndk
Hi Philippe,

That is correct. The library that is generated is called
libaudioserver.so

Any other ideas as to what could be going wrong?

Thanks

Philippe Simons

unread,
Aug 8, 2011, 4:36:19 PM8/8/11
to andro...@googlegroups.com
yup... your System.loadLibrary call is wrong

 System.loadLibrary("audioserver.so");

should be

 System.loadLibrary("audioserver");

Ashish

unread,
Aug 8, 2011, 6:30:42 PM8/8/11
to android-ndk
Hi Philippe,
Thanks for the prompt reply. I updated the source line to
System.loadLibrary("audioserver") but no change. Dalvik Debug Monitor
indicates that library could not be loaded.

Can indicate if the following steps are correct when loading the
library in Eclipse project.

After starting Eclipse, I load my test Android application. I have
created a new folder called libs. I right click on the folder libs and
select Import -> General -> File System. I then select my
libaudioserver.so library and click OK.

Is this correct? Note that I do not use Eclipse to generate the
libaudioserver.so.

Thanks for your help.
Ash

Ash

unread,
Aug 9, 2011, 7:24:21 AM8/9/11
to android-ndk
OK I have made some progress. Instead of using System.loadLibrary(), I
ended up copying the library to / directory on the android and used
the function System.load("/libaudioserver.so").

Dalvik Debug Monitor indicates that the library was loaded however,
gives the message that
"No JNI_OnLoad found in /libaudioserver.so"

I found another post in which it was indicated that:
No JNI_OnLoad is perfectly normal, it's only a debug message.
http://groups.google.com/group/android-ndk/browse_thread/thread/186678942625f46a/557bfe20de389d24

So I went ahead and started calling the functions, however, the
application crashes.

Questions:
How to include .jar in Eclipse. If I try to load my .jar, and right
click on it to say that 'include in build path' I get the following
errors:
[2011-08-09 21:13:58 - IPPA] Dx
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Loa/as/
NewTestClass;
[2011-08-09 21:13:58 - IPPA] Dx at
com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:
123)
[2011-08-09 21:13:58 - IPPA] Dx at
com.android.dx.dex.file.DexFile.add(DexFile.java:143)
[2011-08-09 21:13:58 - IPPA] Dx at
com.android.dx.command.dexer.Main.processClass(Main.java:372)
[2011-08-09 21:13:58 - IPPA] Dx at
com.android.dx.command.dexer.Main.processFileBytes(Main.java:346)
[2011-08-09 21:13:58 - IPPA] Dx at
com.android.dx.command.dexer.Main.access$400(Main.java:59)
[2011-08-09 21:13:58 - IPPA] Dx at com.android.dx.command.dexer.Main
$1.processFileBytes(Main.java:294)
[2011-08-09 21:13:58 - IPPA] Dx at
com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:
244)
[2011-08-09 21:13:58 - IPPA] Dx at
com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:
130)
[2011-08-09 21:13:58 - IPPA] Dx at
com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:
108)
[2011-08-09 21:13:58 - IPPA] Dx at
com.android.dx.command.dexer.Main.processOne(Main.java:313)
[2011-08-09 21:13:58 - IPPA] Dx at
com.android.dx.command.dexer.Main.processAllFiles(Main.java:233)
[2011-08-09 21:13:58 - IPPA] Dx at
com.android.dx.command.dexer.Main.run(Main.java:185)
[2011-08-09 21:13:58 - IPPA] Dx at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[2011-08-09 21:13:58 - IPPA] Dx at
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
[2011-08-09 21:13:58 - IPPA] Dx at
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
[2011-08-09 21:13:58 - IPPA] Dx at
java.lang.reflect.Method.invoke(Unknown Source)
[2011-08-09 21:13:58 - IPPA] Dx at
com.android.ide.eclipse.adt.internal.build.DexWrapper.run(DexWrapper.java:
179)
[2011-08-09 21:13:58 - IPPA] Dx at
com.android.ide.eclipse.adt.internal.build.BuildHelper.executeDx(BuildHelper.java:
585)
[2011-08-09 21:13:58 - IPPA] Dx at
com.android.ide.eclipse.adt.internal.build.builders.PostCompilerBuilder.build(PostCompilerBuilder.java:
490)
[2011-08-09 21:13:58 - IPPA] Dx at
org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:
629)
[2011-08-09 21:13:58 - IPPA] Dx at
org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
[2011-08-09 21:13:58 - IPPA] Dx at
org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:
172)
[2011-08-09 21:13:58 - IPPA] Dx at
org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:
203)
[2011-08-09 21:13:58 - IPPA] Dx at
org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:
255)
[2011-08-09 21:13:58 - IPPA] Dx at
org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
[2011-08-09 21:13:58 - IPPA] Dx at
org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:
258)
[2011-08-09 21:13:58 - IPPA] Dx at
org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:
311)
[2011-08-09 21:13:58 - IPPA] Dx at
org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:
343)
[2011-08-09 21:13:58 - IPPA] Dx at
org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:
144)
[2011-08-09 21:13:58 - IPPA] Dx at
org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:
242)
[2011-08-09 21:13:58 - IPPA] Dx at
org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
[2011-08-09 21:13:58 - IPPA] Dx 1 error; aborting
[2011-08-09 21:13:58 - IPPA] Conversion to Dalvik format failed with
error 1

How to deal with this error? How to start using functions from .jar
archive?

Thanks


On Aug 9, 8:30 am, Ashish <anan...@gmail.com> wrote:
> Hi Philippe,
> Thanks for the prompt reply. I updated the source line to
> System.loadLibrary("audioserver") but no change. Dalvik Debug Monitor
> indicates that library could not be loaded.
>
> Can indicate if the following steps are correct when loading the
> library in Eclipse project.
>
> After starting Eclipse, I load my test Android application. I have
> created a new folder called libs. I right click on the folder libs and
> select Import -> General -> File System. I then select my
> libaudioserver.so library and click OK.
>
> Is this correct? Note that I do not use Eclipse to generate the
> libaudioserver.so.
>
> Thanks for your help.
> Ash
>
> On Aug 9, 6:36 am, Philippe Simons <simons.phili...@gmail.com> wrote:
>
>
>
>
>
>
>
> > yup... your System.loadLibrary call is wrong
>
> >  System.loadLibrary("audioserver.so");
>
> > should be
>
> >  System.loadLibrary("audioserver");
>
> > On Mon, Aug 8, 2011 at 10:23 PM,Ashish<anan...@gmail.com> wrote:
> > > Hi Philippe,
>
> > > That is correct. The library that is generated is called
> > > libaudioserver.so
>
> > > Any other ideas as to what could be going wrong?
>
> > > Thanks
>
> > > On Aug 8, 10:23 pm, Philippe Simons <simons.phili...@gmail.com> wrote:
> > > > System.loadLibrary take as arguments the undecorated name of your library
> > > > if your library file is libaudioserver.so, you need to pass "audioserver"
> > > to
> > > > loadLibrary()
>

Ash

unread,
Aug 9, 2011, 7:42:23 AM8/9/11
to andro...@googlegroups.com
This is how I am calling a function from .jar
NewTestClass ntc = new NewTestClass();
int x = ntc.getX();

When I run my android app I get the following in Dalvik Debug Monitor
01-16 21:41:09.664: DEBUG/dalvikvm(20418): Trying to load lib /libaudioserver.so 0x43feb908
01-16 21:41:09.664: DEBUG/dalvikvm(20418): Added shared lib /libaudioserver.so 0x43feb908
01-16 21:41:09.664: DEBUG/dalvikvm(20418): No JNI_OnLoad found in /libaudioserver.so 0x43feb908, skipping init
01-16 21:41:09.671: INFO/LoginActivity(20418): onCreate(): function entered
01-16 21:41:09.703: WARN/dalvikvm(20418): No implementation found for native Loa/com/au/newtestclassmoduleJNI;.new_NewTestClass ()J
01-16 21:41:09.703: DEBUG/AndroidRuntime(20418): Shutting down VM
01-16 21:41:09.703: WARN/dalvikvm(20418): threadid=1: thread exiting with uncaught exception (group=0x4001d7c8)
01-16 21:41:09.703: ERROR/AndroidRuntime(20418): FATAL EXCEPTION: main
01-16 21:41:09.703: ERROR/AndroidRuntime(20418): java.lang.UnsatisfiedLinkError: new_NewTestClass
01-16 21:41:09.703: ERROR/AndroidRuntime(20418):     at oa.com.au.newtestclassmoduleJNI.new_NewTestClass(Native Method)
01-16 21:41:09.703: ERROR/AndroidRuntime(20418):     at oa.com.au.NewTestClass.<init>(NewTestClass.java:37)
01-16 21:41:09.703: ERROR/AndroidRuntime(20418):     at oa.com.au.LoginActivity.onCreate(LoginActivity.java:57)
01-16 21:41:09.703: ERROR/AndroidRuntime(20418):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-16 21:41:09.703: ERROR/AndroidRuntime(20418):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-16 21:41:09.703: ERROR/AndroidRuntime(20418):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-16 21:41:09.703: ERROR/AndroidRuntime(20418):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-16 21:41:09.703: ERROR/AndroidRuntime(20418):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-16 21:41:09.703: ERROR/AndroidRuntime(20418):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 21:41:09.703: ERROR/AndroidRuntime(20418):     at android.os.Looper.loop(Looper.java:123)
01-16 21:41:09.703: ERROR/AndroidRuntime(20418):     at android.app.ActivityThread.main(ActivityThread.java:4627)
01-16 21:41:09.703: ERROR/AndroidRuntime(20418):     at java.lang.reflect.Method.invokeNative(Native Method)
01-16 21:41:09.703: ERROR/AndroidRuntime(20418):     at java.lang.reflect.Method.invoke(Method.java:521)
01-16 21:41:09.703: ERROR/AndroidRuntime(20418):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-16 21:41:09.703: ERROR/AndroidRuntime(20418):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-16 21:41:09.703: ERROR/AndroidRuntime(20418):     at dalvik.system.NativeStart.main(Native Method)


David Turner

unread,
Aug 9, 2011, 9:37:44 AM8/9/11
to andro...@googlegroups.com
What is the name of your Java native method, and what is the name of the corresponding extern "C" native one.
If your method name contains an underscore, you will have to replace with '_1' in the native name.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/android-ndk/-/6VqVU-2aIfEJ.

Ash

unread,
Aug 9, 2011, 11:01:10 PM8/9/11
to andro...@googlegroups.com
I made some progress last night. I managed to compile and build a libaudioserver.so of my small test application (newtestclass.cpp) and generate .jar.
I then copied the library.so to /system/lib directory of my android device.
I opened up Eclipse and imported my .jar archive and using System.load("/system/lib/libaudioserver.so") was successfully able to load the library and call the functions in the newtestclass.cpp.

The above gave me more confidence and thus I decided to take the deep plunge and started compiling our huge C\C++ code base. I managed to compile everything using android ndk and was able to generate libaudioserver.so and .jar archive.
Unfortunately when I execute my android app, I get an error when trying to load the libaudioserver.so. The good news is that the libaudioserver.so is found however, while loading it fails. The error message I get form Dalvik Debug Monitor is:

01-17 12:35:28.523: INFO/dalvikvm(20568): Unable to dlopen(/system/lib/libaudioserver.so): Cannot load library: link_image[1995]: failed to link libaudioserver.so
01-17 12:35:28.523: ERROR/LoginActivity(20568): WARNING: Could not load libaudioserver.so

My makefile is using the following libraries - -loa -lz -lpthread -ldl
I have access to oa and z libs. How do I link pthread and dl?
Do I need to statically link these to my libaudioserver.so?

Please advice.

Thanks

Ash

unread,
Aug 11, 2011, 2:14:08 AM8/11/11
to andro...@googlegroups.com
Hi All,

I have made more progress. I have managed to get our huge C/C++ to fully compile with Android NDK into a libaudioserver.so library and was successfully able to load it and use it in our Android device. The only thing remaining is that the library I have built uses 2 other libraries mainly liboa (which is internal to us) and libz.

I found that I had to upload these to /system/lib in order for Eclipse to load the app. Without these libraries Eclipse will fail to load the app. Final goal for me would be to statically link these 2 libraries into libaudioserver.so.

If anyone has any advice it will be great.

Thanks
Ash

charles

unread,
Aug 12, 2011, 11:04:27 AM8/12/11
to andro...@googlegroups.com
Hi,

How do I link pthread and dl?
`-lpthread` and `-ldl`, respectively.


I found that I had to upload these to /system/lib in order for Eclipse to load the app.
 Are you sure they have to pushed to /system/lib? Have you tried storing them in the application sandbox (e.g., `/data/data/your.app.namespace/libyourlib.so`)?

Best,
- Charles

 

Ash

unread,
Aug 14, 2011, 11:33:50 PM8/14/11
to andro...@googlegroups.com
Hi Charles,

If I copy my libaudioserver.so to /data/data/your.app.namespace/lib OR /system/lib then I can use System.loadLibrary("audioserver").

However, I still need to have liboa.so and libz.so in system/lib folder for Eclipse to be able to install the apk. 

Why is this considering when I do arm-linux-androideabi-objdump -p libaudioserver.so the only NEEDED parts are libm.so, libc.so, and libdl.so

Thanks


Reply all
Reply to author
Forward
0 new messages