Recently migrating a small application from Eclipse development to Android studio, there are native code implementations in the program that were compiled by android.mk such Mk file, but not on Android studio, Because it is by Gradle organization, so makefile in the configuration to convert to Build.gradle in the statement (although actually Gradle is also the organization of a Mk file), while in the migration process encountered some problems, here to record, for later check.
There are two main scenarios for the JNI development of Android: one is to use a compiled. so dynamic library; The following are respectively described:
First, use the already compiled so
This is a relatively simple case, just put the. so file in the appropriate directory. As follows:
. [Module_name]
. . [SRC]
. . . [Main]
. . . . [Jnilibs]
. . . . . [Armeabi]
. . . . . [ARMEABI-V7A]
. . . . . [x86]
. . . . . [MIPS]
Note that the jniLibs directory is placed module below , in Android Studio, the effect is as follows, so after compiling so will be automatically packaged into the APK, the code directly LoadLibrary:
1 // Library name, note that there is no prefix lib and suffix. So 2 system.loadlibrary (libname);
Second, the use of C + + source code
1 r9d or later NDK
First make sure your NDK version is above r9d, and the latest available is R10:
http://tools.android-studio.org/
Thanks for the selfless dedication of the Chinese team at Android Studio.
if below the r9d version, the NDK compilation under Android studio will appear No rule to make target error.
2 Configuring Ndk.dir
local.propertiesAdd the following configuration:
sdk.dir=/path/to/android-sdk
ndk.dir=/PATH/TO/ANDROID-NDK
The yellow part is modified for your native reality, as mine is:
sdk.dir=d\:\\android-sdk-Windowsndk.dir=d\:\\android-ndk
BUILD.G radle Configuration NDK module under the 3 app
Android { "20.0.0" defaultconfig { "com.example.ndksample" 9 1" 1.0" ndk { / / <--This is the name of my C + + module! } } // ... more gradle stuff ... // End of Android section
ndkYou can also configure more options, as follows:
NDK { "Myepicgamecode" "-dandroid_ndk-d_debug dnull=0" // Define Some Macros ldlibs "EGL", "GLESv3", "DL", "Log" // Link with these libraries! Add the library that you originally linked in Makefile ldlibs STL "stlport_shared" // use shared STLport Library }
4 adding C + + source code
By default, you need to place the C + + source code [module]/src/main/jni/ under the path. The effect is as follows:
Of course, you can also customize the source code path:
Android { // : Android settings . sourcesets.main { 'src/main/source'} }
One thing to note here is that there is a bug in the current Windows Android Studio under the NDK compilation, that is, if your source files (. C and. cpp files, not counting. h) There is only one, then there will be compile error, the current Google has not fixed, the temporary solution is to create a new empty. c file to put in there, you can compile the pass .... A little wonderful!!
5 Sub-platform configuration compilation
This step is not required, you can make different compilation configurations for each platform as needed, and you can set the previous compilation options (for example cFlags ). For example, you only want to compile the. So for the specified platform, not all platforms. As follows:
Android { // : Android settings . productflavors { x86 { NDK { "x86" } } arm { NDK { " armeabi-v7a " } } MIPS { NDK { " MIPS "} } // Android
Reference Links:
Http://www.race604.com/android-studio-with-ndk/?utm_source=tuicool
http://blog.csdn.net/sodino/article/details/41946607
http://www.dewen.io/q/17530/Android+Studio+NDK%E5%BC%80%E5%8F%91%E7%BC%96%E8%AF%91%E9%93%BE%E6%8E%A5%E5%87%BA% E9%94%99%e9%97%ae%e9%a2%98
The next Jni app in Android Studio