This article records the basic configuration and introductory tutorials for jni development in Android Studio. It was difficult to use eclipse to configure the ndk environment. Now it is basically transformed to AS. This article is to help the novice students who need help to avoid detours ☺ 1. Environment Configuration The main thing you need to configure is NDK (Native Development Kit). Now Android studio is very convenient and can be downloaded with one click: file → setting → find the following path as shown in the screenshot → select NDK → confirm the application download After the installation is complete, you can start 2. jni hello world! 1. Create a new project Create a new app and test jni development 2. Set up support for jni Open gradle.properties and add:
Open local.properties and add:
Finally, open appbuild.gradle and add ndk configuration under android/defaultConfig
moduleName indicates the name of the compiled so file 3. Create a new interface class for Java to access the C layer Create a Jni tool class and define the interface function. Use native as the function keyword (static can be used or not)
The getJniString() method is the function that interacts with the C layer 4. Generate header file "make-project" compilation completed Open the terminal and run After running successfully, open app/build/intermediates/classes/debug/ and you can find the compiled header file "com_lilei_testjni_JniUtils.h". It is not difficult to find that the header file name is composed of the original registration name + class name
5. Create a folder for jni development Click the app folder, New → Folder → JNI Folder, and select the main folder. After the generation is successful, a jni folder will appear in the main directory. Find the header file just generated and copy it to the jni folder (remember to close the terminal just used, otherwise it cannot be copied) Now that we have the header file, we can create a C++ file in the jni directory for development and name it the same as the header file. Write the code that defines the function in the C++ file
6. Java layer loading so Back to JniUtils, add
7. Run Calling jni function
So far, jni's Hello World has been successfully run. 3. Generate so file The previous article introduces how to run C++ programs, but in actual development, most of the development is done by encapsulating and compiling so files, just like the jar package in java. 1. Configure NDK environment variables Find the file directory of the NDK package installed by Android Studio (E:AndroidStudioSDKSDKndk-bundle) and add it to the system environment variables 2. Create a new mk file Create a new Android.mk in the jni directory
Create a new Application.mk file in the jni directory
3. Compile and generate so Open the terminal and cd to the jni directory Call ndk-build to start compiling so
If the operation is correct, it will be as shown in the figure After the operation is successful, you will see that there are more folders libs and obj under the main folder, which contain the generated so files of various CPUs. There are so files in both libs and obj. Google explains the difference between the two as follows: As part of the build process, the files in the libs folder have been stripped of symbols and debugging information. So you'll want to keep two copies of each of your .so files: One from the libs folder to install on the Android device, and one from the obj folder to install for GDB to get symbols from. In other words, the library generated in the libs directory is stripped of symbol tables and debugging information, while the library under obj has debugging information. At this point, the introduction to jni development has been completed |
<<: Gradle for Android Part 2 (Getting Started with Build.gradle)
>>: Gradle for Android Part 3 (Dependency Management)
As a developer, the last thing you want to see is...
Wearable technology has evolved rapidly over the ...
How does an ordinary user use Mobike ? I thought ...
In the early stages of entrepreneurship , from bu...
WeChat's business strategy has always been co...
A bee lands on a flower to collect nectar (Photo/...
Each product has its own core function, and there...
Dried bamboo shoots are a favorite snack for many...
Windows 10's Wi-FiSense feature has a securit...
Zhongshan appointment registration mini program a...
96 days after injecting US$409 million into Coolp...
For marketing, 2020 was a year of challenges, but...
On Toutiao , there are 50 million people who play...
This tweet starts off by talking about disseminat...