as android.mk 变色,FFmpeg4Android:AS中使用NDK

1 FFmpeg4Android:AS中使用NDK

1.1 用Android.mk编译.so

step 1:创建AS项目MyDNK,新建JNIS类并写好native方法helloJNI(),如图:

2a1bc389d731

image.png

step 2: 进入java文件夹,使用"javah com.lzp.myndk.JNIS"命令,生成头文件:

2a1bc389d731

image.png

step 3:编写test.c文件,实现helloJNI()函数:

2a1bc389d731

image.png

step 4:修改build.gradle文件:

import org.apache.tools.ant.taskdefs.condition.Os

apply plugin: 'com.android.application'

android {

...

//(1)指定动态库路径

sourceSets{

main{

jni.srcDirs = [] // disable automatic ndk-build call, which ignore our Android.mk

jniLibs.srcDir 'src/main/libs'

}

}

// (2)call regular ndk-build(.cmd) script from app directory

task ndkBuild(type: Exec) {

workingDir file('src/main')

commandLine getNdkBuildCmd()

//commandLine 'E:/Android/android-ndk-r10e/ndk-build.cmd' //也可以直接使用绝对路径

}

tasks.withType(JavaCompile) {

compileTask -> compileTask.dependsOn ndkBuild

}

task cleanNative(type: Exec) {

workingDir file('src/main')

commandLine getNdkBuildCmd(), 'clean'

}

clean.dependsOn cleanNative

}

//根据不同系统获取ndk-build脚本

def getNdkBuildCmd() {

def ndkbuild = getNdkDir() + "/ndk-build"

if (Os.isFamily(Os.FAMILY_WINDOWS))

ndkbuild += ".cmd"

return ndkbuild

}

//获取NDK目录路径

def getNdkDir() {

if (System.env.ANDROID_NDK_ROOT != null)

return System.env.ANDROID_NDK_ROOT

Properties properties = new Properties()

properties.load(project.rootProject.file('local.properties').newDataInputStream())

def ndkdir = properties.getProperty('ndk.dir', null)

if (ndkdir == null)

throw new GradleException("NDK location not found. Define location with ndk.dir in the local.properties file or with an ANDROID_NDK_ROOT environment variable.")

return ndkdir

}

...

step 6:编译.so文件,有两种方式:

(1)使用ndk-build命令:

进入app\src\main,在cmd中执行NDK的ndk-build命令生成动态库(ndk-build所在文件夹,即NDK目录如果没有加入环境变量则需要绝对路径);

(2)使用编译脚本(推荐):

编写Android.mk和Application.mk

Android.mk:

# Android.mk

# walker lee

# http://blog.csdn.net/itismelzp

LOCAL_PATH := $(call my-dir)

# Program

include $(CLEAR_VARS)

LOCAL_MODULE := jni-native # 库文件,对应生成libjni-native.so文件

LOCAL_SRC_FILES :=test.c # 还可添加其他.c文件

LOCAL_C_INCLUDES += $(LOCAL_PATH)/include

LOCAL_LDLIBS := -llog -lz

include $(BUILD_SHARED_LIBRARY)

Application.mk:

APP_ABI :=armeabi

APP_PLATFORM := android-9

点击工具栏中的build->Make Project,会在main目录下生成两个目录libs和obj,动态库便在其中:

2a1bc389d731

image.png

1.2 用CMakeList编译.so

step 1:新建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)

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 )

add_library( jni-native

SHARED

src/main/jni/test.c )

include_directories(jni)

#target_include_directories(jni-native PRIVATE jni)

target_link_libraries( jni-native

${log-lib} )

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值