Error compiling jni code with android-ndk-r10e - java

I'm having trouble compiling my jni code using android-ndk-r10e.
Inside the jni folder there is the main c code file hello.c and then there is another folder includeLocal where there is the com_MyPackage_Hello.h file inside. It used to compile fine with previous ndk versions but now I get this error:
Error:error: unknown type name 'jobjectArrayJNICALL'
JNIEXPORT jobjectArrayJNICALL Java_com_MyPackage_Hello_hello(
^
Error:error: conflicting types for 'Java_com_MyPackage_Hello_hello'
JNIEXPORT jobjectArrayJNICALL Java_com_MyPackage_Hello_hello(
^
Information:(Unknown) In file included
jni/includeLocal/Java_com_MyPackage_Hello.h:22:9: note: previous declaration of 'Java_com_MyPackage_Hello_hello' was here
Java_com_MyPackage_Hello_hello(JNIEnv *, jobject, jbyteArray, jint, jint);
^
jni/hello.c: In function 'Java_com_MyPackage_Hello_hello':
Warning:warning: return makes integer from pointer without a cast [enabled by default]
return NULL; /* exception already thrown */
^
Warning:warning: return makes integer from pointer without a cast [enabled by default]
return NULL;
^
Warning:warning: return makes integer from pointer without a cast [enabled by default]
return ret;
^
any ideas how to fix this? I'm not very keen on ndk stuff.
Below is the h. file as well
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_MyPackage_Hello */
#ifndef _Included_com_MyPackage_Hello
#define _Included_com_MyPackage_Hello
#ifdef __cplusplus
extern "C" {
#endif
#undef com_MyPackage_Hello_MIN_PRIORITY
#define com_MyPackage_Hello_MIN_PRIORITY 1L
#undef com_MyPackage_Hello_NORM_PRIORITY
#define com_MyPackage_Hello_NORM_PRIORITY 5L
#undef com_MyPackage_Hello_MAX_PRIORITY
#define com_MyPackage_Hello_MAX_PRIORITY 10L
/*
* Class: com_MyPackage_Hello
* Method: hello
* Signature: ([BI)[Ljava/lang/String;
*/
JNIEXPORT jobjectArray JNICALL
Java_com_MyPackage_Hello_hello(JNIEnv *, jobject, jbyteArray, jint, jint);
#ifdef __cplusplus
}
#endif
#endif

Apparently reformat-code option in android studio 1.3.2 is very dangerous for C/C++ stuff. It should have left a space on jobjectArrayJNICALL like this jobjectArray JNICALL
NDK support is still very limited in general.

Related

Assembly code inlining in java and how to distinguish 32bit and 64bit os in java

I need to write some assembly injection in java code for tasks at my university.
I have a class with native function
import java.io.File;
public class AsmOR {
static {
String path = System.getProperty("user.dir");
System.load(path+File.separator+"mydll.dll");
}
public static native int or(int num1, int num2);
}
Then I compiled the class using command javac -h AsmOR.java and I got header.
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class org_user_AsmFun_AsmOR */
#ifndef _Included_org_user_AsmFun_AsmOR
#define _Included_org_user_AsmFun_AsmOR
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: org_user_AsmFun_AsmOR
* Method: or
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_org_user_AsmFun_AsmOR_or
(JNIEnv *, jclass, jint, jint);
#ifdef __cplusplus
}
#endif
#endif
I used assembly this assembly code, but I don't understand why are r8 and r9 parameters of the function. I tried to read javadoc, but it's useless.
global Java_org_user_AsmFun_AsmOR_or
Java_org_user_AsmFun_AsmOR_or:
mov rax,r8
or rax,r9
ret 32
end
Also, I wanted to use the coprocessor for double sum, but it doesn't work.
fld dword [r8]
fld dword [r9]
fadd st0,st1
fistp dword [rax]
ret 32
How to do this and how to distinguish between 32bit and 64bit version of the systems and load the dll library depending on the version?
Judging by the "mydll.dll" in your code, you are on Windows.
That means the Microsoft x64 calling convention applies:
The first four arguments are placed onto the registers. That means RCX, RDX, R8, R9 for integer, struct or pointer arguments (in that order), and XMM0, XMM1, XMM2, XMM3 for floating point arguments. Additional arguments are pushed onto the stack (right to left). Integer return values (similar to x86) are returned in RAX if 64 bits or less. Floating point return values are returned in XMM0. Parameters less than 64 bits long are not zero extended; the high bits are not zeroed.
So RCX and RDX are the JNIEnv pointer and a pointer to your jclass, respectively.
As for your second question: fld dword [r8] will treat the contents of r8 as a float* and dereference that to get the actual float value. I think you have to use fild dword r8 if you want to load directly from a register.
The third question is already answered here.

How to resolve UnsatisfiedLinkError when using JNI with packages?

Firstly, my example has the following directory structure:
Sample.c
lib/
mypackage/
--Sample.java
Sample.java in mypackage looks like this:
package mypackage;
public class Sample {
public static native int sampleMethod(int x);
public static void main(String[] args) {
System.loadLibrary("Sample");
System.out.println("sampleMethod result: " + sampleMethod(5));
}
}
I run javac mypackage/Sample.java to compile the java file and javah mypackage.Sample to generate the JNI headers. Then I compile the library using the following command:
clang -I"${JAVA_HOME}/include" -I"${JAVA_HOME}/include/darwin" -o lib/libSample.so -shared Sample.c
At this point the directory structure looks like this:
Sample.c
mypackage_Sample.h
lib/
--libSample.so
mypackage/
--Sample.java
--Sample.class
Now when I try to run the example using java -Djava.library.path=./lib/ mypackage.Sample I get the following error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no Sample in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at mypackage.Sample.main(Sample.java:7)
I tried specifying the full path to lib/, but I get the same error.
I am not sure if the code for the header and the implementation matter, but I will post them anyway.
mypackage_Sample.h:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class mypackage_Sample */
#ifndef _Included_mypackage_Sample
#define _Included_mypackage_Sample
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: mypackage_Sample
* Method: sampleMethod
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_mypackage_Sample_sampleMethod
(JNIEnv *, jclass, jint);
#ifdef __cplusplus
}
#endif
#endif
Sample.c:
#include "mypackage_Sample.h"
#include <stdio.h>
JNIEXPORT jint JNICALL Java_mypackage_Sample_sampleMethod
(JNIEnv * env, jclass obj, jint num) {
return num * num;
}
I run this on OS X Yosemite 10.10.5 using clang 7.0.2 and java 1.8.0_101.
It looks like you have the wrong name from your library file (libSample.so).
If you use:
System.loadLibrary("Sample");
The JVM will map this name to a platform specific file name to try and load. On Linux that is libSample.so, on Windows that is Sample.dll, but on OS X it's something else.
You can find out which name your library file should have by looking at the output of:
System.mapLibraryName("Sample");
Called on the target platform.
After that, you can use that as the name of your library file.

