编译 GDAL For Android

需要编译的项目:GDAL2.4.4 + PROJ5.2.0 + SQLite3.22.0

一、安装NDK

从官网下载NDK https://developer.android.google.cn/ndk/downloads

解压并配置环境变量,我的路径是 /usr/local/android-ndk,可以写到 /etc/profile 里,更改配置重新 source /etc/profile 就行了。

  export NDK_HOME=/usr/local/android-ndk/android-ndk-r21

创建独立工具链

!/bin/bash
 platform=android-19
 shmake=$NDK_HOME/build/tools/make-standalone-toolchain.sh
 archs=(
     'arm'
     'x86'
 )
 toolchains=(
     'arm-linux-androideabi-4.9'
     'x86-4.9'
 )
 echo $NDK_HOME
 num=${#archs[@]}
 for ((i=0;i<$num;i++))
 do
    sh $shmake --arch=${archs[i]} --platform=$platform --install-dir=/home/sean/Android/android-toolchain/${archs[i]} --toolchain=${toolchains[i]}
 done

配置工具链环境变量(更换编译架构需要重启一下重新设置变量)

export TOOLCHAIN_HOME=/home/sean/Android/android-toolchain/arm
export NDK_HOME=/usr/local/android-ndk/android-ndk-r21
export PATH=$PATH:$TOOLCHAIN_HOME/bin

二、编译GDAL

从GDAL官网下载源码:https://gdal.org/download.html

为了精简到核心功能,这里我们用到的是2.4.4版本(如果使用3.x版本,需要先编译安装对应架构的 PROJ.6,然后加上编译参数 --with-proj=" 你的PROJ6路径 "))下面是编译 arm v7 架构的配置,移除了不必要的功能。配置输出的结尾应该能看到

Statically link PROJ:      no

这样我们的 GDLAL2.4.4 编译完成后就是动态链接 PROJ 的了。

CC="arm-linux-androideabi-gcc" CXX="arm-linux-androideabi-g++" LDFLAGS="-march=armv7-a -Wl,--fix-cortex-a8" CFLAGS="-mthumb" CXXFLAGS="-mthumb" LIBS="-lstdc++" ./configure --prefix=/home/sean/gdal2/Android/arm --host=arm-linux-androideabi --with-unix-stdio-64=no --with-ogr --with-geos --with-hide-internal-symbols --with-libz=internal --with-threads --without-bsb --without-cfitsio --without-cryptopp --without-curl --without-dwgdirect --without-ecw --without-expat --without-fme --without-freexl --without-gif --without-gif --without-gnm --without-grass --without-grib --without-hdf4 --without-hdf5 --without-idb --without-ingres --without-jasper --without-jp2mrsid --without-jpeg --without-kakadu --without-libgrass --without-libkml --without-libtool --without-mrf --without-mrsid --without-mysql --without-netcdf --without-odbc --without-ogdi --without-openjpeg --without-pcidsk --without-pcraster --without-pcre --without-perl --without-pg --without-php --without-png --without-python --without-qhull --without-sde --without-sqlite3 --without-webp --without-xerces --without-xml2

然后 make 编译和 make install 安装到对应目录,这样我们就完成了GDAL的编译工作。

三、SQLite

官网下载源码: https://www.sqlite.org/download.html

因为PROJ依赖于SQLite3,因此同样编译对应架构的 SQLite3,安装到指定目录。

CC="arm-linux-androideabi-gcc" CXX="arm-linux-androideabi-g++" LDFLAGS="-march=armv7-a -Wl,--fix-cortex-a8" CFLAGS="-mthumb" CXXFLAGS="-mthumb"./configure --prefix=/home/sean/sqlite3/Android/arm --host=arm-linux-androideabi 

编译完成后还要将安装目录下 lib 文件夹下面的库文件合并到工具链对应 {ANDROID_ABI}/lib 目录下,这么做是为了解决PROJ编译时报错 ld 找不到 -lsqlite3(后面已经手动指定目录,不知道该错误产生的原因,如果有了解的请留言)

我的路径为: /home/sean/Android/android-toolchain/arm/arm-linux-androideabi/lib

四、PROJ

因为GDAL的某些功能依赖于PROJ,所以我们需要编译对应的PROJ动态库进行链接。因为GDAL更新日志中提到了对PROJ.5的支持,但没提到过PROJ.6及更高版本,这里就用PROJ.5。

官网PROJ下载: https://proj.org/download.html

由于工具链的 ld 不包含我们的 sqlite3 目录,这里就手动指定 include 和 lib。

LOCAL_LDFLAGS="-L/home/sean/sqlite3/Android/arm/lib/libsqlite3.a" CC="arm-linux-androideabi-gcc -I/home/sean/sqlite3/Android/arm/include" CXX="arm-linux-androideabi-g++ -I/home/sean/sqlite3/Android/arm/include" LDFLAGS="-march=armv7-a -Wl,--fix-cortex-a8" CFLAGS="-mthumb" CXXFLAGS="-mthumb" LIBS="-lstdc++  -L/home/sean/sqlite3/Android/arm/lib/libsqlite3.a"  ./configure --prefix=/home/sean/proj5/Android/arm  --host=arm-linux-androideabi 

至此,我们有了 libgdal.a 、libproj.so 两个库以及相关头文件。我们只要在 Android 项目中集成就行了。CMake相关部分配置(注意 .so 动态库必须放在 jniLib 下,静态库这里是放在了自定义的 jni 目录,头文件放在了自定义的 inc 目录):

add_library(proj SHARED IMPORTED)
set_target_properties(proj
        PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libproj.so)

add_library(gdal STATIC IMPORTED)
set_target_properties(gdal
        PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../jni/lib/${ANDROID_ABI}/libgdal.a)

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc/${ANDROID_ABI}/)

target_link_libraries(YOUR_LIB
        android
        proj
        gdal
        log)

五、GEOS

安装对应架构GEOS后在GDAL配置中添加

--with-geos=/home/sean/geos/Android/arm/bin/geos-config

使用时链接GEOS的动态库即可。

参考资料

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注