一. 从C++返回hello world

这一步是android studio自带功能

新建工程时选择native c++即可

Screenshot from 2020-12-10 00-41-57.png

做一个简单的记录

build.gradle

3处修改

android {

compileSdkVersion 29

buildToolsVersion "29.0.3"

defaultConfig {

minSdkVersion 19

targetSdkVersion 29

versionCode 1

versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

consumerProguardFiles "consumer-rules.pro"

// 1) 设置cppFlags

externalNativeBuild {

cmake {

cppFlags ""

}

}

}

buildTypes {

release {

minifyEnabled false

proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

}

}

// 2) 指定 CMakeLists.txt的路径和版本

externalNativeBuild {

cmake {

path "src/main/cpp/CMakeLists.txt"

version "3.10.2"

}

}

// 3)as 3.6.3上需要加上这段代码,告诉gradle本地库的路径,4.0上不需要

sourceSets {

main {

jniLibs.srcDirs = ['src/main/ffmpegLibs/lib']

}

}

}

编写jni代码和对应的java调用代码

jni代码

#include

#include

//这里的方法名字要和调用层 FFmpegWrapper的方法签名保持一致,

//并且如果源文件是C++代码,要加上extern "C",不然会影响链接器链接,导致找不到方法

``

extern "C"

JNIEXPORT jstring JNICALL

Java_com_lv_ffmpeg_FFmpegWrapper_ffmpegVersion(JNIEnv *env, jobject thiz) {

std::string hello = "Hello from C++";

return env->NewStringUTF(hello.c_str());

}

java调用代码

public class FFmpegWrapper {

//加载.so

static {

System.loadLibrary("native-lib");

}

public native static String ffmpegVersion();

}

编写CmakeFile

# For more information about using CMake with Android Studio, read the

# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

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 them 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).

native-lib.cpp )

# Searches for a specified prebuilt library and stores the path as a

# variable. Because CMake includes system libraries 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 this

# 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} )

二. 使用编译好的.so

设置支持的CPU架构

defaultConfig {

applicationId "com.lv.media_demo"

minSdkVersion 19

targetSdkVersion 29

versionCode 1

versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

//由于编译的.so是架构类型是arm64-v8a,所以这里限制只支持'arm64-v8a

ndk {

//abiFilters 'armeabi-v7a' //, 'arm64-v8a', 'x86', 'x86_64', 'mips', 'mips64'

abiFilters 'arm64-v8a' //, 'arm64-v8a', 'x86', 'x86_64', 'mips', 'mips64'

}

}

复制头文件和.so文件到工程

Screenshot from 2020-12-10 01-01-49.png

修改cmakefile.txt

有4出修改

# For more information about using CMake with Android Studio, read the

# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# 1) 设置.so和头文件存放的位置

set(jni_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../ffmpegLibs)

# 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 them 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).

native-lib.cpp)

# 2) 设置头文件的查找路径

target_include_directories(native-lib PRIVATE

${jni_DIR}/include)

# 3)添加一个.so依赖

add_library(avcodec SHARED IMPORTED)

set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION

${jni_DIR}/lib/${ANDROID_ABI}/libavcodec.so)

add_library(avformat SHARED IMPORTED)

set_target_properties(avformat PROPERTIES IMPORTED_LOCATION

${jni_DIR}/lib/${ANDROID_ABI}/libavformat.so)

add_library(avfilter SHARED IMPORTED)

set_target_properties(avfilter PROPERTIES IMPORTED_LOCATION

${jni_DIR}/lib/${ANDROID_ABI}/libavfilter.so)

add_library(avutil SHARED IMPORTED)

set_target_properties(avutil PROPERTIES IMPORTED_LOCATION

${jni_DIR}/lib/${ANDROID_ABI}/libavutil.so)

add_library(swscale SHARED IMPORTED)

set_target_properties(swscale PROPERTIES IMPORTED_LOCATION

${jni_DIR}/lib/${ANDROID_ABI}/libswscale.so)

add_library(swresample SHARED IMPORTED)

set_target_properties(swresample PROPERTIES IMPORTED_LOCATION

${jni_DIR}/lib/${ANDROID_ABI}/libswresample.so)

# Searches for a specified prebuilt library and stores the path as a

# variable. Because CMake includes system libraries 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 this

# build script, prebuilt third-party libraries, or system libraries.

# 4) 添加到链接库

target_link_libraries( # Specifies the target library.

native-lib

avcodec

avformat

avfilter

avutil

swscale

swresample

# Links the target library to the log library

# included in the NDK.

${log-lib})

修改native-lib.cpp,获取ffmpeg的版本号

#include

#include

extern "C" {

#include "libavutil/avutil.h"

}

extern "C"

JNIEXPORT jstring JNICALL

Java_com_lv_ffmpeg_FFmpegWrapper_ffmpegVersion(JNIEnv *env, jclass clazz) {

std::string hello = "Hello from C++";

return env->NewStringUTF(av_version_info());

}

