Before the development of Ubuntu, the project is also old, naturally it is natural to use eclipse to do a variety of Android development. Recently want to switch in Android Studio, a bit unaccustomed. Android Studio provides two different ways for NDK development, using CMake to automatically compile scripts and similar ndk-build on Eclipse. The difference between the two is that if you want to directly develop a new single module you can consider using Cmake,ide with its own auxiliary plug-in, which makes the programmer more comfortable to use. If you want to do two development on the third-party repository, or need more than one Source Library Association, feel or use ndk-build will be more flexible, or old experience do not have to trample so many pits. For the environment, you can refer to the Android Developer documentation for detailed instructions https://developer.android.google.cn/studio/projects/add-native-code.html# Link-gradle below will build an NDK sample project in Android Studio. 1, a new general project, add the Jni folder and subdirectories include the header file, src storage source 2, in Java code, the new Testjni class calls the native method, and uses the Javah command to generate the header file 3 to create the appropriate method file back to the main directory ? Classpath: Class search path, which means looking from the current bin directory? D: Place the generated header file in the current JNI directory? O: Specifies the generated header file name, which is generated by default in the class full pathname (package name + class name. h)Note:-D and-O can only use one of these parameters. Refresh the project directory under Android Studio to see the Javah instructions above, which you can customize in external tools for ease of use later. File->setting->tools->external Tools Click Add So you can right-use 4 in files that need to include native methods The CMake method provides an automatically generated Android.mk script based on the script, but the Ndk-build method needs to write its own compilation configuration script Android.mk local_path: = $ (call My-dir) Include $ (clear_vars) local_c_includes + $ (local_path)/include \ & nbsp $ (local_path)/src local_src_files: = src/hello-jni.c local_module : = Nativelib include $ (build_shared_library) android.mk The document must begin with the definition of the Local_path variable. local_path: = $ (call My-dir) android build system uses Local_path to locate source files. Because setting this variable to hard-coded values is not appropriate, the Android build system provides a macro feature called my-dir. By setting the variable to the return value of the MY-DIR macro function, you can place it in the current directory. The Android build system sets the Clear_vars variable to the location of the clear_vars.mk fragment. Contains makefile fragments to remove local_<name> variables other than Local_path, such as Local_src_files,local_module . include $ (Clear_vars) This is done because the Android build system parses multiple build files and module definitions in a single execution, while local_<name> is a global variable. Clearly they can avoid conflicts, and each native component is called aA module. Local_module variables are used to set a unique name for these modules. The following code sets the name of the module to Nativelib. LOCAL_MODULE : = nativelib actually LOCAL_MODULE : = libnativelib The result is also possible, Compiled build results, the build system will automatically add the appropriate prefix suffix to generate dynamic shared files, if you have already added Lib, then it will be as is, such as: libnativelib.so. You can see the path "Project path \unit2\app\build\intermediates\ndkbuild\debug\obj\local\ schema type \". Use Local_c_includes to specify the search path for the header file, local_src_files specify the source file list, and multiple files can be separated by a space with multiple source file names. Here, a simple description of a native project. The build system that compiles and builds the actual module also needs to contain the appropriate build system fragments, depending on the type of module you want to build. include $ (build_shared_library) where build_shared_library is a pointer to a script that generates a shared file. There are three types of file generation format build_static_library: compiled into a static library.
Build_shared_library: Compiling to Dynamic library build_executable: compiling to native C executable programIt 's not going to start here. Compiling native projects in Android Studio, you also need to configure Build.gradle in the module (APP) directory. Here's what you need to add android { defaultconfig { ndk{ abifilters "Armeabi", "armeabi-v7a", "x86" & nbsp } } externalnativebuild{ ndkBuild{ & nbsp path "src/main/jni/android.mk" } }}apk in different When the hardware platform is running, different shared files are called according to the CPU architecture. ndk{ abifilters "Armeabi", "armeabi-v7a", "x86" will generate three shared files for different CPU architectures, if not configured, the system will default to generate all supported packages, if the NDK configuration changes, to clean project externalnativebuild{ ndkBuild{ path "src/ Main/jni/android.mk " } } Select CMake or NDK-build need to write down the script path here and then run the project to see the results.
Android Studio NDK-BUILLD mode development