NDK Cmake 爬坑
来源:互联网 发布:js find的用法 编辑:程序博客网 时间:2024/04/29 01:29
官网介绍
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"jstringJava_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"jstringJava_com_yeliang_ndktest_MainActivity_stringFromJNI( JNIEnv *env, jobject /* this */) { std::string hello = "Hello from C++"; return env->NewStringUTF(hello.c_str());}jstringJava_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" } }}
阅读全文
0 0
- NDK Cmake 爬坑
- nmake + cmake + android ndk
- CMake构建NDK
- NDK、CMAKE、LLDB
- AndroidStudio JNI NDK CMake
- NDK(四)CMake
- JNI NDK (AndroidStudio+CMake )
- NDK开发--CMake篇
- Android-Ndk Cmake编译JNI
- AndroidStudio2.3 NDk Cmake配置
- NDK编译——CMake
- NDK之cmake初步尝试
- Android NDK 开发:CMake 使用
- Android 使用CMake 编译NDK
- CMake语法简介(androidstudio中利用CMake开发NDK)
- android studio 2.2 使用cmake编译NDK
- AndroidStudio用Cmake方式编译NDK代码
- Android Studio2.2 CMAKE高效NDK开发
- 编写还可以的php 图片上传类代码
- VUEX学习笔记(1)-实现计算器(附最详注释)
- Tinker热修复 及walle多渠道打包流程
- Oracle数据库减少redo日志产生方式
- 修复官方duilib-移动鼠标到Button不能变成手型
- NDK Cmake 爬坑
- iOS 绘制简单图形---UIBezierPath篇
- 一些自己常用的adb命令
- SDUT-1525 字符统计2
- squirrel-foundation状态机的使用细节
- Error running Unnamed: Error copying configuration files from /opt/apache-tomcat-7.0.78/conf to /hom
- node+vue实现用户注册--头像上传
- Oracle的知识点(杂记)
- SDUT-2556 传说中的数据结构