user984846
user984846

Reputation: 283

kivy: python for android + C++

I've used python-for_android to create a kivy based application running on android.

Some parts of my application have been optimized in c++ using cython.

I manage to compile all my code using python for android and a custom recipes.

My code also works perfectly with kivy under linux.

But on my android device, it failed to load some c++ function. For instance, I get the message :

ImportError: Cannot load library: reloc_library[1307]:  1839 cannot locate '_ZNSt9basic_iosIcSt11char_traitsIcEE4initEPSt15basic_streambufIcS1_E'...

Any idea ?

Thanks

Upvotes: 7

Views: 2966

Answers (2)

Thorsten B'eggs
Thorsten B'eggs

Reputation: 31

I am currently trying to build pybox2d (with swig) via python-for-android.

The build seems to be fine unit i try to import Box2D ( from the app on the actual android device) i get a "cannot locate symbol __cxa_end_cleanup".

Unfortunately the above fix doies not help. Any other ideas?

Update: I could fix all issues. I had to link against stlport_shared.

All my work is in my fork of https://github.com/DerThorsten/python-for-android/ . It works with newer ndks then the orginal python-for-android. And it has Box2D.

Upvotes: 1

user984846
user984846

Reputation: 283

I've finally managed to make my code work using C++ under android.

There were two difficulties :

1 - Access to c++ header from the arm environment created by push_arm. I had to add the correct includes in my recipe, and modify the default CXX var :

    #dirty hack
    export C_INCLUDE="-I$ANDROIDNDK/sources/cxx-stl/gnu-libstdc++/$TOOLCHAIN_VERSION/include/ -I$ANDROIDNDK/sources/cxx-stl/gnu-libstdc++/$TOOLCHAIN_VERSION/libs/armeabi/include/"
    export OLD_BOUBOU=$CC
    export CC="$CXX $C_INCLUDE"

    try $BUILD_PATH/python-install/bin/python.host setup.py install -O2
    #try cp libgnustl_shared.so $LIBS_PATH/
    try cp $ANDROIDNDK/sources/cxx-stl/gnu-libstdc++/4.4.3/libs/armeabi/libgnustl_shared.so $LIBS_PATH/

    export CC=$OLD_BOUBOU

2 - Find the shared library containing the libstl functions, and load it. This was the harder part :

After some research, I discover that stl functions are stored in libgnustl_shared.so, and not listdc++.so. So you have to embed this library in your apk.

This is the purpose of the line try cp $ANDROIDNDK/sources/cxx-stl/gnu-libstdc++/4.4.3/libs/armeabi/libgnustl_shared.so $LIBS_PATH/

Then, you have to load it. I've modified :

src/src/org/renpy/android/PythonActivity.java
src/src/org/renpy/android/PythonService.java
by adding this line after the others System.loadLibrary() :
System.loadLibrary("gnustl_shared");

Upvotes: 7

Related Questions