1

I'm using ndk-build to build a set of shared library(.so) for my android project. I configured and made the source code of C++ library(gdal-2.2.2). everything was ok.("./configure & make & make install" was successful).

So i created my jni folder like this documentation.

but when I'm trying to use ndk-build on windows, I get a lot of error like "Undefined refrence to somthing".

I've spent a lot of time on this project. Is there someone to help me? Thanks.

Update

I used configure like this on ubuntu 16.04:

./configure --prefix=/home/mahdi/Desktop/build/ --with-spatialite=yes --with-spatialite-soname=libspatialite.so --host=i686-linux-android  CXXFLAGS="-D_GLIBCXX_USE_CXX11_ABI=0" LIBS="-lsupc++ -lstdc++"

After make & make install step I created JNI. this is my directory.

jniwrap
jni
  gdal
  Android.mk
  Application.mk
  gdal_wrap.cpp
  gdalconst_wrap.c  
  gnm_wrap.cpp  
  libgdal.a 
  ogr_wrap.cpp
  osr_wrap.cpp

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := gdal
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/gdal/include
LOCAL_SRC_FILES := libgdal.a
LOCAL_EXPORT_LDLIBS := -lz
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := gdaljni
LOCAL_SRC_FILES := gdal_wrap.cpp
LOCAL_STATIC_LIBRARIES := gdal
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := gdalconstjni
LOCAL_SRC_FILES := gdalconst_wrap.c
LOCAL_STATIC_LIBRARIES := gdal
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := ogrjni
LOCAL_SRC_FILES := ogr_wrap.cpp
LOCAL_STATIC_LIBRARIES := gdal
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := osrjni
LOCAL_SRC_FILES := osr_wrap.cpp
LOCAL_STATIC_LIBRARIES := gdal
include $(BUILD_SHARED_LIBRARY)

Aplication.mk

APP_STL := gnustl_shared
APP_CFLAGS := Android.mk
APP_ABI := x86
APP_PLATFORM := android-14

Then I used android-ndk-r16b in windows-x86_64 but I faced with these errors like this picture:

undefined reference ...

There was a lot of "undefined reference error" that i can't show here.

Note: for making gdal Java Binding I used swig and jdk7 on my ubuntu 16.04.

Mahdi Nazari Ashani
  • 372
  • 1
  • 5
  • 22
  • Please provide an minimal example along with the resulting error messages. – Michael May 03 '18 at 13:50
  • Note that this project used NDK r7. Most likely, these instructions are not relevant anymore with modern NDK (r16). Sometimes, installing an older NDK (e.g. r10 from https://developer.android.com/ndk/downloads/older_releases) can help. Otherwise, please provide more information: which undefined references, which steps completed successfully, etc. Disclose all the parameters for `./configure` – it's very easy to make a mistake here. – Alex Cohn May 03 '18 at 14:25
  • @AlexCohn Please read the given update. – Mahdi Nazari Ashani May 03 '18 at 22:03
  • See if this is your problem https://stackoverflow.com/questions/4585591/no-rule-to-make-target-ndk/4650113#4650113 – Marcos Vasconcelos May 03 '18 at 22:09
  • a fun fact about your setup is that the gdal fork that explains how to build the library for Android (https://github.com/nutiteq/gdal) does not contain the `ogrsqliteapiroutines.c` file that caused the undefined references to `sqlite3_bind_…` functions. Could it happen that you got different versions mixed together? Why don't you run the ndk step on the same ubuntu machine? Moving to Windows is yet another point of trouble in your setup. It is safer to copy the resulting JNI libraries to Windows if you prefer to work with Android Studio on that platform. – Alex Cohn May 04 '18 at 05:35
  • @AlexCohn I wanted to do that on ubuntu same machine but i got another error on ubuntu so i decided to come to windows. – Mahdi Nazari Ashani May 04 '18 at 08:02
  • @AlexCohn I used [this repo](https://github.com/houlian0/GdalAndroid/tree/master/ndk-build-gdal2.1.4) on windows and it worked well but my project fails. – Mahdi Nazari Ashani May 04 '18 at 08:09
  • But this is 2.1.4! That's why it doesn't match your 2.2.2 – Alex Cohn May 04 '18 at 10:01
  • @AlexCohn yes. I should build 2.1.4 for myself and compare that with abow repo. But i think i'm not doing configuration truely. Or I think i need a dependancy. Is it possible to have chat with u. Thank u for ur help. – Mahdi Nazari Ashani May 04 '18 at 12:57

1 Answers1

1

When you build libgdal.a on your ubuntu machine, you must have sqlite3, which resolves #include "sqlite3.h".

These include files are enough for a static library, but to create libgdaljni.so you also need libsqlite3.a. You can cross-compile it for Android it yourself on the same ubuntu machine, but it is probably OK to get prebuilt library e.g. from https://github.com/couchbase/couchbase-lite-java-native/tree/master/vendor/sqlite/libs/android.

Copy this file (for appropriate ABI) to the same directory, and modify your Android.mk accordingly:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := sqlite3
LOCAL_SRC_FILES := libsqlite3.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := gdal
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/gdal/include
LOCAL_SRC_FILES := libgdal.a
LOCAL_EXPORT_LDLIBS := -lz
LOCAL_STATIC_LIBRARIES := sqlite3
include $(PREBUILT_STATIC_LIBRARY)

*continued without changes*

If you still have "undefined reference error", this could mean that some other libraries should be added.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • thank u so much. It worked fine and sqlite3 errors went. but there are another reference errors that i cant find how I should resolve them. Undefined reference to std::__CXX11::basic_string. Do u know how i should solve it? – Mahdi Nazari Ashani May 08 '18 at 17:18
  • There is a problem other "Undefined refrence errors" are very much. Should I solve them like something u have said? – Mahdi Nazari Ashani May 08 '18 at 17:51
  • Not all undefined reference errors can be solved the same way. E.g. this std::__CXX11::basic_string - which file does it come from? – Alex Cohn May 08 '18 at 21:04
  • /usr/include/c++/5/bits/basic_string.h:589 errorL undefined reference to 'std::__cxx11::__cxx11::basic_string,std::allocator>::swap... – Mahdi Nazari Ashani May 08 '18 at 21:33
  • This comes from some module that was not built for Android. Does the error message not mention some .a or .o file ? – Alex Cohn May 09 '18 at 04:10
  • I don't understand what u mean. I'have also for example this error "undefined reference to 'json_object_new_array' " this image shows my errors https://imgur.com/a/wRSXOqK – Mahdi Nazari Ashani May 24 '18 at 08:18
  • Your screenshot shows that `gdalwarpoperation.cpp` is responsible for, at least, part of these errors. And also, that it was not compiled with Android (NDK) toolchain. We can understand the situation a bit better if you run `ndk-build V=1`, it will show full build commands as they are executed. – Alex Cohn May 24 '18 at 13:14