embed python in android NDK - android

I have a android program that has main logic written in NDK. But now I want to evaluate some python codes as part of main logic. So I tried to use this embed python in c http://docs.python.org/2/extending/embedding.html
I created a simple android project, with native.c like this (all I did was just adding python header)
#include <Python.h>
#include <jni.h>
#include <string.h>
#include <android/log.h>
#include <pthread.h>
#define DEBUG_TAG "NDK_AndroidNDK1SampleActivity"
void Java_com_example_com_test_mytest_MainActivity_helloLog(JNIEnv * env, jobject this, jstring logThis)
{
jboolean isCopy;
const char * szLogThis = (*env)->GetStringUTFChars(env, logThis, &isCopy);
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", szLogThis);
(*env)->ReleaseStringUTFChars(env, logThis, szLogThis);
}
Here is my Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog `python2.7-config --ldflags`
LOCAL_MODULE := ndk1
LOCAL_SRC_FILES := native.c
LOCAL_CFLAGS := `python2.7-config --cflags`
#LOCAL_SHARED_LIBRARIES := -lpthread
include $(BUILD_SHARED_LIBRARY)
I got following error when compiling (using Mac)
Compile thumb : ndk1 <= native.c
arm-linux-androideabi-gcc: error: i386: No such file or directory
arm-linux-androideabi-gcc: error: x86_64: No such file or directory
arm-linux-androideabi-gcc: error: unrecognized option '-arch'
arm-linux-androideabi-gcc: error: unrecognized option '-arch'
make: *** [obj/local/armeabi/objs/ndk1/native.o] Error 1
Anyone could help please? :D

Python2.7-config was built for a 64 bit i386 system not for Android. It's passing bad arguments to the compiler in the LOCAL_CFLAGS field. Either run one that you built to cross compile on Android or specify the values yourself. Check google for 'Python-For-Android' (Py4A) on how to embed python in an NDK app.

Related

Android ndk-build linker fails to find prebuilt library function

I'm trying to modify this tutorial to include a prebuilt C library in my Android Studio project (ie. not using the experimental Gradle plugin) http://kvurd.com/blog/compiling-a-cpp-library-for-android-with-android-studio/
The library itself is coming from a client who won't reveal the source code, therefore I have no control over that part of the build process, however they are already following the same tutorial.
The project builds, load-library works and the NDK link (/jni/my-wrapper.c) works fine, until I try to call the actual library function defined in my prebuild header. The error I'm receiving is:
$ ndk-build
[arm64-v8a] Compile : my-wrapper <= my-wrapper.c
[arm64-v8a] SharedLibrary : libmy-wrapper.so
/Users/me/AndroidStudioProjects/MyProject/app/obj/local/arm64-v8a/objs/my-wrapper/my-wrapper.o: In function `Java_com_my_project_SignInActivity_CallFunction':
/Users/me/AndroidStudioProjects/MyProject/app/jni/my-wrapper.c:44: undefined reference to `MyFunction'
collect2: error: ld returned 1 exit status
make: *** [/Users/me/AndroidStudioProjects/MyProject/app/obj/local/arm64-v8a/libmy-wrapper.so] Error 1
Here's my Android.mk:
LOCAL_PATH := $(call my-dir)
# static library info
include $(CLEAR_VARS)
LOCAL_MODULE := libMyLib
LOCAL_MODULE_FILENAME := libMyLib
LOCAL_SRC_FILES := ../prebuild/libMyLib.a
LOCAL_EXPORT_C_INCLUDES := ../prebuild/include
include $(PREBUILT_STATIC_LIBRARY)
# wrapper info
include $(CLEAR_VARS)
LOCAL_C_INCLUDES += ../prebuild/include
LOCAL_MODULE := my-wrapper
LOCAL_SRC_FILES := my-wrapper.c
LOCAL_STATIC_LIBRARIES := libMyLib
include $(BUILD_SHARED_LIBRARY)
And MyLib.h (note that foobar() works fine as it's in the header but as long as I'm calling MyFunction from within my-wrapper.c the ndk-build fails):
#include <math.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int MyFunction(some stuff);
int foobar(){return 1;};
Finally, my-wrapper.c:
#include <MyLib.h>
jbyte Java_com_my_project_SignInActivity_MyFunction(JNIEnv *env, jobject thiz, some other stuff){
// return MyFunction(some other stuff which I cast to C types); //linker fails if uncommented
return foobar(); //works fine
}
That's a C++ mangled name. You can only use it from C++, not from C.
If you really need to call it from C, you might be able to do it like so:
extern int _Z12MyFunctionP9my_structPhS1_S1_(/* whatever the function args are */);
jbyte Java_com_my_project_SignInActivity_MyFunction(
JNIEnv *env, jobject thiz, some other stuff) {
return _Z12MyFunctionP9my_structPhS1_S1_(args);
}
That depends on the code you're calling being compatible as such (if that's the case, you should ask the client to build their APIs as extern "C").
I'd really recommend just moving your code to C++ though.

Android NDK includes

