我想制作一个将NDK与C ++结合使用的Android应用
我在Xamarin Studio中制作了一个新的Android App解决方案,称为ndkTest。我添加了一个文件夹jni,并在其中添加了以下文件:
以下是每个内容:
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ndkTest
LOCAL_SRC_FILES := test.cpp
LOCAL_STATIC_LIBRARIES := my
include $(BUILD_SHARED_LIBRARY)
Application.mk:
APP_ABI := armeabi-v7a
APP_STL := stlport_static
my.h:
#ifndef __MY_H__
#define __MY_H__
#define MY_CONST 1
#ifdef __cplusplus
extern "C"
#endif
short
my_func( short my_param );
#endif /* __MY_H__ */
test.cpp:
// test.cpp
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <iostream>
#include <fstream>
#include <vector>
#include "my.h"
#define SOME_CONST 2*MY_CONST
short some_short;
#ifdef __cplusplus
extern "C"
{
#endif
static void static_func_0()
{
some_short = 0;
}
static void static_func_1()
{
some_short = 1;
}
#ifdef __cplusplus
}
#endif
static void static_print()
{
printf("static_print\n");
printf("some_short = %d\n", some_short);
}
extern "C" short getSomeShort()
{
printf("myExtern\n");
return some_short;
}
这是有趣的地方:我已经安装并正确配置了ndk。在命令行中,我进入项目目录并运行
ndk-build
我得到这个错误:
make.exe: *** No rule to make target [path to ndk]/sources/cxx-stl/stlport/test.cpp', needed byobj/local/armeabi/objs/ndkTest/test.o'. Stop.
这很奇怪,但是无论如何,我可以在Android.mk中指定test.cpp的完整路径,它会构建:
[armeabi] Compile++ thumb: ndkTest <= test.cpp
[armeabi] SharedLibrary : libndkTest.so
[armeabi] Install : libndkTest.so => libs/armeabi/libndkTest.so
所以现在我有了.so文件。将其添加到MainActivity.cs的顶部后using System.Runtime.InteropServices;
:
[DllImport("libndkTest", EntryPoint="getSomeShort")]
static extern short getSomeShort();
我在OnCreate中添加了一个调用。我得到一个DllNotFoundException
。我想念什么?
我找到了一种解决方法:
在Xamarin Studio中,转到“项目选项”,“构建”,“ Android Build”,然后在“高级”选项卡中,取消选中除armeabi-v7a以外的所有受支持的ABI。由于某些原因,即使显然可以使用,该应用也无法在使用arm64-v8a的Samsung S6上运行。
青年汽车
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句