Changes between Version 1 and Version 2 of BuildingForAndroid


Ignore:
Timestamp:
Feb 8, 2011, 3:45:43 PM (13 years ago)
Author:
rolker
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • BuildingForAndroid

    v1 v2  
    1 == Building GDAL using Android NDK on Linux ==
     1= Building GDAL using Android NDK on Linux =
    22
    33''' Warning! this is still a work in progress! '''
    44
    55
    6 This procedure was developed on a Linux system using Android NDK r5b.
     6This procedure was developed on a Linux system using Android NDK r5b and is based on
     7information from this thread: http://groups.google.com/group/android-ndk/browse_thread/thread/9988752f332eff45
     8
     9== Cross compiling GDAL ==
    710
    811First, a standalone toolchain is created to make the configure script easier to use. (See android-ndk-r5b/docs/STANDALONE-TOOLCHAIN.html for more details)
     
    1821}}}
    1922
    20 From the GDAL source directory, configure for cross compiling and make.
     23From the GDAL source directory, configure for cross compiling and make. (This assumes config.guess and config.sub
     24where updated. Latest versions can be found here: http://git.savannah.gnu.org/cgit/config.git/tree/)
    2125
    2226{{{
    23 gdal$ CC="arm-linux-androideabi-gcc" CXX="arm-linux-androideabi-g++" \
    24 CFLAGS="-mthumb" CXXFLAGS="-mthumb" LIBS="-lsupc++ -lstdc++"  ./configure --host=arm-linux-eabi --without-grib
     27gdal$ CFLAGS="-mthumb" CXXFLAGS="-mthumb" LIBS="-lsupc++ -lstdc++" \
     28      ./configure --host=arm-linux-androideabi --without-grib --prefix=$PROJECT/external/gdal
    2529gdal$ make
     30gdal$ make install
     31}}}
     32
     33The $PROJECT variable contains the path to where you want your Android NDK modules to live.
     34
     35In $PROJECT/external/gdal, an Android.mk file is created  with the following contents:
     36
     37{{{
     38LOCAL_PATH := $(call my-dir)
     39include $(CLEAR_VARS)
     40LOCAL_MODULE := gdal
     41LOCAL_SRC_FILES := lib/libgdal.a
     42LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
     43LOCAL_EXPORT_LDLIBS := -lz
     44include $(PREBUILT_STATIC_LIBRARY)
     45}}}
     46
     47== Using in a JNI library ==
     48
     49Here's a !HelloGdal app to test the use of the library as part of a JNI component
     50of an Android project.
     51
     52In the project's jni directory, hello-jdal.cpp was create with the following contents:
     53
     54{{{
     55#include <jni.h>
     56#include <ogr_api.h>
     57#include <sstream>
     58
     59extern "C" {
     60    JNIEXPORT jstring
     61    JNICALL Java_org_gdal_HelloGdal_stringFromGDAL(JNIEnv*env, jobject thiz);
     62}
     63
     64JNIEXPORT jstring
     65JNICALL Java_org_gdal_HelloGdal_stringFromGDAL(JNIEnv* env, jobject thiz)
     66{
     67    OGRRegisterAll();
     68    std::ostringstream drivers;
     69    drivers << "OGR Drivers:\n";
     70    for (int i = 0; i < OGRGetDriverCount(); ++i)
     71        drivers << "\t" << OGR_Dr_GetName(OGRGetDriver(i)) << "\n";
     72
     73    return env->NewStringUTF(drivers.str().c_str());
     74}
     75}}}
     76
     77(Yes, I know, it's called !HelloGdal, yet it lists the OGR drivers. I'll leave it as
     78an exercise to have the GDAL drivers listed)
     79
     80Here's the corresponding Android.mk file:
     81
     82{{{
     83LOCAL_PATH := $(call my-dir)
     84include $(CLEAR_VARS)
     85LOCAL_MODULE := hello-gdal
     86LOCAL_SRC_FILES := hello-gdal.cpp
     87LOCAL_STATIC_LIBRARIES := gdal
     88include $(BUILD_SHARED_LIBRARY)
     89$(call import-module,gdal)
     90}}}
     91
     92The path to the external GDAL module is set and the library is built using the NDK's build process.
     93
     94{{{
     95jni$ export NDK_MODULE_PATH=$PROJECT/external
     96jni$ $NDK_ROOT/ndk-build
     97}}}
     98
     99Finally, the Java portion that uses the JNI library.
     100
     101{{{
     102package org.gdal;
     103
     104import android.app.Activity;
     105import android.os.Bundle;
     106import android.widget.TextView;
     107
     108
     109public class HelloGdal extends Activity {
     110       
     111        public void onCreate(Bundle savedInstanceState) {
     112        super.onCreate(savedInstanceState);
     113        TextView  tv = new TextView(this);
     114        tv.setText( stringFromGDAL() );
     115        setContentView(tv);
     116    }
     117       
     118        public native String stringFromGDAL();
     119       
     120        static {
     121        System.loadLibrary("hello-gdal");
     122    }
     123       
     124}
    26125}}}
    27126
    28127== Next steps ==
    29128
    30  * check if resulting libs are usable.
    31129 * build and adapt the Java swig bindings to expose GDAL to the Java portion of an Android application.