官网介绍
https://developer.android.com/studio/projects/add-native-code.html#link-gradle
首先来说一下我今天遇到的问题
1 在自动生成的项目中,添加了一个native方法后,调用此方法报错说找不到。
2 新建cpp文件提示一堆错误。
1在MainActivity中新建一个native方法并调用
按照官方介绍新建项目,选择支持c++
文件预览:
MainActivity
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = (TextView) findViewById(R.id.sample_text);
tv.setText(stringFromJNI());
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
}
native-lib.cpp
#include <jni.h>
#include <string>
extern "C"
jstring
Java_com_yeliang_ndktest_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
CMakeLists.txt
# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds it for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
# Associated headers in the same location as their source
# file are automatically included.
src/main/cpp/native-lib.cpp )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in the
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
运行结果
在MainActivity中新增加一个方法
public native String getStringFromC();
调用此方法
tv.setText(getStringFromC());
native-lib.cpp 在此文件中增加一个方法
#include <jni.h>
#include <string>
extern "C"
jstring
Java_com_yeliang_ndktest_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
jstring
Java_com_yeliang_ndktest_MainActivity_getStringFromC(
JNIEnv *env,
jobject /* this */) {
std::string hello = "这句话来自C";
return env->NewStringUTF(hello.c_str());
}
运行后将会报错
Process: com.yeliang.ndktest, PID: 25628
java.lang.UnsatisfiedLinkError: No implementation found for java.lang.String com.yeliang.ndktest.MainActivity.getStringFromC() (tried Java_com_yeliang_ndktest_MainActivity_getStringFromC and Java_com_yeliang_ndktest_MainActivity_getStringFromC__)
at com.yeliang.ndktest.MainActivity.getStringFromC(Native Method)
at com.yeliang.ndktest.MainActivity.onCreate(MainActivity.java:21)
后来在这个方法上加了一个
extern "C"
我也不知道这个是干嘛的 然后在运行
再此运行好了。
2 新建cpp文件 并调用
新建java类
接下来自己新建ndk-test.cpp文件
添加这个文件到Cmake
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
# Associated headers in the same location as their source
# file are automatically included.
src/main/cpp/native-lib.cpp
src/main/cpp/ndk-test.cpp)
最后一行是添加进来的
src/main/cpp/ndk-test.cpp
重新运行后:
3 Cmake文件中各标签的含义
C文件和C文件编译生成的so
添加倒入的so 并指定倒入so的位置
指定库的路径
包含头文件的路径
添加这个标签后就不用写出头文件的路径全称,而只需要写包含路径的子目录即可。
本地库和倒入库头文件的映射关系
添加关联库
下面是Cmake完整版
# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.
cmake_minimum_required(VERSION 3.4.1)
add_library(
native-lib
SHARED
src/main/cpp/video_player.c)
find_library(
log-lib
log )
set(distribution_DIR ${CMAKE_SOURCE_DIR}/../../../../libs)
add_library(libavcodec-56
SHARED
IMPORTED )
set_target_properties( libavcodec-56
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libavcodec-56.so )
add_library( libavdevice-56
SHARED
IMPORTED )
set_target_properties( libavdevice-56
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libavdevice-56.so )
add_library( libavfilter-5
SHARED
IMPORTED )
set_target_properties( libavfilter-5
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libavfilter-5.so)
add_library( libavformat-56
SHARED
IMPORTED)
set_target_properties( libavformat-56
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libavformat-56.so )
add_library( libavutil-54
SHARED
IMPORTED)
set_target_properties( libavutil-54
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libavutil-54.so )
add_library( libpostproc-53
SHARED
IMPORTED)
set_target_properties( libpostproc-53
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libpostproc-53.so )
add_library( libswresample-1
SHARED
IMPORTED)
set_target_properties( libswresample-1
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libswresample-1.so )
add_library( libswscale-3
SHARED
IMPORTED)
set_target_properties( libswscale-3
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libswscale-3.so)
add_library(libyuv
SHARED
IMPORTED)
set_target_properties( libyuv
PROPERTIES IMPORTED_LOCATION
../../../../libs/armeabi-v7a/libyuv.so)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
include_directories(libs/include)
include_directories(libs/include/libyuv)
#target_include_directories(native-lib PRIVATE libs/include)
target_link_libraries( native-lib libavcodec-56 libavdevice-56 libavfilter-5 libavformat-56
libavutil-54 libpostproc-53 libswresample-1 libswscale-3 libyuv android
${log-lib} )
4 gradle相关配置
gradle完整版
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.ffmpeg07_thread_decode"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
arguments '-DANDROID_PLATFORM=android-21',
'-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=gnustl_static'
}
}
ndk {
abiFilters "armeabi-v7a"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets.main {
jniLibs.srcDirs = ['libs']
jni.srcDirs = []
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}