Java not loading dependent libraries

I am trying to use JNI for the first time but when I run my java program I keep on getting an UnsatisfiedLinkError. The error says that it can't find my dependent libraries. I did see the question JNI Hello World Unsatisfied Link Error but their recommendations did not work. As a side-note I don't own microsoft visual studio and I'm running on windows 10.
I am using the following commands in order to compile and run my program:
javac HelloWorld.java
javah HelloWorld
gcc -Wl,--add-stdcall-alias -I"C:/Program Files/Java/jdk1.8.0_91/include" -I"C:/Program Files/Java/jdk1.8.0_91/include/win32" -shared -o HelloWorld.dll HelloWorld.c
java -Djava.library.path=. HelloWorld
Source Code:
Java Source (HelloWorld.java):
public class HelloWorld {
static {
System.loadLibrary("HelloWorld");
}
private static native void sayHello();
public static void main(String[] args) {
sayHello();
}
}
Generated header file (HelloWorld.h):
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorld */
#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloWorld
* Method: sayHello
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloWorld_sayHello
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
C file (HelloWorld.c):
#include <jni.h>
#include <stdio.h>
#include "HelloWorld.h"
JNIEXPORT void JNICALL Java_HelloWorld_sayHello(JNIEnv *env, jclass cls) {
printf("C says hello!");
}
Take a look here for a simple HelloWorld sample:
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo001
(Linux/OSX).
In case you want to use shared library, make sure JVM can see it. You can use: "-Djava.library.path=" or you should make sure library is on LD_LIBRARY_PATH.

vc++ JNI error LNK2019: unresolved external symbol