Suppose my Android project uses jni with next structure:
jni__
|__src__
|__first.c
|__second.c
|
|__include__
|__first.h
There are two source files in src directory and one header file in src/include director.
first.h:
#include <string.h>
#ifndef FIRST_H
#define FIRST_H
extern char * text();
#endif
first.c:
#include "first.h"
char * text() {
...
return ...;
}
second.c:
#include "first.h"
#include <jni.h>
jbyteArray Java_com_example_android_MainActivity_getText( JNIEnv* env, jobject this) {
char * str = text();
...
return str;
}
How to compile this source and correctly specify the directory with headers in Android.mk file?
Now my Android.mk file is as follows:
LOCAL_PATH:= $(call my-dir)
LOCAL_SRC_PATH := $(LOCAL_PATH)/../src
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../src/include
include $(CLEAR_VARS)
LOCAL_MODULE := mylib
LOCAL_SRC_FILES := $(LOCAL_SRC_PATH)/first.c \
$(LOCAL_SRC_PATH)/second.c
include $(BUILD_SHARED_LIBRARY)
But when I try to build this sources compilation terminated with error: "first.h: No such file or directory".
LOCAL_C_INCLUDES := $(LOCAL_PATH)/src/include should do it. As stated in ANDROID-MK file in NDK docs that you can find in your NDK directory, $(LOCAL_PATH) refers to the directory that your Android.mk file is located. Assuming that it is located in jni directory, $(LOCAL_PATH)/src/include points to your header directory where the missing file is located.
And I would use #include <first.h> rather than using #include "first.h" according to the general convention of using headers.

Android NDK Build failing for simple native function

I am trying to build a very simple native function using ndk-build and getting the following error.
DriverAdapter.cpp:6:69: error: expected ',' or '...' before 'this'
The following are my .mk and .cpp files
DriverAdapter.cpp
#include <jni.h>
#include <string.h>
#include <android/log.h>
#define DEBUG_TAG "NDK_AndroidNDK1SampleActivity"
void Java_com_ihearhtpi_MainActivity_helloLog(JNIEnv * env, jobject this, jstring logThis)
{
jboolean isCopy;
const char * szLogThis = (*env)->GetStringUTFChars(env, logThis, &isCopy);
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", szLogThis);
(*env)->ReleaseStringUTFChars(env, logThis, szLogThis);
}
Android.mk
LOCAL_PATH := $(call my-dir)
include $(call all-subdir-makefiles)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog
LOCAL_MODULE :=driver
LOCAL_SRC_FILES :=DriverAdapter.cpp
include $(BUILD_SHARED_LIBRARY)
Not sure why this is happening.
The problem is that this is c++ keyword. And you can't use a keyword as variable name.
You can't name your argument "this"

android libpcap -> no such file or directory

I'm trying to use libpcap with android to test it I wrote a simple c program that looks like this
#include <string.h>
#include <pcap.h>
#include <jni.h>
jstring Java_com_example_penguin_AvailableHosts_stringFromJNI(JNIEnv* jobject thiz) {
char *dev, errbuf[PCAP_ERRBUF_SIZE];
dev = pcap_lookupdev(errbuf);
if (dev != NULL)
return (*env)->NewStringUTF(env, dev);
else
return (*env)->NewStringUTF(env, "ERROR");
}
The Android.mk file looks like
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test.c
include $(BUILD_SHARED_LIBRARY)
In the root of my project I call
ndk-build
which gives me the following error
jni/test.c:2:18: fatal error: pcap.h: No such file or directory
compilation terminated.
Is there anything missing in the Android.mk file?

Android ndk-build fails with dyld error

I'm doing the following tutorial http://mobile.tutsplus.com/tutorials/android/ndk-tutorial/.
And I can't seem to compile properly. I get the following error:
dyld: unknown required load command 0x80000022
dyld: unknown required load command 0x80000022
Compile thumb : ndk1 <= native.c
dyld: unknown required load command 0x80000022
make: *** [obj/local/armeabi/objs/ndk1/native.o] Trace/BPT trap
I'm running Mac OS X 10.5.8. I'm using Gnu Make 3.81. I'm using the awk that ships with mac os x. And I'm using android ndk r7b.
The dyld error seems to pop up with a lot of code compiled for 10.6 and then tried on a 10.5.8 machine.
Android.mk make file:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog
LOCAL_MODULE := ndk1
LOCAL_SRC_FILES := native.c
include $(BUILD_SHARED_LIBRARY)
native.c file:
#include <jni.h>
#include <string.h>
#include <android/log.h>
#define DEBUG_TAG "NDK_NDKtestActivity"
void Java_my_mumbo_jumbo_NDKtestActivity_helloLog(JNIEnv * env, jobject this, j\
string logThis)
{
jboolean isCopy;
const char * szLogThis = (*env)->GetStringUTFChars(env, logThis, &isCopy);
__android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "NDK:LC: [%s]", szLogThis);
(*env)->ReleaseStringUTFChars(env, logThis, szLogThis);
}
Am I not including an android library in my path possibly? I've only added the android r7b folder to my path so i could find ndk-build?
Thanks,
Thomas
Please look at this links.it may be helpfull..
http://psrdotcom.blogspot.in/2011/12/android-ndk-jni-windows-xp7-with-3264.html#!http://psrdotcom.blogspot.com/2011/12/android-ndk-jni-windows-xp7-with-3264.html
http://marakana.com/forums/android/examples/49.html
http://mindtherobot.com/blog/452/android-beginners-ndk-setup-step-by-step/
You need to downgrade to NDK 6b as shown here:
https://ar.qualcomm.at/arforums/showthread.php?t=1590
Vaclav

Categories

Resources