安卓使用記錄筆記(4)Android studio 使用PCL庫
個人查資料,看到的關於PCL在安卓端使用,是需要重新編譯的,,,,,
編譯方式詳見:https://github.com/bashbug/pcl-for-android
pcl-for-android
Bash scripts for cross compiling PCL ( https://github.com/PointCloudLibrary/pcl ) and its dependencies for Android.Scripts were tested for
- Fedora 23, 24 (這個是幹嘛的。。我到現在也沒搞清)
- Ubuntu 16.04
- Android NDK 10e, 11c, 12b(原連結中是這麼寫的,我在編譯的時候使用的是14b)
Install Requirements
Ubuntu
$ sudo apt-get install git make cmake
Fedora
$ sudo dnf install git make cmake
Cross-compilation
$ git clone https://github.com/bashbug/pcl-for-android.git $ export ANDROID_NDK=PATH_TO_YOUR_LOCAL_ANDROID_NDK_FOLDER $ ./download-setup.sh $ ./pcl-build-for-android.sh
The compilations within the pcl-build-for-android.sh needs at least 4GB RAM + 4 GB Swap or 8GB RAM.
Default uses for make -j the number of CPUs.
The number of CPUs and thereby the memory usage can be reduced by setting the option as follows.
$ jobs=NUM_OF_CPUS ./pcl-build-for-android.sh
download-setup.sh
This script downloads the dependencies:
- EIGEN 3.2.9
- FLANN 1.8.4
- BOOST 1.61.0
- PCL 1.8
pcl-build-for-android.sh
This script cross compiles FLANN, BOOST and PCL for android. Add the following folders to your Android.mk file:
- eigen
- flann-android
- boost-android
- pcl-android
//編譯大概就是這樣,後面怎麼在Android Studio 中gradle和Cmakelists中寫依賴的話,容我在琢磨琢磨,後續再更新
生成檔案匯入到Android Studio工程中的步驟如下
一. gradle修改
經過檢視程式碼和測試,上面的方法交叉編譯對應的是armeabi-v7a和armeabi,如果有別的需求或者程式碼能力可以的話,可以自己修改android.toolschain.cmake檔案,改成別的也行,我這裡沒太大要求,所以就用armeabi-v7a。
所以在gradle中需要指定對應的環境,不要全部都包含了。由於大多數安卓機都支援armeabi-v7a,所以我的方式為:
defaultConfig{
ndk{
abiFilters 'armeabi-v7a'//, 'x86', 'arm64-v8a', 'x86_64' 最好指定特定的一個平臺
}
externalNativeBuild{
cmake{
arguments '-DANDROID_TOOLCHAIN=gcc',//clang沒有試過,不知道可以不可以,這裡gcc是可行的
'-DANDROID_STL=gnustl_static'
}
}
}
如果不改變的話,容易出現 “incompatible target”的error.,個人認為這個錯誤大多是編譯安卓版本時對應的平臺不一致導致。
其次,最好在src/main資料夾下新建lib/armeabi-v7a資料夾,然後把.so和.a檔案都copy過來,這樣的原因:
1. 算是一種程式碼規範吧。。其實要說規範的話,標頭檔案也最好放在include資料夾下。。。
2. 似乎這樣子操作的話,是告訴安卓,我用的就是armeabi-v7a,你別給我瞎比編譯成別的環境了。。。嗯好像是這樣,有興趣的同學可以查查,然後告訴我
二. 然後是CMakeLists中的修改。
1). 新增標頭檔案。
include_directories( src/main/cpp/include/pclcpp)//路徑按照自己的來
2). 指定連結庫的位置
add_library(pclcpp STATIC IMPORTED)
set_target_properties(pclcpp
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libpclcpp.a)
3). 連線到主工程中
target_link_libraries(native-lib
pclcpp
${log-lib})
如果在build時報錯error: xxx.so,needed by xxxx.so,missing and no known rule to make it
這個直白點就是說,你的xxx.so沒找到。八成是路徑寫錯了
嗯目前就遇到了這些坑,如果還有別的再繼續寫了~歡迎留言討論。
轉載請註明原處,謝謝
自用的小坑解決方案備忘錄(針對我自己的工程):
1.在include的時候,標頭檔案可以不放在android studio,在cmakelists中寫好路徑就可以了
2.point cloud trianglation時,就是pcl上greedy的那個演算法,在target_link_libraries時,最好將pcl_common放在pcl_search後面,不然容易報錯,提示undefined refence to pcl::getCameraMatrixFromProjectionMatrix
3.如果編譯時出現std::ofstream之類的錯誤,在gradle中改為“-DANDROID_STL=gnustl_static”