GDAL on Android

Building GDAL on Android is one of the hardest tasks I worked on, this is why I find that it would be useful to share it with you 🙂

Environment: Windows 7

Tools: Mingw, Eclipse.

1- Update config.guess and config.sub in gdal directory: http://git.savannah.gnu.org/cgit/config.git/tree/config.guess

http://git.savannah.gnu.org/cgit/config.git/tree/config.sub

Or you can contact me to get the config.guess and config.sub

2- Open Mingw/msys/msys.bat

$ANDROID_NDK/build/tools/make-standalone-toolchain.sh --platform=android-8 --install-dir=android-8-toolchain

($ANDROID_NDK is the environement variable which contains the path to ndk directory)

3-

 export PATH=$PATH:~/android-8-toolchain/bin/

4- Go to GDAL directory

CFLAGS="-mthumb" CXXFLAGS="-mthumb" LIBS="-lsupc++ -lstdc++" \
      ./configure --host=arm-linux-androideabi --without-libtool --prefix=$PROJECT/external/gdal

5- Edit GNUmakefile (in GDAL folder)

-GDAL_OBJ	=	$(GDAL_ROOT)/frmts/o/*.o \
-			$(GDAL_ROOT)/gcore/*.o \
-			$(GDAL_ROOT)/port/*.o \
-			$(GDAL_ROOT)/alg/*.o
+GDAL_OBJ	=	./frmts/o/*.o \
+			./gcore/*.o \
+			./port/*.o \
+			./alg/*.o

 ifeq ($(OGR_ENABLED),yes)
-GDAL_OBJ += $(GDAL_ROOT)/ogr/ogrsf_frmts/o/*.o
+GDAL_OBJ += ./ogr/ogrsf_frmts/o/*.o
 endif

6-

make

7-

make install

•Create a new environement variable : NDK_MODULE_PATH and set it to the path of the gdal directory add •%NDK_MODULE_PATH%; to PATH •From jni folder

export NDK_MODULE_PATH=/c/android

Testing GDAL on Android app 

1- Create Android.mk on gdal directory (the built one):

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

2- Create android project

HelloGdal.java

package com.gdal;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloGdal extends Activity {

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView  tv = new TextView(this);
        tv.setText( stringFromGDAL() );
        setContentView(tv);
    }	
	public native String stringFromGDAL();
	 	static {
        System.loadLibrary("hello-gdal");
    }
    	}

3-

Add native library: hello-gdal •Hello-gdal.cpp

#include <jni.h>
#include <ogr_api.h>
#include <sstream>

extern "C" {
    JNIEXPORT jstring JNICALL Java_org_gdal_HelloGdal_stringFromGDAL(JNIEnv*env, jobject thiz);
}

JNIEXPORT jstring JNICALL Java_org_gdal_HelloGdal_stringFromGDAL(JNIEnv* env, jobject thiz)
{
    OGRRegisterAll();
    std::ostringstream drivers;
    drivers << "OGR Drivers:\n";
    for (int i = 0; i < OGRGetDriverCount(); ++i)
        drivers << "\t" << OGR_Dr_GetName(OGRGetDriver(i)) << "\n";

    return env->NewStringUTF(drivers.str().c_str());
}

•Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello-gdal
LOCAL_SRC_FILES := hello-gdal.cpp
LOCAL_STATIC_LIBRARIES := gdal
include $(BUILD_SHARED_LIBRARY)
$(call import-module,gdal)

•Application.mk

APP_STL := gnustl_static

4 thoughts on “GDAL on Android

  1. Ams says:

    hey there,

    I am not sure when you posted this article but I am having trouble following your procedure.
    could you please clarify a few points if I may ask?
    I am using the latest versions of java, android SDK and NDK
    if so please let me know you would be of a great help.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s