Good day.
I'm trying to compile a library for use it in Java. But getting the error "error LNK2019: unresolved external symbol". In c ++ I do not much understand, ask for help.
Begin cpp file
//ftrJavaScanAPI.cpp : Defines the entry point for the DLL application.
//
#ifdef _WINDOWS
#pragma warning (disable:4996)
#endif
#include "C:\ftrJavaScanAPI\ftrScanAPI.h"
#include "ftrJavaScanAPI.h"
#ifdef FTR_OS_UNIX
#include <string.h>
#endif
FTRHANDLE hDevice = NULL;
FTRSCAN_IMAGE_SIZE m_ImageSize;
FTR_DWORD m_dwErrCode = 0;
#ifdef _WINDOWS
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
#endif
JNIEXPORT jboolean JNICALL Java_com_Futronic_ScanApiHelper_Scanner_OpenDevice(JNIEnv *env, jobject obj)
{
hDevice = ftrScanOpenDevice();
if( hDevice == NULL )
return JNI_FALSE;
return JNI_TRUE;
}
Begin .h file:
#include <C:\Program Files\Java\jdk1.7.0_05\include\jni.h>
/* Header for class com_Futronic_ScanApiHelper_Scanner */
#ifndef _Included_com_Futronic_ScanApiHelper_Scanner
#define _Included_com_Futronic_ScanApiHelper_Scanner
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_Futronic_ScanApiHelper_Scanner
* Method: OpenDevice
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL Java_com_Futronic_ScanApiHelper_Scanner_OpenDevice
(JNIEnv *, jobject);
Error on Debug
1>ftrJavaScanAPI.obj : error LNK2019: unresolved external symbol ftrScanOpenDevice referenced in function Java_com_Futronic_ScanApiHelper_Scanner_OpenDevice
I understand that it is necessary add code to export some symbols from the DLL so that an export library, but do not know how to do it
What are you linking into your DLL. Your source files don't contain function 'ftrScanOpenDevice' make sure you add appropriate .LIB file to your build.
Before you introduce the complexities of DLL. Make you can compile and run this simple program:
#include "C:\ftrJavaScanAPI\ftrScanAPI.h"
int main ()
{
FTRHANDLE hDevice = ftrScanOpenDevice();
}
Make sure you can compile this and produce EXE file. Also, your executable should run without an error. It should not produce any output. If get you errors regarding DLL files not found, make sure you have appropriate DLL files available in your path or in the same directory that contains your EXE file. Also, make sure you have the same DLLs available for your final JNI program.
One more thing, you shouldn't hard code the absolute names for your include files. Use Visual Studio settings to add 'FTRScanAPI' to your include and library paths. So your include directive should be just
#include "ftrScanAPI.h"

Call C from Android using JNI with only a C shared library and header file

Android Studio 0.3.1
Hello,
I have the following library written in C but don't have the source code for it only the libapp_module.so
libapp_module.so: ELF 32-bit LSB shared object, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
I have the header file for this library:
#ifdef __cplusplus
extern "C" {
#endif
#ifdef _WIN32
#define LIB_API(type) __declspec(dllexport) type
#else
#define LIB_API(type) type
#endif
LIB_API(int) module_init();
#ifdef __cplusplus
}
#endif
The problem is the exception when I try and call the function from my Android App:
D/dalvikvm﹕ Added shared lib /data/app-lib/com.sunsystems.snaptest-1/libapp_module.so 0x416f3d88
D/dalvikvm﹕ No JNI_OnLoad found in /data/app-lib/com.sunsystems.snaptest-1/libapp_module.so 0x416f3d88, skipping init
W/dalvikvm﹕ No implementation found for native Lcom/sunsystems/snaptest/App_Module;.module_init:()I
UnsatisfiedLinkError: Native method not found: com.sunsystems.snaptest.App_Module.module_init:()I
This is what I am doing
I have created a Android App and I want to call that module_init() from my App so I have created a class called App_Module.java
public class App_Module {
/* Load native C libraries */
static {
System.loadLibrary("app_module");
}
public native static int module_init();
}
And I used JNI like this in my root of the project:
javah -jni -classpath build/classes/debug -d jni/ com.sunsystems.snaptest.App_Module
Which generated the following header interface:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_sunsystems_snaptest_App_Module */
#ifndef _Included_com_sunsystems_snaptest_App_Module
#define _Included_com_sunsystems_snaptest_App_Module
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_sunsystems_snaptest_App_Module
* Method: module_init
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_sunsystems_snaptest_App_Module_module_1init
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
Then in my Android App I load the library in the above App_Module class and call it like this:
App_Module.module_init()
So I guess it cannot find symbol inside the libapp_module.so library.
Many thanks for any suggestions,
You have to implement the native method and make it call the function in your library. For example:
#include "header_file_for_your_library.h"
#include "com_sunsystems_snaptest_App_Module.h"
JNIEXPORT jint JNICALL Java_com_sunsystems_snaptest_App_Module_module_1init(JNIEnv *env, jclass klass) {
return module_init();
}
Just about any tutorial on JNI or Android NDK will have further details.

Categories