天天看点

MNN编译android

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"); 

    }

}

继续阅读