I have generated a .so file via C++ on Linus and got .so file generated now I have cross compile this .so file for Android ARM so I have to comiple it via Android NDK so that new genrated .so
can be used in my android project .
So can anyone help me where I have to put Linux generated .so file in my Android project and what to add in Make file(Android.mk) so that it can generate new .so file with existing methods in my previous Linux generated .so file.
I hope my question is clear to you all, if not please tell me.
Please help me. Thanks in advance
Here are the steps to build a native C++ shared object.
Add native support to your project. See Android's Add native support
Add your C++ files to the JNI folder
Create an Android.mk, add it to the the JNI folder, and add the architectures you want to support. For example:
APP_ABI := armeabi x86 mips armeabi-v7a
Pick a C++ runtime library. See the CPLUSPLUS.README in the NDK. I picked STL Port for the runtime (the GNU runtime was toxic due to its license).
Create an Appication.mk, add it to the the JNI folder, and add the magic. For example:
LOCAL_PATH := $(call my-dir)
# NDK_DEBUG_IMPORTS := 1
#########################################################
# STLport library
include $(CLEAR_VARS)
STLPORT_INCL := /opt/android-ndk-r9/sources/cxx-stl/stlport/stlport
STLPORT_LIB := /opt/android-ndk-r9/sources/cxx-stl/stlport/libs/$(TARGET_ARCH_ABI)
LOCAL_MODULE := stlport_shared
LOCAL_SRC_FILES := $(STLPORT_LIB)/libstlport_shared.so
LOCAL_EXPORT_CPPFLAGS :=
LOCAL_EXPORT_C_INCLUDES := $(STLPORT_INCL)
include $(PREBUILT_SHARED_LIBRARY)
LOCAL_SHARED_LIBRARIES := stlport_shared
#########################################################
# Crypto++ library
include $(CLEAR_VARS)
CRYPTOPP_INCL := /usr/local/cryptopp/android-$(TARGET_ARCH_ABI)/include
CRYPTOPP_LIB := /usr/local/cryptopp/android-$(TARGET_ARCH_ABI)/lib
LOCAL_MODULE := cryptopp
LOCAL_SRC_FILES := $(CRYPTOPP_LIB)/libcryptopp.so
LOCAL_EXPORT_CPPFLAGS := -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function
LOCAL_EXPORT_C_INCLUDES := $(CRYPTOPP_INCL) $(CRYPTOPP_INCL)/cryptopp
include $(PREBUILT_SHARED_LIBRARY)
LOCAL_SHARED_LIBRARIES := cryptopp
#########################################################
# PRNG library
include $(CLEAR_VARS)
APP_STL := stlport_shared
APP_MODULES := prng stlport_shared cryptopp
# My ass... LOCAL_EXPORT_C_INCLUDES is useless
LOCAL_C_INCLUDES := $(STLPORT_INCL) $(CRYPTOPP_INCL)
LOCAL_CPP_FEATURES := rtti exceptions
LOCAL_CPP_FLAGS := -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function
LOCAL_CPP_FLAGS += -Wl,--exclude-libs,ALL
LOCAL_LDLIBS := -llog -landroid
LOCAL_SHARED_LIBRARIES := cryptopp stlport_shared
LOCAL_MODULE := prng
LOCAL_SRC_FILES := libprng.cpp
include $(BUILD_SHARED_LIBRARY)
My library depends on STLport. The stuff following "STLport library" ensures my library is compiled against stlport_shared.so, and the stlport_shared.so is copied into the APK.
My library also depends upon a cross-compiled version of Crypto++. Crypto++ was also compiled/linked against stlport_shared.so. The stuff following "Crypto++ library" ensures my library is compiled against libcryptopp.so, and the libcryptopp.so is copied into the APK.
Finally, my library is called out. My library is the stuff following "PRNG library" (its a one file test project). It builds libprng.so, and ensures libprng.so is copied into the APK.
You'll also need Android classes. Here's what mine looks like.
package com.cryptopp.prng;
public class PRNG {
static {
System.loadLibrary("stlport_shared");
System.loadLibrary("cryptopp");
System.loadLibrary("prng");
}
private static native int CryptoPP_Reseed(byte[] bytes);
private static native int CryptoPP_GetBytes(byte[] bytes);
private static Object lock = new Object();
// Class method. Returns the number of bytes consumed from the seed.
public static int Reseed(byte[] seed) {
synchronized (lock) {
return CryptoPP_Reseed(seed);
}
}
// Class method. Returns the number of bytes generated.
public static int GetBytes(byte[] bytes) {
synchronized (lock) {
return CryptoPP_GetBytes(bytes);
}
}
// Instance method. Returns the number of bytes consumed from the seed.
public int reseed(byte[] seed) {
synchronized (lock) {
return CryptoPP_Reseed(seed);
}
}
// Instance method. Returns the number of bytes generated.
public int getBytes(byte[] bytes) {
synchronized (lock) {
return CryptoPP_GetBytes(bytes);
}
}
}
The Android modified build system really sucks. Its sufficiently different from standard make-based projects and poorly documented. But that's what Android offers, so that's what you have to use. Eclipse's Android native support is built around it.
If interested, here's what the wrapper header file looks like. You can use javah to generate it from your DEX file (compiled Java classes).
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_cryptopp_prng_PRNG */
#ifndef _Included_com_cryptopp_prng_PRNG
#define _Included_com_cryptopp_prng_PRNG
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_cryptopp_prng_PRNG
* Method: CryptoPP_Reseed
* Signature: ([B)I
*/
JNIEXPORT jint JNICALL Java_com_cryptopp_prng_PRNG_CryptoPP_1Reseed
(JNIEnv *, jclass, jbyteArray);
/*
* Class: com_cryptopp_prng_PRNG
* Method: CryptoPP_GetBytes
* Signature: ([B)I
*/
JNIEXPORT jint JNICALL Java_com_cryptopp_prng_PRNG_CryptoPP_1GetBytes
(JNIEnv *, jclass, jbyteArray);
#ifdef __cplusplus
}
#endif
#endif
Related
I'm trying to add firebase c++ sdk to my cocos2d-x 3.14 game. So far I've create Android.mk in firebase_cpp_sdk directory:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := firebase-prebuilt
LOCAL_SRC_FILES := libs/android/$(TARGET_ARCH_ABI)/c++/libapp.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := firebase-analytics
LOCAL_SRC_FILES := libs/android/$(TARGET_ARCH_ABI)/c++/libanalytics.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_STATIC_LIBRARY)
Then in my project in Android.mk I've added:
LOCAL_C_INCLUDES += /Users/piotr/Documents/pierdoly/firebase_cpp_sdk/include
LOCAL_STATIC_LIBRARIES += firebase-prebuilt firebase-analytics
$(call import-add-path, /Users/piotr/Documents/pierdoly/firebase_cpp_sdk)
I can sync gradle and build project.
I also can
#include <firebase/app.h>
in AppDelegate.cpp (or h) and it works fine. Even Android studio can see all firebase headers and I can inspect them.
Now, in applicationDidFinishLaunching I've added this:
#if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID)
::firebase::App* app = ::firebase::App::Create(::firebase::AppOptions());
#else
::firebase::App* firebaseApp = ::firebase::App::Create(::firebase::AppOptions(), cocos2d::JniHelper::getEnv(), cocos2d::JniHelper::getActivity());
#endif
There's also alternative version via JNICALL from AppActivity.java (to pass correct activity as some say solution above doesn't work):
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
JNIEXPORT void JNICALL Java_org_cocos2dx_cpp_AppActivity_initFirebase(JNIEnv* env, jobject thiz)
{
::firebase::App* app = ::firebase::App::Create(::firebase::AppOptions(), env, thiz);
}
#endif
Yet my problem is: it won't even compile. There's linker error:
Error:(141) undefined reference to 'firebase::App::Create(firebase::AppOptions const&, _JNIEnv*, _jobject*)'
I can clearly "go" into this function in Android Studio and I've rechecked twice parameters I'm giving. They're fine. Yet linker yells at me.
How can I fix linker? What am I missing here?
I had the same problem but found the answer:
It's about the order of the libraries being linked.
You just have to rearrange it like this:
LOCAL_STATIC_LIBRARIES += firebase-analytics firebase-prebuilt
And it should work.
Say thanks to the guys in this github issue
Cocos uses different utils for native libraries build, it can be cmake or ndk util, so check in your frameworks/runtime-src/proj.android/gradle.properties option PROP_BUILD_TYPE and change if it needs from cmake to ndk-build
then look on build.gradle at frameworks/runtime-src/proj.android/app/build.gradle it must use ndk-build correctly.
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.
If you link with c++_shared (LLVM's libc++) and your app uses pthread_create at least once, any calls to sscanf (after pthread_create) will hang in pthread_mutex_lock.
The reason is frivolous use of 'struct FILE' internals in Google's "android_support" auxiliary library. It's likely this structure was changed in Android 5.0 Bionic.
Since NDK r10d eliminates android_support's buggy sscanf implementation (in commit 47e68e84ee043436387a053c1cd47b97cabbb8ca), it is no longer affected. If you must use an older NDK, apply commit 47e68e84ee043436387a053c1cd47b97cabbb8ca to it as an interim fix.
Simple reproduction follows.
Application.mk:
APP_ABI := armeabi
APP_STL := c++_shared
APP_PIE := true
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test.cpp
include $(BUILD_EXECUTABLE)
test.cpp:
#include <stdio.h>
#include <pthread.h>
void* threadproc(void *) {
return NULL;
}
int main(int argc, char **argv) {
pthread_t thread;
pthread_create(&thread, NULL, threadproc, NULL); // causes __isthreaded to be set
unsigned int foo;
sscanf("12345", "%u", &foo); // locks up by calling Bionic's flockfile on its own fake FLE
}
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.
So I'm attempting to use libopus on my native code for an Android application.
My Android.mk file looks like this:
PLATFORM_PREFIX := /opt/android-ext/
LOCAL_PATH := $(PLATFORM_PREFIX)/lib
include $(CLEAR_VARS)
LOCAL_MODULE := libopus
LOCAL_SRC_FILES := libopus.a
include $(PREBUILT_STATIC_LIBRARY)
# I have to redeclare LOCAL_PATH because the library is in /opt/android-ext/
# and my project is somewhere else. Not very elegant.
LOCAL_PATH := /home/sergio/workspace/Project/jni
include $(CLEAR_VARS)
LOCAL_MODULE := opusUtilsNative
LOCAL_SRC_FILES := opusUtilsNative.c
LOCAL_C_INCLUDES += $(PLATFORM_PREFIX)/include
LOCAL_STATIC_LIBRARIES := android_native_app_glue libopus
include $(BUILD_SHARED_LIBRARY)
And my code in opusUtilsNative.c looks like this:
#include "opusUtilsNative.h"
#include <opus/opus.h>
#include <opus/opus_types.h>
JNIEXPORT jbyteArray JNICALL Java_Project_OpusUtils_encode
(JNIEnv * je, jclass jc, jbyteArray data){
int rc;
opus_int16 * testOutBuffer;
unsigned char* opusBuffer;
OpusDecoder *dec;
dec = opus_decoder_create(48000, 2, &rc);
return data;
}
And when I try to build it, it works fine only if I remove the line that uses the "opus_decoder_create" function. Else I will get this:
error: undefined reference to 'opus_decoder_create'
I can see that opus_decoder_create is clearly defined on opus.h, which is clearly being included since if I exclude that line, I'll get an error regarding the opus_int16 and OpusDecoder declarations. How come some definitions are being included and some aren't?
Any help will be greatly appreciated.
This was tricky. After digging around for a bit, I realized I hadn't cross-compiled the opus library correctly, and I didn't have an ARM binary after all.
A good way to verify if your library was cross-compiled correctly:
cd /opt/android-ext/lib #Or wherever the .a file is
ar x libopus.a
file tables_LTP.o #Or any of the .o files generated by ar x
The output should look like this:
tables_LTP.o: ELF 32-bit LSB relocatable, ARM, version 1 (SYSV), not stripped
Otherwise, you might want to double-check your cross-compilation process.
It's error from linker, not from compiler. You forgot to add reference to correspondent libraries to your Android.mk file, do smth like this:
LOCAL_LDLIBS += -lopus
I forgot to integrate one key library
LOCAL_LDLIBS := -lGLESv2
This fixed my problem.