Note: This answer does not show how you can use CMake after writing native (C++) code. For that setup, read the answer below to get full understanding of the setup and then look at this answer.
Ok so you have bunch of questions. Some of these questions are personal preference type but I will provide them as my personal choice.
#1
This is your choice. I, personally, would use the compiled .so
file. This way I never have to worry about NDK and CMake and .mk files. If you have the file, all you have to do is add the file to the libs
folder (not lib
folder) and make a minor change to your build.gradle
file. That's it.
Change to build.gradle:
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
jniLibs.srcDirs = ['libs']
}
}
#2 & 3
These would be irrelevant with this option.
#4
You would have to do something like this no matter if you use the files or the compiled libraries:
@SuppressWarnings("JniMissingFunction")
public class MyNativeMethods {
static {
System.loadLibrary("my_native_lib");
}
public native int native_method_1(int fd);
public native int native_method_2(int fd);
public native void native_method_3(int fd, int arr[]);
public native int[] native_method_4(int fd);
}
And then, you can call those methods from your Activity
/Fragment
.
Hope this is clear enough.
EDIT (based on comment below):
.so
or .a
files are your native libraries.
the .cpp
, .c
, etc. files are just your native source code files. If you were to use those files in the project, you would have to use a build system (for example, CMake) to use them. CMake would take your source code files and make a .so
library which is again the native library. This is why I suggested to use the .so
files because why do the work of implementing CMake in your project when you don't need to?
If you want to try CMake or learn it in the future, check this answer: C/C++ with Android Studio version 2.2
System.loadLibrary("my_native_lib");
: Here you are telling the Java runtime to add this given library. This way you are creating a link between Java and the C++ code that is within the library. The methods below that line should have the same name as they do in the C++/C code. This way Java runtime will find and open the library and look for those method in the library you load. See more here