My source file can not include some header, because of local flag is not defined.
SSS.cpp:
#include <jni.h>
//This code is not defined:
#ifdef WORD
#include "Word.h"
#endif
//...rest of code
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := word
LOCAL_CPPFLAGS := -DWORD
LOCAL_SRC_FILES := SSS.cpp
include $(BUILD_SHARED_LIBRARY)
And one more moment, project build was successful, but I can not run it because of lots of errors in source file (Eclipse c++ editor still can not see my header).
Probably not -WORD but -DWORD ?
-D defines a macro to be used by the preprocessor.
And what this
-std=c++11
belongs to?
Related
I'm trying to link libmedia_jni.so in my android project so that I could access android_media_MediaCodec.h, but It fails to find the header.
my .cpp:
#include <jni.h>
#include <media/android_media_MediaCodec.h>
and Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := AndroidMediaCodec
LOCAL_SRC_FILES := AndroidMediaCodec.cpp
LOCAL_SHARED_LIBRARY := libmedia_jni
include $(BUILD_SHARED_LIBRARY)
I just want to link the library, because I don't have the whole framework. Is that even possible?
I am trying my hand at Android JNI.
So far I have just written the basic Hello World Android JNI App.
Now I was thinking if it would be possible to build a .so file seperately. Then use that library in my JNI Layer C Code.
So basically I want to build a libsample.so using command line gcc in windows. Then use this .so file in the JNI I write for my android app.
So far from my understanding I should be able to do this by editing the Android.mk file.
But what would those edits be ?
EDIT: Source code attached.
jnitest.cpp -
#include <jni.h>
#include "specialPrint.h"
extern "C" jstring Java_com_example_hwjni_MainActivity_helloFromJni(JNIEnv *env, jobject thiz) {
return env->NewStringUTF(SpecialPrint("This is external .so talking."));
}
specialPrint.h -
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
char* SpecialPrint(char* s);
#ifdef __cplusplus
}
#endif
specialPrint.c -
#include <stdio.h>
#include "specialPrint.h"
char* SpecialPrint(char* s) {
return s;
}
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := MyPrebuiltLib
LOCAL_SRC_FILES = specialPrint.c
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := jnitest
LOCAL_SRC_FILES := jnitest.cpp
LOCAL_SHARED_LIBRARIES := MyPrebuiltLib
include $(BUILD_SHARED_LIBRARY)
P.S. - Although right now I am trying to do this with the source. But eventually I really need to do this stuff without the source of specialPrint. If you could help me achive that then it would be great!
Step 1: declare a prebuilt library as module
Step 2: refer the prebuilt module as local shared library
for example
include $(CLEAR_VARS)
LOCAL_MODULE := MyPrebuiltLib
LOCAL_SRC_FILES = path/to/libSuper.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := DroidJNILib
LOCAL_SRC_FILES := AwsomeCode.cpp
LOCAL_SHARED_LIBRARIES := MyPrebuiltLib
include $(BUILD_SHARED_LIBRARY)
If you have the source
include $(CLEAR_VARS)
LOCAL_MODULE := MyPrebuiltLib
LOCAL_SRC_FILES =mysource.cpp
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := DroidJNILib
LOCAL_SRC_FILES := AwsomeCode.cpp
LOCAL_SHARED_LIBRARIES := MyPrebuiltLib
include $(BUILD_SHARED_LIBRARY)
I have come across a problem with trying to compile cURL with SSL support into an app.
So far, I have successfully compiled the openSSL package into libcrypt.so and libssl.so.
I believe I have successfully compiled a version of libcurl.a with SSL support using the configure script and running it through the cross-chain compiler found in the NDK (under a linux environment).
Now, I am attempting to write a .so library under Eclipse that can be called by the Java code of an Android App.
Here is the file structure so far:
Project Folder ---> jni ---> include ---> curl ---> curl headers
| |
| -> openssl ---> ssl and crypto headers
|
-> libcrypto.so
-> libssl.so
-> libcurl.a
-> jniProcessRequest.c
-> Android.mk
Android.mk reads:
LOCAL_PATH := $(call my-dir)
MY_PATH := $(LOCAL_PATH)
include $(CLEAR_VARS)
LOCAL_PATH := $(MY_PATH)
LOCAL_MODULE := crypto
LOCAL_SRC_FILES := libcrypto.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/openssl/
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := ssl
LOCAL_SRC_FILES := libssl.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/openssl/
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := curl
LOCAL_SRC_FILES := libcurl.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/curl/
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := JNIProcessRequest
LOCAL_SRC_FILES := JNIProcessRequest.c
LOCAL_SHARED_LIBRARIES := crypto ssl
LOCAL_STATIC_LIBRARIES := curl
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
jniProcessRequest.c:
#include <jni.h>
#include <stdlib.h>
#include "include/curl/curl.h"
#include <android/log.h>
JNIEXPORT void JNICALL Java_com_example_jniprocessrequest_MainActivity_jniProcessRequest(JNIEnv * env, jobject obje){
CURL *conn;
conn = curl_easy_init();
}
Every time I attempt to compile the above, I have undefined reference errors in Eclipse:
make: *** [obj/local/armeabi/libJNIProcessRequest.so] Error 1
undefined reference to 'curl_easy_init'
I am thinking this is some sort of linkage error but am struggling to find where the error is occurring. I have spent nearly two days trying all different methods of placing the shared libraries in differing places, switching the static libcurl with a shared libcurl, altering the Android.mk file and following tutorials on how to get cURL working in Android.
Any help would be greatly appreciated!
I believe that the problem was stemming from an incorrectly built libcurl.a static library. I replaced my library with one that was compiled by a user on GitHub and without changing any code, the linkage errors disappeared.
You use curl as a prebuilt module.
As stated by the NDK documentation you should take care to wrap it under include $(CLEAR_VARS) and (most importantly) include $(PREBUILT_STATIC_LIBRARY) directives as follow:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := foo-prebuilt
LOCAL_SRC_FILES := libfoo.a
include $(PREBUILT_STATIC_LIBRARY)
Right after take care to wrap your JNIProcessRequest module with include $(CLEAR_VARS) and include $(BUILD_SHARED_LIBRARY).
For more details please refer to the docs/PREBUILTS.html section that you can find within your Android NDK folder (it looks like this).
I'm a newbie of Android NDK. I want to try create fast blur effect to bitmap and I found a NDK solution from here: Fast Bitmap Blur For Android SDK answered by #zeh
after I did my configuration, I am not able to run the project and It said "Type 'AndroidBitmapInfo' could not be resolved" in the *.c file.
Could you guys tell me how to fix this problem?
Here is my Android.mk
LOCAL_PATH := $(call my-dir)
# Create BitmapUtils library
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog -ljnigraphics -landroid
LOCAL_MODULE := bitmaputils
LOCAL_SRC_FILES := bitmaputils.c
LOCAL_CFLAGS = -ffast-math -O3 -funroll-loops
include $(BUILD_SHARED_LIBRARY)
Thank you
Just in case: have you correctly included the bitmap header?
#include <android/bitmap.h>
Add the following line to your Application.mk
APP_PLATFORM := android-8
In case you don't use Application.mk, run ndk-build as follows:
ndk-build APP_PLATFORM=android-8
according to ndk samples\bitmap-plasma\jni, you'd better double check mk file, and header file.
Application.mk
# The ARMv7 is significanly faster due to the use of the hardware FPU
APP_ABI := armeabi armeabi-v7a
APP_PLATFORM := android-8
----------------------------------------------------------------
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := plasma
LOCAL_SRC_FILES := plasma.c
LOCAL_LDLIBS := -lm -llog -ljnigraphics
include $(BUILD_SHARED_LIBRARY)
--------------------------------------------------------
plasma.c
#include <jni.h>
#include <time.h>
#include <android/log.h>
#include <android/bitmap.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
The configuration of CDT indexer needs to enable "Index unused headers ..."
To get there: Project->Properties->C/C++ General->Indexer.
Than rebuild the project
If still this appears than repeat this again.It will be gone :)
First.h
#ifndef FIRST_H
#define FIRST_H
class Test
{
public:
void create();
void test();
private:
};
#endif /* FIRST_H */
Second.cpp
#include "first.h"
#ifdef __cplusplus
extern "C" {
#endif
jint
Java_com_example_ndkcpp2_MainActivity_stringFromJNI( JNIEnv* env,
jobject thiz )
{
Test t;
t.test();
}
#ifdef __cplusplus
}
#endif
When I do a NDK-Build on second.cpp I got
pp2/jni/second.cpp:44: error: undefined reference to 'Test::test()'
collect2: ld returned 1 exit status
Using C++, you declare Class in .h file and write implementation in .cpp file. for example, you created First.h, then you should create First.cpp, in which write your method implementation like void Test::test(){}. Remember to add First.cpp to your makefile(Android.mk) for compilation.
You have multiple options here assuming that you have a first.cpp file that you implemented the Test class correctly. Without being able to see your Android.mk, I will go through all options:
Build First.cpp as a static or shared library and add this library to your module which compiles Second.cpp. Your Android.mk should look like:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := firstlib
LOCAL_C_INCLUDES := path/to/first.h
LOCAL_SRC_FILES := first.cpp
include $(BUILD_STATIC_LIBRARY)
If you would like First to be a shared library instead of a static library, change include $(BUILD_STATIC_LIBRARY) line to:
include $(BUILD_SHARED_LIBRARY)
Now, your second lib is compiled as follows:
include $(CLEAR_VARS)
LOCAL_MODULE := second
LOCAL_C_INCLUDES := path/to/first.h
LOCAL_C_INCLUDES += path/to/second.h
LOCAL_SRC_FILES := second.cpp
LOCAL_STATIC_LIBRARIES := firstlib
include $(BUILD_SHARED_LIBRARY)
If firstlib is built as shared library, you can link it by chaning LOCAL_STATIC_LIBRARIES += firstlib line to the following:
LOCAL_SHARED_LIBRARIES += firstlib
As a second solution, you can build first.cpp as a part of the second lib and this way you don't have to worry about linking against the first library. This is more like a design choice and how you would like to shape up your libraries:
include $(CLEAR_VARS
LOCAL_MODULE := libtwolib-second
include $(CLEAR_VARS)
LOCAL_MODULE := libtwolib-second
LOCAL_C_INCLUDES := path/to/first.h
LOCAL_C_INCLUDES += path/to/second.h
LOCAL_SRC_FILES := first.cpp
LOCAL_SRC_FILES += second.cpp
include $(BUILD_SHARED_LIBRARY)
Finally, you can find a sample of the first approach in your NDK directory, under samples/twolibs.