docker編譯:
https://github.com/hzung/build-mnn-for-android
我用的win10下的linux子系統,ndk下載下傳:
好像21,22都可以
https://developer.android.google.cn/ndk/downloads/revision_history
不可以的版本:
1. android-ndk-r23-linux
需要更新cmake到3.19以上,否則報錯:
An old version of CMake is being used that cannot automatically detect
compiler attributes. Compiler identification is being bypassed. Some
values may be wrong or missing. Update to CMake 3.19 or newer to use
CMake's built-in compiler identification.
2. android-ndk-r15c-linux-x86_64
ndk15rc報錯:
Neon double or quad precision register expected
這個錯這次沒見到:
Could NOT find Protobuf (missing: Protobuf_LIBRARIES Protobuf_INCLUDE_DIR)
下載下傳MNN代碼:
https://github.com/alibaba/MNN
MNN CMakeLists.txt
CMakelist.txt 設定MNN_ARM82為ON (低精度需要的動态庫)
添加android-NDK:
vim ~/.bashrc
export ANDROID_NDK=/$(ndk電腦的路徑)/android-ndk-r21
source ~/.bashrc
檢視NDK路徑:
echo $ANDROID_NDK
報錯:
CMake Error: CMake was unable to find a build program corresponding to "Unix Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_ASM_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
make: *** No targets specified and no makefile found. Stop.
原因:
沒有找到ndk,
原因1:~/.bashrc沒有權限。
報錯顯示:
bash後,報錯:
bash: /home/xxxxx/.bashrc: Permission denied
解決方法:
sudo chmod 777 ~/.bashrc
source ~/.bashrc
../build_32.sh繼續報錯,需要把build檔案夾中的内容清空,
再編譯
原因2:ndk路徑寫錯了
再Ubuntu子系統裡面,區分大小寫,Windows盤符必須小寫,
比如D盤,子系統中是:
/mnt/d/
修改./bashrc檔案,
export ANDROID_NDK=/mnt/d/soft/android-ndk-r23-linux/android-ndk-r23
編譯64位ok:
cd /mnt/d/third_partyMNN
./schema/generate.sh
cd MNN/project/android
mkdir build_64 && cd build_64 && ../build_64.sh
make -j4
編譯32位報錯,build_32.sh
解決方法:換高版本ndk,編譯ok。
使用動态庫
代碼設定
// 建立session需要的配置
MNN::ScheduleConfig config;
// 選擇Backend
config.type = MNN_FORWARD_CPU;
// 線程數
config.numThread = 2;
// 配置相應的Backend
BackendConfig backendConfig;
// 選擇低精度/一般配置計算
backendConfig.precision = BackendConfig::Precision_Low;
// 或者
// backendConfig.precision = BackendConfig::Precision_Normal;
config.backendConfig = &backendConfig;
android添加庫
public class MNNNetNative { // load libraries
static {
System.loadLibrary("MNN");
System.loadLibrary("MNN_Arm82");
System.loadLibrary("mnncore");
}
}