简体   繁体   中英

Bridging from C/C++ to Java in Android App

I want to call functions I implemented in c/c++ within my Java code using JNA. Path for my bridge file, which loads the library:

main/java/com/identifier/projectname/BridgeFile.java

Path for my library I want to call:

main/libs/lib1/functions.c and main/libs/lib1/functions.h

from my functions.c file I call functions from other libs contained within my libs folder (main/libs/lib2/, main/libs/lib3/). The files are more or less "loose" and I call them within my functions.c.

I include jna within my build.gradle:

dependencies {
   implementation 'net.java.dev.jna:jna:5.12.1'
}

My Viewer (file where I load the library) looks like the following:

package com.identifier.projectname;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;

public class Viewer extends SimpleViewManager<TextureView> {

    public interface CLibrary extends Library {
        CLibrary INSTANCE = (CLibrary) Native.loadLibrary("finder", CLibrary.class);
        int testFunc();
    }
    // Calling it like this: int testval = CLibrary.INSTANCE.testFunc();
}

The native code of testFunc inside of my functions.c (declared in functions.h) looks like the following:

int testFunc(){
    return 1;
}

When I call my testFunc() within another Java file, my app crashes, because I don't know how to include the other files with my make file to my functions.c so JNA can find the methods or my lib can't be found under the name finder .

If all of the above code is correct, how do I build my c/c++ library so I can include the headers/source files (.c +.cpp) from the other lib folders and use the lib with JNA?

Another solution path would be to try to create a small binary executable, which will call your method, which it is located, not in the executable, but in a shared library, so you could perhaps better target the cause of your problem which may not be related to JNA

We also use JNA with Java here, I will be happy to help you if you provide more example of c codes and compilation parameters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM