Increase the problems encountered in calling Native code in java in C++
Main error:
- In the directory of the corresponding class file, execute the following command to generate the JNI interface file
javah -jni com.phuket.tour.studio.Mp3Encoder
Javah has been abolished in the higher version of java, and it can be replaced by javac at present, using the following code to achieve
javac -h 保存的位置 ./Mp3Encoder.java
// ./Mp3Encoder.java是指在当前文件夹内的Mp3Encoder.java,如果不在当前文件夹内自行添加前面的文件名称
- Execute the ndk-build command to compile the dynamic so library.
The problem here is huge. There are mainly 3 errors, which are all listed below. Starting from point 7, I have been looking for information for a long time to solve it.
My flow chart for each step:
First you need to install the packages you want to use
- ndk, there are a lot of information on the Internet, you can find it yourself
- Android API
- Android SDK PT 和 B U
- cmake
1. Create a new project
2. The first and second steps in the book are to create a Java file and write a local method.
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG, __VA_ARGS__)
Java_com_phuket_tour_mp3encoder_MpeEncoder_encode(JNIEnv * env, jobject obj) {
LOGI("encoder encode");
}
3. Create the jni folder
If there is no such option, create a file called jni directly in main, and tell build.gradle that this is in Android.
4. Generate JNI interface file
5. Create a cpp file in the cpp folder
6. Create the Android.mk file
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES = ./Mp3Encoder.cpp
LOCAL_LDLIBS :=-L$(SYSROOT)/usr/lib -llog
LOCAL_MODULE := jni
include $(BUILD_SHARED_LIBRARY)
An error will be reported later, and I will tell you one by one how to correct the error. First, let’s see what error is reported:
7. Three Questions
- Android NDK: APP_PLATFORM not set. Defaulting to minimum supported version android-16
Solution:
This requires us to set up an Application.mk and set APP_PLATFORM
APP_STL := c++_static
APP_CPPFLAGS := -frtti -fexceptions -std=c++0x
APP_ABI := armeabi-v7a
APP_PLATFORM := android-16
This error will not be reported after running ndk-build once.
- Android NDK: WARNING: APP_PLATFORM android-16 is higher than android:minSdkVersion 1 in D:/Android_app/mp3encoder2/app/src/main/AndroidManifest.xml. NDK binaries will not be compatible with devices older than android-16.
Solution:
This solves it, run it once
Source of the second solution: https://blog.csdn.net/jiao_mrswang/article/details/79820228
- make: *** No rule to make target ‘D:/Android_app/mp3encoder2/app/src/main/jni/./Mp3Encoder.cpp’, needed by ‘D:/Android_app/mp3encoder2/app/src/main/obj/local/armeabi-v7a/objs/jni/./Mp3
Encoder.o’. Stop.
This problem is caused by the LOCAL_SRC_FILES = ./Mp3Encoder.cpp in this Android.mk
No rule to make target: the translation means that there is no rule to create a target, that is, this LOCAL_SRC_FILES is wrong. See the error and know it, Mp3Encoder. The address of cpp is wrong. In fact, it is in cpp, not in jni. I still don't know how to correct the error by modifying the value. I can only determine the target file of Android.mk by other methods:
first delete LOCAL_SRC_FILES = ./Mp3Encoder.cpp, and then as shown below:
Then run: