For the use of ndk (1)

Recently, the boss asked me to develop a streaming media player, which requires the use of NDK. Everyone is familiar with the use of JDK in the usual process, and rarely uses NDK. What is NDK?
Let us give an explanation of the following concepts:

SDK: (Software Development Kit) software development kit; this is a very broad term, so to speak: a collection of related documents, API necessary materials, examples and tools that assist in the development of a certain type of software can be called "SDK" . In Android development, the SDK we call is the android SDK, which provides developers with library files and other development tools; generally speaking, the SDK is the development tool, and a development IDE is added to build a development Environment, such as IDE of eclipse + android SDK

NDK: (Native Development Kit) is similar to the android SDK. It is also a development kit, which is used to develop software for Android phones, but it is different from the SDK in that it uses the C language, while the android SDK uses the Java language;

JDK: (Java Development Kit) Java language software development kit, Java core, including Java runtime environment, class library, Java development tools, in short, JDK is an SDK for Java developers;

ADT: (Android Developer Tools), Android development tools, a plug-in developed by Google, integrated in eclipse, to provide an exclusive development environment for Android development, simply ADT is the development tool of Android on Eclipse, between Eclipse and SDK acts as a bridge;

ANT: is a project management tool of Apache. It is a tool that can automate the steps of software compilation, testing, deployment, etc. It is mostly used for software development in the Java environment.

All in all, we use C and C++ to write programs for Android.
We use JNI to implement communication between JAVA and C and C++ (the details will not be explained)

Here I mainly talk about, I got through some operations from the adb of the mobile phone to the mac.
At the beginning, I downloaded an Android Studio. You know that AS itself has a very powerful function of downloading JDK packages, so I will use AS to download The NDK
process is: Tools->SDK Manager->Android SDK->SDK Tools
write picture description here
(of course the first two are omitted and invisible above)

Of course, after the installation is complete, we can create a new C++ project, such as:
write picture description here
naturally generate a project

At this time, we can see such a project.
write picture description here
We can see that this part is basically automatically generated.

Here we can open MainActivity
write picture description here
. Suppose we write a function, and a basic function will be generated.
But this is not enough.
We also need JNI to open up the channel between C, C++ and Java.
Below is the terminal.


We need to enter javah -d jni -jni com.example.nali.ndkapplication.MainActivity in the java folder to
generate the .h header file

Generally, when you click compile and run at this time, you can output the most basic app, which can be run.

Of course, this is the first step to connect us with the mobile phone, that is, we can use AS to compile the NDK. For the rest, we need to encapsulate the streaming media algorithm itself, to open the channel between the mobile phone and the shell, use the adb shell, open the mobile phone, and Write a hello world on the terminal!

This configuration has been configured for a long time. I have read many strategies on the Internet and summarized them here:
1. I have found most of the online processing of ndk packages, most of which are directly downloaded android ndk packages, such as:
H:\gp The configuration of \Android\android-ndk-r9
is a better configuration path
for them, and my ndk package is downloaded from AS, so the path is not the same.
My path is:
/Users/nali/Library/Android /sdk/ndk-bundle
At this time, we want to use the ndk-build command, but other folders cannot be used. At this time, we need to configure this command
(1) Enter the current user's home directory and
enter cd ~ (note the space in the middle )
(2) Create .bash_profile file
Enter touch .bash_profile
(3) Edit .bash_profile file
Enter open -e .bash_profile
(4) Add two lines after
export ANDROID_NDK_ROOT=/Users/nali/Library/Android/sdk/ndk-bundle
export PATH= P A T H : ANDROID_NDK_ROOT
write picture description here
(5) then save and
enter source .bash_profile

2. After this thing is configured, when we enter ndk-build in any folder, the following will appear:
Zerber:~ nali$ ndk-build
Android NDK: Could not find application project directory !
Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.
/Users/nali/Library/Android/sdk/ndk-bundle/build/core/build-local.mk:151: * Android NDK: Aborting . Stop.

3. At this time, we do a test.
We create a folder, create a jni file in it, and write a simple hello world in
write picture description here
it. This includes an Android.mk, which is similar to a makefile, but uses ndk -build to invoke the format
inside :

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := hello
LOCAL_SRC_FILES := hello.c
LOCAL_CFLAGS += -pie -fPIE
LOCAL_LDFLAGS += -pie -fPIE
include $(BUILD_EXECUTABLE)
~

Among them , the two parameters LOCAL_CFLAGS += -pie -fPIE and LOCAL_LDFLAGS += -pie -fPIE will be explained later

Let's talk about the possible errors
(1) Android NDK: APP_PLATFORM not set. Defaulting to minimum supported version android-14.
make: * No rule to make target /Users/nali/Library/Android/sdk/ndk-bundle/sources/cxx-stl/system/hello.c', needed by/Users/nali/work/ndkTest/obj/local/arm64- v8a/objs/hello/hello.o'. Stop.
PS: It must be that your format is not correct, or you have added special characters
(2) No rule to make target at the beginning
PS: Try source .bash_profile a few more times you are not configured

There are still a few mistakes that I forgot, let's keep going

4. After the execution is successful, two files will be generated outside the jni folder, one is the libs folder, and the other is obj.
Then enter the adb shell
adb push libs/armeabi/helloworld/data
We may encounter such a situation :
Permission denied
I can only tell you that your phone needs to be rooted.
If we continue, we may also need to rewrite the data permission
chmod 777 data

5. Solve the above problem

The following problem is likely to occur:
error: only position independent executables (PIE) are supported.
At this time, we need the above two parameters:
LOCAL_CFLAGS += -pie -fPIE and LOCAL_LDFLAGS += -pie -fPIE

Partly reproduced from:
https://blog.csdn.net/yangzhaomuma/article/details/50467825
https://blog.csdn.net/shouhouhuakai/article/details/40892863
https://blog.csdn.net/wkl305268748/ article/details/13504171

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324760322&siteId=291194637