| | 30 | gdal$ make install |
| | 31 | }}} |
| | 32 | |
| | 33 | The $PROJECT variable contains the path to where you want your Android NDK modules to live. |
| | 34 | |
| | 35 | In $PROJECT/external/gdal, an Android.mk file is created with the following contents: |
| | 36 | |
| | 37 | {{{ |
| | 38 | LOCAL_PATH := $(call my-dir) |
| | 39 | include $(CLEAR_VARS) |
| | 40 | LOCAL_MODULE := gdal |
| | 41 | LOCAL_SRC_FILES := lib/libgdal.a |
| | 42 | LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include |
| | 43 | LOCAL_EXPORT_LDLIBS := -lz |
| | 44 | include $(PREBUILT_STATIC_LIBRARY) |
| | 45 | }}} |
| | 46 | |
| | 47 | == Using in a JNI library == |
| | 48 | |
| | 49 | Here's a !HelloGdal app to test the use of the library as part of a JNI component |
| | 50 | of an Android project. |
| | 51 | |
| | 52 | In 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 | |
| | 59 | extern "C" { |
| | 60 | JNIEXPORT jstring |
| | 61 | JNICALL Java_org_gdal_HelloGdal_stringFromGDAL(JNIEnv*env, jobject thiz); |
| | 62 | } |
| | 63 | |
| | 64 | JNIEXPORT jstring |
| | 65 | JNICALL 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 |
| | 78 | an exercise to have the GDAL drivers listed) |
| | 79 | |
| | 80 | Here's the corresponding Android.mk file: |
| | 81 | |
| | 82 | {{{ |
| | 83 | LOCAL_PATH := $(call my-dir) |
| | 84 | include $(CLEAR_VARS) |
| | 85 | LOCAL_MODULE := hello-gdal |
| | 86 | LOCAL_SRC_FILES := hello-gdal.cpp |
| | 87 | LOCAL_STATIC_LIBRARIES := gdal |
| | 88 | include $(BUILD_SHARED_LIBRARY) |
| | 89 | $(call import-module,gdal) |
| | 90 | }}} |
| | 91 | |
| | 92 | The path to the external GDAL module is set and the library is built using the NDK's build process. |
| | 93 | |
| | 94 | {{{ |
| | 95 | jni$ export NDK_MODULE_PATH=$PROJECT/external |
| | 96 | jni$ $NDK_ROOT/ndk-build |
| | 97 | }}} |
| | 98 | |
| | 99 | Finally, the Java portion that uses the JNI library. |
| | 100 | |
| | 101 | {{{ |
| | 102 | package org.gdal; |
| | 103 | |
| | 104 | import android.app.Activity; |
| | 105 | import android.os.Bundle; |
| | 106 | import android.widget.TextView; |
| | 107 | |
| | 108 | |
| | 109 | public 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 | } |