android studio 加载ffmpeg.so,Android studio使用已经编译好的ffmpeg .so库相关推荐

  1. android 自定义加载动画效果,Android自定义View实现loading动画加载效果

    项目开发中对Loading的处理是比较常见的,安卓系统提供的不太美观,引入第三发又太麻烦,这时候自己定义View来实现这个效果,并且进行封装抽取给项目提供统一的loading样式是最好的解决方式了. ...

  2. android module 加载.so,关于Android Studio主Module与依赖Module同时引入so库的问题

    在使用so库的时候遇到一个问题,背景以下:java 项目中有一个录像功能,将录像功能抽取出来变成一个module,这个module引入了一个ffmpeg的so库,将录像功能集成后经测试好用.架构 后来 ...

  3. android异步加载视频缩略图,Android 视频缩略图的缓存机制和异步加载

    关注微信号:javalearns   随时随地学Java 或扫一扫 随时随地学Java 在这次的工作开发项目中,涉及到一个视频缩略图的视频列表:这个在大家看来,制作视频缩略图就是两行代码就搞定的事.确 ...

  4. android无法加载系统so,android - android资源无法加载 - SO中文参考 - www.soinside.com

    android资源无法加载 问题描述 投票:0回答:1 Android resource linking failed /Users/suhaibaskar/Downloads/AndroidStud ...

  5. android webview加载闪屏,Android Webview:加载url时出现闪屏

    我是Android应用程序中的新手,这是我的第一个应用程序. 我已经创建了启动画面和工程..但其后走了一个长长的白色空白屏幕约2-5秒,然后URL开始加载..Android Webview:加载url ...

  6. android资源加载失败,解决Android WebView拦截url,视频播放加载失败的问题

    解决Android WebView拦截url,视频播放加载失败的问题 发布时间:2020-10-08 05:19:44 来源:脚本之家 阅读:86 作者:灵均子孟 需求:Android调用webVie ...

  7. android webview加载不出来,android 中用webview 显示本地html,为什么里面的内容显示不出来?...

    // 设置WebView属性 WebSettings settings = webView.getSettings(); //支持js settings.setJavaScriptEnabled(tr ...

  8. android 自定义加载动画效果,Android 自定义View修炼-自定义加载进度动画LoadingImageView...

    一.概述 本自定义View,是加载进度动画的自定义View,继承于ImageView来实现,主要实现蒙层加载进度的加载进度效果. 支持水平左右加载和垂直上下加载四个方向,同时也支持自定义蒙层进度颜色. ...

  9. android webview 加载本地pdf,android – 在WebView中打开PDF

    我想在我的WebView中打开一个PDF,我在这个论坛上找到并组合了代码. 但是,虽然我安装了多个PDF应用程序,包括Adobe Reader,但它仍然可以找到"找不到PDF应用程序&quo ...

  10. Android动态加载技术

    基本信息 Author:kaedea GitHub:android-dynamical-loading 我们很早开始就在Android项目中采用了动态加载技术,主要目的是为了达到让用户不用重新安装AP ...

最新文章

  1. postman api
  2. python数据挖掘例题_数据挖掘与python实践试题及答案
  3. 通过History Trends Unlimited通过统计服务器上Chrome浏览器Top10网页历史访问量(2021.11.23)
  4. 全志 增加启动默launcher函数 Patch
  5. Matlab 数字图像处理1---图像的收缩和放大
  6. 搭建elasticsearch测试工程
  7. P5022-旅行【基环树,dfs】
  8. opencv 修改图像数值_【1】Introduction to OpenCV (2)使用VS生成OpenCV应用程序
  9. https传输基于多ip实现的网站数据传输
  10. 基于flowplayer的视频缩略图的视频预览
  11. 深入了解创宇网络安全硬件产品--零信任(ZTSA)
  12. 《缠中说禅108课》27: 盘整背驰与历史性底部
  13. defcon quals 2016 feedme writeup
  14. 腾讯云服务器8核16G18M配置测评
  15. intel(r)wireless-ac9462异常//笔记本电脑网络无法连接???吐血后总结 :一次解决,史上最全,N种方案
  16. 【烈日炎炎战后端】JAVA基础(3.4万字)
  17. 电脑连接电视html,电脑HDMI连接电视无信号的解决方法
  18. js数组遍历所有元素方法 总结
  19. 经典四大排序(动图实现)
  20. ITN网络课程笔记(十七)(完)

热门文章

  1. 小米 MySQL 数据实时同步到大数据数仓的架构与实践
  2. Hadoop 03_核心:读写流程
  3. javaWeb Note1
  4. C++学习笔记:(二)函数重载 常量与引用
  5. spark任务jvm内存溢出
  6. 【完美解决】Could not process result for mapping: ResultMapping{property=‘null‘, column=‘xxx‘, javaType=
  7. Python类的结构及属性的获取机制
  8. Web前端开发笔记——第二章 HTML语言 第二节 基本标签
  9. php论坛有哪些_建网站的软件哪个好?建网站的软件有哪些?
  10. 扫地机器人的特点描写_描写扫地机器人五年级作文500字