Issue
I want to make an Android app that uses NDK with C++
I made a new Android App solution in Xamarin Studio called ndkTest. I added a folder jni, and in there added these files:
- Android.mk
- Application.mk
- my.h
- test.cpp
Here are the contents of each:
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;
}
Here's where it gets interesting: I have ndk installed and properly configured. In the command line, I cd to project directory and run
ndk-build
And I get this error:
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.
That's weird, but whatever, I can specify the full path to test.cpp in Android.mk and it builds:
[armeabi] Compile++ thumb: ndkTest <= test.cpp
[armeabi] SharedLibrary : libndkTest.so
[armeabi] Install : libndkTest.so => libs/armeabi/libndkTest.so
So now I have the .so file. After adding this to top of MainActivity.cs : using System.Runtime.InteropServices;
I added this:
[DllImport("libndkTest", EntryPoint="getSomeShort")]
static extern short getSomeShort();
And I added a call to that in OnCreate. I'm getting a DllNotFoundException
.
What am I missing?
Solution
I found a workaround:
In Xamarin Studio, go to Project Options, Build, Android Build, and in the Advanced tab, uncheck all the Supported ABIs except armeabi-v7a. For some reason, the app doesn't work on a Samsung S6 using arm64-v8a, even though it clearly should.
YMMV
Answered By - dstrube
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.