메시지 5개 - 모두 접기 |
Hi all,
I have been experimenting with using native code on the Android Let's start with my successes so far. I have tested the sample My goal however is to access the functionality of an (existing) C++ To the best of my knowledge, the most straightforward way to do so On Android however, I have up till now not been able to achieve the Here is my source code. First there is the "native" C++ class, which newtestclass.h #ifndef NEWTESTCLASS_H class NewTestClass void setX(const int 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; }
As you can see this is pretty straightforward C++ code, to make sure the problems are not being caused by advanced C++ constructs. As instructed in the SWIG tutorial, I have written a SWIG interface newtestclass.i %module newtestclassmodule %include "newtestclass.h" Again very simple. On this interface class, I invoke the swig tool as swig -c++ -o newtestclass_wrap.cpp -package com.example.newtest -java which generates the file newtestclass_wrap.cpp. If desired/needed, I Besides the newtestclass_wrap.cpp file, the swig tool also generates a Finally, I attempt to use the Java NewTestClass inside the onCreate public class RunMe extends Activity { System.loadLibrary("newtest"); NewTestClass ntc = new NewTestClass(); TextView tv = new TextView(this); }
I compile and run the Android activity class using the Eclipse ADT plug-in. When executing the Android application, LogCat output suggests the libnewtest.so library is loaded successfully. However I do get some warnings: D/dalvikvm( 2027): Trying to load lib /data/data/com.example.newtest/ As soon as the application tries to instantiate a NewTestClass object I/DEBUG ( 539): *** *** *** *** *** *** *** *** *** *** *** *** *** I hope my explanation has been sufficiently elaborate. Has anyone any Many thanks in advance, |
As I did not find any solution to this strange problem, I tried compiling the native library using the agcc build script (http:// plausible.org/andy/agcc). Surprisingly, now everything works as expected! In other words, I am able to use my native code in Java as I would in a C++ environment. Unfortunately however, I still have no clue as to why compilation |
I have used the SWIG framework on Android and it works. Regards, On Mon, Jul 6, 2009 at 8:10 PM, Maarten Wijnants <maarten.wijna...@gmail.com |
Hello Sridhar, Many thanks for your reply! I am sorry I did not respond sooner, I On 31 jul, 16:32, Sridhar M <sridhar....@gmail.com> wrote: > Maarten, Indeed I did not implement the JNI_OnLoad method. I saw LogCat > I have used the SWIG framework on Android and it works. printing a warning about this, but adding the method did not help me. Maybe I did something wrong in the JNI_OnLoad implementation, I'm not sure. Anyway, using the agcc build script, I was able to successfully get SWIG to work, WITHOUT the JNI_OnLoad method. So I am still not really sure whether native JNI method registering is absolutely necessary. > > Regards,
Once more my gratitude for your reply! Maybe in the future I will > Sridhar again look into NDK native library building (instead of using the agcc build script). When so, I will definitely look into your suggestion of registering native methods in the JNI_OnLoad method. Greetz, |
You are welcome! Hope it helps. Regards, On Wed, Aug 26, 2009 at 5:31 PM, Maarten Wijnants < |