I have a C++ project compiled in a shared library. I need to use that library (.so file) in Android with the NDK.
The idea is that someone else would compile the C++ project and give me the .so file, so that I don't have to compile it on my computer.
I need to reference the .so file in my Android.mk file, and all the solutions I tried did not work. That's where I'm stuck.
So:
is it possible to do what I described without creating one shared library per platform?
how can I reference my .so library in my Android.mk? All the solutions I tried did not work.
To reproduce everything from scratch, I did these steps:
1°) Create a HelloWorld C++ project:
Test.h:
#include <iostream>
class Test {
public:
Test();
~Test();
static std::string getTest();
};
Test.cpp:
#include "Test.h"
std::string Test::getTest()
{
return "Hey!!\n";
}
2°) Create the shared library:
g++ -Wall -shared -fPIC -o libTest.so Test.cpp
This gaves me a libTest.so file, which I can use on a C++ project. Now I want to use it on an Android project.
3°) Create a basic empty Android project:
package: com.so.test
name: CppTest
4°) Create a "Test.java" file next to the MainActivity.java file containing:
package com.so.test;
public class Test {
static {
System.loadLibrary("hellondk");
}
public native String getMessage();
}
5°) Open a terminal and:
cd /path/to/my/android/project/app/src/main/java
javac com/so/test/Test.java
javah com.so.test.Test
This creates the "com_so_test_Test.h" file.
cd /path/to/my/android/project
mkdir jni
mv app/src/main/java/com_so_test_Test.h jni
touch jni/com_so_test_Test.cpp
6°) Add the following code in "com_so_test_Test.cpp" file:
#include <jni.h>
#include <stdio.h>
#include "com_so_test_Test.h"
#include "Test.hpp"
JNIEXPORT jstring JNICALL Java_com_so_test_Test_getMessage(JNIEnv* env, jobject ob)
{
return env->NewStringUTF(Test::getTest().c_str());
}
7°) Copy "Test.h" and "libTest.so" files created on steps 1 & 2 into "jni" folder
8°) Create an "Android.mk" file in the "jni" folder containing:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hellondk
LOCAL_SRC_FILES := com_so_test_Test.cpp
include $(BUILD_SHARED_LIBRARY)
9°) Create an "Application.mk" file in the "jni" folder containing:
APP_STL := stlport_static
10°) Back in the terminal, run:
/path/to/your/ndk/ndk-build
And that's the step where I'm stuck. I need to reference the libTest.so file in the Android.mk, but all the solutions I tried did not work. Any idea?
EDIT: here is the error I get:
[arm64-v8a] Compile++ : hellondk <= com_so_test_Test.cpp
[arm64-v8a] SharedLibrary : libhellondk.so
./obj/local/arm64-v8a/objs/hellondk/com_so_test_Test.o: In function `Java_com_so_test_Test_getMessage':
/Users/timsandra/Desktop/CppTest/jni/com_so_test_Test.cpp:8: undefined reference to `Test::getTest()'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [obj/local/arm64-v8a/libhellondk.so] Error 1
Related
I have .so file which I take from apk. With command nm -D libnative-lib.so I took list of functions. I need to call function named Java_com_example_nativelib_utils_Constants_getStringFromJNI . I wrote the code below:
#include <stddef.h>
#include <dlfcn.h>
#include <stdio.h>
int init_library() {
void* hdl = dlopen("./libnative-lib.so", RTLD_LAZY);
if(hdl == 0)
return 0;
else return 1;
}
int main() {
printf("%s\n", (init_library() ? "OK": "ERROR"));
}
and make file:
all: exe
exe: main.c
gcc main.c -fPIC -ldl -o main
but result of program is always "ERROR". I tried libnative-lib.so from x86 and armeabi-v7a architectures (others not present). How to load library and call function from it? C++ or C do not care.
It is not possible to use android .so files in non-android project. similar question is here
I am using Android Studio 2.1.2 I am not using Experimental Plugin
Please Check the following files and Check the error I am getting.
I solved the issue. Edited file is as follows. They way I fixed it may not be the correct way as I am setting property to use deprecated way, but it works. Experimental plugin can still be unstable. I will try with experimental plugin soon.
build.gradle from Module
sourceSets.main {
jniLibs.srcDir 'src/main/libs'
/*jni.srcDirs = [] not using this, I commented this. Please check SO links which explained when to use this and when not to use this*/
}
following 4 files are in jni folder of main
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mylib
LOCAL_SRC_FILES := HelloJni.cpp
include $(BUILD_SHARED_LIBRARY)
Application.mk
APP_ABI := all
HelloJni.cpp
#include <jni.h>
#include <Header.h>
JNIEXPORT jstring JNICALL Java_com_example_m1035325_ndksampleapp_MainActivity_getStringFromJni(JNIEnv *env,jobject thiz)
{
env-> NewStringUTF ( "Hellofrom JNI!");
}
Header.h
#include <jni.h>;
using namespace std;
#ifndef HEADER
#define HEADER
extern "C" {
JNIEXPORT jstring JNICALL Java_com_example_m1035325_ndksampleapp_MainActivity_getStringFromJni(JNIEnv *env, jobject obj);
}
#endif //NDKSAMPLEAPP_HEADER_H
MainActicity.java
static
{
System.loadLibrary("mylib");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tvHello=(TextView)findViewById(R.id.tvHello);
tvHello.setText(getStringFromJni());
}
public native String getStringFromJni();
Here when I hover on method getStringFromJni it shows Can't resolve corresponding JNI function
I have set NDK path in Project Structure and in Path environment variable too.
I am getting following error
Process: com.example.m1035325.ndksampleapp, PID: 12831
java.lang.UnsatisfiedLinkError: No implementation found for java.lang.String com.example.m1035325.ndksampleapp.MainActivity.getStringFromJni() (tried Java_com_example_m1035325_ndksampleapp_MainActivity_getStringFromJni and Java_com_example_m1035325_ndksampleapp_MainActivity_getStringFromJni__)
at com.example.m1035325.ndksampleapp.MainActivity.getStringFromJni(Native Method)
I searched a lot on SO also but I am not getting what I missed?
No error now , above error is fixed. Please check my answer to this question.
I think the problem is in your Android.mk file:
LOCAL_SOURCE_FILE := HelloJni.cpp
AFAIK the Android build system doesn't use variable of that name. It should be:
LOCAL_SRC_FILES := HelloJni.cpp
One important change I made is in file gradle.properties, is as follows
android.useDeprecatedNdk=true
so its related to Android Studio version 2.1.2 , for this version experimental plugin is the default option to use and the approach I am using is deprecated.
I will be trying with experimental plugin soon.
I wanna realize this idea. I spent several days searching for information, but could not find anything. All tutorials say how to write my own library with JNI, but how to wrap already existing? I need just simple tutorial step by step (and why? if it possible). So I wanna start create native android application.
What I have :
I create C++ library in QTCreator by tutorial from youtube: simple library on C++ (.so) with headers (.h) which do simple cout in console:
Not compiled code mylib.cpp:
#include "mylib.h"
MyLib::MyLib() { }
void MyLib::Test() {
qDebug() << "Hello from our DLL";
// .so
}
Header mylib.h:
#ifndef MYLIB_H
#define MYLIB_H
#include "mylib_global.h"
#include <QDebug>
class MYLIBSHARED_EXPORT MyLib
{
public:
MyLib();
void Test();
};
#endif // MYLIB_H
and mylib_global.h (I think it does't matter)
So after build I have myLib.so.
And now I need wrap it in my android app. So I don't understand what I need to do for it.
I'm develop in Android Studio. And what I know:
Create in java package LibWrappClass with native method - something like "simplePrint()":
public native void simplePrint();
I need to create in /src/main folder "jni". Create Android.mk, myLibWrapper.h and myLibWrapper.cpp. But I don't understand what I need to write in Android.mk for connect my myLib.so to "myLibWrapper.h", and where should I put my library with headers. Can anyone help?
After adding the native method in your java code, You simply build the project. Now you need to move to the location where the class files are written by your IDE. Since you use Android Studio, it must be somewhere your project folder with path
out/production/YourModuleName
Open the location in commandline and run the javah command to generate the header file for your native function
javah -d <your jni folder path> <com.YourPackage.YourClass>
The class YourClass should be where you have declared the native method. This command will create a header file with name something like com_YourPackage_YourClass.h with a function declaration looks like
JNIEXPORT void JNICALL Java_com_YourPackage_YourClass_simplePrint
(JNIEnv *, jobject);
Implement this function in a C/C++ file with whatever operations you have to perform on jni side.
Then, Define the Android.mk file, In this case it will be something like
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := lib
LOCAL_SRC_FILES := lib.c
include $(BUILD_SHARED_LIBRARY)
If you have multiple source files add them in LOCAL_SRC_FILES separated by space.
Next, go to your jni folder and build the project using command ndk-build. This will place the .so file inside the android folder libs/armeabi
Finally rebuild the Android project from Android studio and run.
For detailed instructions and complete source checkout this gist
I'm encountering an issue with linking C libraries in my Android project using the Android NDK r8b.
I built with the toolchain for ARM the cURL library (no SSL) and it provided me a libcurl.a file and a libcurl.so.4.2.0 one.
I also created a C file to provide functions to my Java code and to use the cURL library.
Android.mk
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:= libcurl
LOCAL_SRC_FILES := libcurl.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := curljni
LOCAL_SRC_FILES := curljni.c
LOCAL_STATIC_LIBRARIES := libcurl
include $(BUILD_SHARED_LIBRARY)
and now my C file
#include <string.h>
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
jstring Java_test_curljni_MainActivity_stringFromJNI(JNIEnv* env, jobject thiz)
{
CURL *curl;
CURLcode
......
}
When I try to build with the ndk-build command from my project folder, here's the output error :
Compile thumb : curljni <= curljni.c
jni/curljni.c: In function 'Java_test_curljni_MainActivity_stringFromJNI':
jni/curljni.c:8:3: error: unknown type name 'CURL'
jni/curljni.c:9:3: error: unknown type name 'CURLcode'
jni/curljni.c:11:8: warning: assignment makes pointer from integer without a cast [enabled by default]
jni/curljni.c:13:28: error: 'CURLOPT_URL' undeclared (first use in this function)
jni/curljni.c:13:28: note: each undeclared identifier is reported only once for each function it appears in
/cygdrive/c/android-ndk-r8b/build/core/build-binary.mk:252: recipe for target `obj/local/armeabi/objs/curljni/curljni.o' failed
make: *** [obj/local/armeabi/objs/curljni/curljni.o] Error 1
I've tried many things within the Android.mk file, nothing successful. Using a PREBUILT_SHARED_LIBRARY doesn't help because it requires a .so file only and changing the extension from .so.4.2.0 to .so just tells me that the format is not supported when building ...
One thing I'm not sure about if whether I have to include the headers in my C file, but when I do it, it ofcourse don't find them.
As a note, when I build with only the curl library declared in my Android.mk file (the shared library with the C file is ommitted) then nothing is created in the libs folder !
Thanks in advance for your help !
EDIT ::::
Actually it did work mbrenon (I've tried it millions time and now it works, no idea what's different), but now I have
c:/android-ndk-r8b/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6.x-google/../../../. ./arm-linux-androideabi/bin/ld.exe: cannot find ./obj/local/armeabi/libcurl.a: Permission denied
I'm using Cygwin, could that be why I encounter permission issues ?
I manage to get around it when the .so and the .a are created, returning the permission error, chmod on it and running the ndk-build again, but that seems a bit rough !
Plus now none of my curl functions can be reached ("undefined reference to (function)").
$ ../../../ndk-build
SharedLibrary : libcurljni.so
./obj/local/armeabi/objs/curljni/curljni.o: In function `Java_test_curljni_MainActivity_stringFromJNI':
C:\android-ndk-r8b\apps\curljni\project/jni/curljni.c:12: undefined reference to `curl_easy_init'
C:\android-ndk-r8b\apps\curljni\project/jni/curljni.c:14: undefined reference to `curl_easy_setopt'
C:\android-ndk-r8b\apps\curljni\project/jni/curljni.c:15: undefined reference to `curl_easy_perform'
C:\android-ndk-r8b\apps\curljni\project/jni/curljni.c:17: undefined reference to `curl_easy_cleanup'
collect2: ld returned 1 exit status
/cygdrive/c/android-ndk-r8b/build/core/build-binary.mk:378: recipe for target `obj/local/armeabi/libcurljni.so' failed
make: *** [obj/local/armeabi/libcurljni.so] Error 1
and my curljni.c is the following
jstring Java_test_curljni_MainActivity_stringFromJNI(JNIEnv* env, jobject thiz)
{
CURL *curl;
CURLcode res;
char buffer[10];
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "yahoo.com");
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if(res == 0)
return (*env)->NewStringUTF(env, "0 response");
else
sprintf(buffer,"code: %i",res);
return (*env)->NewStringUTF(env, buffer);
}
else {
return (*env)->NewStringUTF(env, "no curl");
}
}
I made sure every needed function is declared in the curl.h
Yes, you do have to include the Curl headers in your C file. To allow ndk-build to find these header files at link time, you'll have to add their path using LOCAL_EXPORT_C_INCLUDES:
include $(CLEAR_VARS)
LOCAL_MODULE:= libcurl
LOCAL_SRC_FILES := libcurl.a
LOCAL_EXPORT_C_INCLUDES := /* put the path to the Curl headers here */
include $(PREBUILT_STATIC_LIBRARY)
This will automatically add the path to the headers in all the targets requiring libcurl, and it should build and link.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
Hi, can anyone suggest me some good resources to learn JNI for Android and some good JNI Tutorials?
Tutorial for ECLIPSE
Here is first and second small tutorials, but if you want to write a simple program that uses JNI , you may continue reading :)
Create Android application project , Once your project has been created, you’ll need to create a new folder inside the top level of the project. To do this right click on your project name → New → Folder. Name this folder jni. Than add class with name SquaredWrapper. add these code in it
package org.edwards_research.demo.jni;
public class SquaredWrapper {
// Declare native method (and make it public to expose it directly)
public static native int squared(int base);
// Provide additional functionality, that "extends" the native method
public static int to4(int base)
{
int sq = squared(base);
return squared(sq);
}
// Load library
static {
System.loadLibrary("square");
}
}
Open terminal. You must compile these code for getting header file. At first call this command.
cd src # change into the source directory
javac -d /tmp/ org/edwards_research/demo/jni/SquaredWrapper.java
Than
cd /tmp
javah -jni org.edwards_research.demo.jni.SquaredWrapper
SO you'll have header file named org.edwards_research.demo.jni.SquaredWrapper in your tmp directory.
it must be something like this
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class org_edwards_research_demo_jni_SquaredWrapper */
#ifndef _Included_org_edwards_research_demo_jni_SquaredWrapper
#define _Included_org_edwards_research_demo_jni_SquaredWrapper
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: org_edwards_research_demo_jni_SquaredWrapper
* Method: squared
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_org_edwards_1research_demo_jni_SquaredWrapper_squared
(JNIEnv *, jclass, jint);
#ifdef __cplusplus
}
#endif
#endif
change these name for some short one for your comfort, for example square.h. Copy this header file in the jni folder of your app. Than create square.c file in same folder, copy this in it.
#include "square.h"
JNIEXPORT jint JNICALL Java_com_example_android_1jni_1second_1demo_SquaredWrapper_squared
(JNIEnv * je, jclass jc, jint base)
{
return (base*base);
}
Add this in your MainActivity.java
int x = SquaredWrapper.to4(2);
x = x*2;
Add Android.mk file in jni folder with this body
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := squared
LOCAL_SRC_FILES := square.c
include $(BUILD_SHARED_LIBRARY)
It must create library from header and cpp files.
So you have only to do some configurations by looking on my first link
Now you can compile it , be sure that your library has created and your lib directory consist it.
Tutorial for Android Studio
Let's consider you have simple android application opened by Android Studio
step 1: open Your application by Android Studio
step 2: Download NDK and set the path to NDK in local properties of your application(below/above of android sdk path) like this ndk.dir=C\:\\Android\\android-ndk-r10e
P.S. for windows double // , for linux one /
step3: Add JNI folder in the app (right click on the app -> new -> folder -> JNI Folder)
step 4 Set up the Gradle by this way:
Add this code in app/build.gradle for starting NDK
sourceSets.main {
jni.srcDirs = []
jniLibs.srcDir 'src/main/libs'
}
tasks.withType(NdkCompile) { // disable automatic ndk-build call
compileTask -> compileTask.enabled = false
}
task ndkBuild(type: Exec) { // call ndk-build(.cmd) script
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine 'cmd', '/c', 'ndk-build.cmd', '-C', file('src/main').absolutePath
} else {
commandLine 'ndk-build', '-C', file('src/main').absolutePath
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
step 4:
Create files Android.mk and Application.mk in JNI folder of the app with this bodies:
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := main
LOCAL_SRC_FILES := main.cpp
include $(BUILD_SHARED_LIBRARY)
Application.mk
APP_ABI := all
include $(BUILD_SHARED_LIBRARY)
In APP_ABI you choose which compilers to use. It
step 5:
We have to manually run NDK as we disable it from build.config. For creating com_example_nativedemo_app_MainActivit.h file in src\main\jn folder we have to run this command in terminal
javah -d .\jni -classpath C:\Intel\INDE\IDEintegration\android-sdk-windows\platforms\android-21\android.jar;..\..\build\intermediates\classes\debug com.example.mydemo.nativedemo.MainActivity
for windows cmd you have to give the full path of files.
For
step 6:
Add file main.cpp in JNI folder with this body:
#include <string.h>
#include <jni.h>
#include <vchat_cpptest_Hellojnicpp.h>
extern "C" {
JNIEXPORT jstring JNICALL
Java_vchat_cpptest_Hellojnicpp_stringFromJNI
(JNIEnv *env, jobject obj)
{
#ifdef __INTEL_COMPILER_UPDATE
return env->NewStringUTF("Hello from Intel C++ over JNI!");
#else
return env->NewStringUTF("Hello from default C++ over JNI!");
#endif
}
I would suggest downloading the ndk. Unzip it and browse the sample folder ndk codes. Start with the hello-jni and go further. It explains a lot with ease.
You can also browse these links and this while going through the code and keep coming back and forth.