ijkplayer android端jni层调试

转自: https://www.jianshu.com/p/560110afcb4d

亲测可用

本篇文章将跟大家分享一下如何配置ijkplayer的编译脚本,使得我们可以在android studio IDE下调试android端c层的代码。

环境要求

Android studio3.0.1版本 (可以使用其他版本,但是下面的配置就要做相应的改变,但必须是3以上的版本)

Android NDK12+版本 (这个是官网要求的,只有在12+以上才支持c层调试)

操作步骤

  1. 拉取ijkplayer代码 (git clone https://github.com/Bilibili/ijkplayer.git ijkplayer-android)

  2. cd ijkplayer-android

  3. git checkout -B latest k0.8.8

  4. 运行sh android/patch-debugging-with-lldb.sh armv7a,这一步很可能出错,可以改为下列步骤进行:

1). 查看一下patch-debugging-with-lldb.sh文件中需要patch的文件,可以看出对于armv7a来说需要添加的patch有:

android/patches/0001-gitignore-ignore-.externalNativeBuild.patch
android/patches/0002-gradle-upgrade-build-tool-to-2.2.0-beta2.patch
android/patches/0003-armv7a-enable-debugging-with-LLDB.patch
android/patches/0004-armv7a-link-prebuilt-staic-libraries-of-ffmepg.patch

2). 按照patch文件做相应的修改,修改如下(因为版本原因,与ijkplayer提供的修改内容不太一样):

a)=====android/patches/0001-gitignore-ignore-.externalNativeBuild.patch=====

        diff --git a/android/ijkplayer/.gitignore b/android/ijkplayer/.gitignore
        index eac8754..2a3badc 100644
        --- a/android/ijkplayer/.gitignore
        +++ b/android/ijkplayer/.gitignore
        @@ -6,3 +6,4 @@
         /build
         /captures
         android-ndk-prof
        +.externalNativeBuild
        \ No newline at end of file
        -- 

b)=====android/patches/0002-gradle-upgrade-build-tool-to-2.2.0-beta2.patch=====

        diff --git a/android/ijkplayer/build.gradle b/android/ijkplayer/build.gradle
        index 0de03ec..6132c1d 100644
        --- a/android/ijkplayer/build.gradle
        +++ b/android/ijkplayer/build.gradle
        @@ -5,7 +5,7 @@ buildscript {
                 jcenter()
             }
             dependencies {
        -        classpath 'com.android.tools.build:gradle:2.1.3'
        +        classpath 'com.android.tools.build:gradle:3.0.1'
         
                 classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
                 classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
        -- 

c)=====android/patches/0003-armv7a-enable-debugging-with-LLDB.patch=====

        diff --git a/android/ijkplayer/ijkplayer-armv7a/build.gradle b/android/ijkplayer/ijkplayer-armv7a/build.gradle
        index b3b7b3a..9cc99b5 100644
        --- a/android/ijkplayer/ijkplayer-armv7a/build.gradle
        +++ b/android/ijkplayer/ijkplayer-armv7a/build.gradle
        @@ -10,16 +10,33 @@ android {
             defaultConfig {
                 minSdkVersion 9
                 targetSdkVersion rootProject.ext.targetSdkVersion
        +        externalNativeBuild {
        +            ndkBuild {
        +                arguments "NDK_APPLICATION_MK:=src/main/jni/Application.mk"//,"APP_ABI=armeabi-v7a","NDK_ALL_ABIS=armeabi-v7a"//,"NDK_LIBS_OUT:=src/main/libs"
        +//                cFlags "-DTEST_C_FLAG1", "-DTEST_C_FLAG2"  // output dir -> 'src/main/libs'
        +//                cppFlags "-DTEST_CPP_FLAG2", "-DTEST_CPP_FLAG2"
        +                abiFilters "armeabi-v7a"
        +            }
        +        }
             }
             buildTypes {
                 release {
                     minifyEnabled false
                     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                 }
        +        debug {
        +            debuggable true
        +            jniDebuggable true
        +            ndk {
        +                debuggable true
        +            }
        +        }
             }
        -    sourceSets.main {
        -        jniLibs.srcDirs 'src/main/libs'
        -        jni.srcDirs = [] // This prevents the auto generation of Android.mk
        +
        +    externalNativeBuild {
        +        ndkBuild {
        +            path 'src/main/jni/Android.mk'
        +        }
             }
        +     buildToolsVersion '26.0.2'
        }


        diff --git a/android/ijkplayer/ijkplayer-armv7a/src/main/jni/Android.mk b/android/ijkplayer/ijkplayer-armv7a/src/main/jni/Android.mk
        index ba2db29..a126226 100644
        --- a/android/ijkplayer/ijkplayer-armv7a/src/main/jni/Android.mk
        +++ b/android/ijkplayer/ijkplayer-armv7a/src/main/jni/Android.mk
        @@ -22,25 +22,7 @@ MY_APP_JNI_ROOT := $(realpath $(LOCAL_PATH))
        MY_APP_PRJ_ROOT := $(realpath $(MY_APP_JNI_ROOT)/..)
        MY_APP_ANDROID_ROOT := $(realpath $(MY_APP_PRJ_ROOT)/../../../..)

        # ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)
        MY_APP_FFMPEG_OUTPUT_PATH := $(realpath $(MY_APP_ANDROID_ROOT)/contrib/build/ffmpeg-armv7a/output)
        MY_APP_FFMPEG_INCLUDE_PATH := $(realpath $(MY_APP_FFMPEG_OUTPUT_PATH)/include)
        # endif
        # ifeq ($(TARGET_ARCH_ABI),armeabi)
        # MY_APP_FFMPEG_OUTPUT_PATH := $(realpath $(MY_APP_ANDROID_ROOT)/contrib/build/ffmpeg-armv5/output)
        # MY_APP_FFMPEG_INCLUDE_PATH := $(realpath $(MY_APP_FFMPEG_OUTPUT_PATH)/include)
        # endif
        # ifeq ($(TARGET_ARCH_ABI),arm64-v8a)
        # MY_APP_FFMPEG_OUTPUT_PATH := $(realpath $(MY_APP_ANDROID_ROOT)/contrib/build/ffmpeg-arm64/output)
        # MY_APP_FFMPEG_INCLUDE_PATH := $(realpath $(MY_APP_FFMPEG_OUTPUT_PATH)/include)
        # endif
        # ifeq ($(TARGET_ARCH_ABI),x86)
        # MY_APP_FFMPEG_OUTPUT_PATH := $(realpath $(MY_APP_ANDROID_ROOT)/contrib/build/ffmpeg-x86/output)
        # MY_APP_FFMPEG_INCLUDE_PATH := $(realpath $(MY_APP_FFMPEG_OUTPUT_PATH)/include)
        # endif
        # ifeq ($(TARGET_ARCH_ABI),x86_64)
        # MY_APP_FFMPEG_OUTPUT_PATH := $(realpath $(MY_APP_ANDROID_ROOT)/contrib/build/ffmpeg-x86_64/output)
        # MY_APP_FFMPEG_INCLUDE_PATH := $(realpath $(MY_APP_FFMPEG_OUTPUT_PATH)/include)
        # endif

        include $(call all-subdir-makefiles)

        diff --git a/android/ijkplayer/ijkplayer-example/build.gradle b/android/ijkplayer/ijkplayer-example/build.gradle
        index b06790d9..15393b66 100644
        --- a/android/ijkplayer/ijkplayer-example/build.gradle
        +++ b/android/ijkplayer/ijkplayer-example/build.gradle
        @@ -16,12 +16,23 @@ android {
                 targetSdkVersion rootProject.ext.targetSdkVersion
                 versionCode rootProject.ext.versionCode
                 versionName rootProject.ext.versionName
        +        //added by liuheng
        +        flavorDimensions "800800" //这串数字与工程下的build.gradle中的versionCode相同
        +        externalNativeBuild {
        +            ndkBuild {
        +                //moduleName "ijkplayer", "ijksdl"
        +                abiFilters "armeabi-v7a"
        +            }
        +        }
             }
             buildTypes {
                 release {
                     minifyEnabled false
                     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                 }
        +        debug {
        +            jniDebuggable true
        +        }
             }
             productFlavors {
                 all32 { minSdkVersion 9 }
        @@ -38,38 +49,52 @@ dependencies {
             compile 'com.android.support:appcompat-v7:23.0.1'
             compile 'com.android.support:preference-v7:23.0.1'
             compile 'com.android.support:support-annotations:23.0.1'
        -
             compile 'com.squareup:otto:1.3.8'
        -
             compile project(':ijkplayer-java')
             compile project(':ijkplayer-exo')
        +    //compile project(':ijkplayer-armv7a')
         
             // all32Compile project(':ijkplayer-armv5')
             all32Compile project(':ijkplayer-armv7a')
        +
             // all32Compile project(':ijkplayer-x86')
         
             // all64Compile project(':ijkplayer-armv5')
             all64Compile project(':ijkplayer-armv7a')
        +
             // all64Compile project(':ijkplayer-arm64')
        +
             // all64Compile project(':ijkplayer-x86')
        +
             // all64Compile project(':ijkplayer-x86_64')
         
             // compile 'tv.danmaku.ijk.media:ijkplayer-java:0.8.8'
        +
             // compile 'tv.danmaku.ijk.media:ijkplayer-exo:0.8.8'
         
             // all32Compile 'tv.danmaku.ijk.media:ijkplayer-armv5:0.8.8'
        +
             // all32Compile 'tv.danmaku.ijk.media:ijkplayer-armv7a:0.8.8'
        +
             // all32Compile 'tv.danmaku.ijk.media:ijkplayer-x86:0.8.8'
         
             // all64Compile 'tv.danmaku.ijk.media:ijkplayer-armv5:0.8.8'
        +
             // all64Compile 'tv.danmaku.ijk.media:ijkplayer-armv7a:0.8.8'
        +
             // all64Compile 'tv.danmaku.ijk.media:ijkplayer-arm64:0.8.8'
        +
             // all64Compile 'tv.danmaku.ijk.media:ijkplayer-x86:0.8.8'
        +
             // all64Compile 'tv.danmaku.ijk.media:ijkplayer-x86_64:0.8.8'
         
             // armv5Compile project(':player-armv5')
        +
             // armv7aCompile project(':player-armv7a')
        +
             // arm64Compile project(':player-arm64')
        +
             // x86Compile project(':player-x86')
        +
             // x86_64Compile project(':player-x86_64')
         }


        diff --git a/android/ijkplayer/settings.gradle b/android/ijkplayer/settings.gradle
        index 758234a..b51e5d3 100644
        --- a/android/ijkplayer/settings.gradle
        +++ b/android/ijkplayer/settings.gradle
        @@ -1,7 +1,7 @@
        -include ':ijkplayer-armv5', ':ijkplayer-x86_64'
        +//include ':ijkplayer-armv5', ':ijkplayer-x86_64'
         include ':ijkplayer-armv7a'
        -include ':ijkplayer-arm64'
        -include ':ijkplayer-x86'
        +//include ':ijkplayer-arm64'
        +//include ':ijkplayer-x86'
         
         include ':ijkplayer-java'
         include ':ijkplayer-exo'
        -- 

d)=====android/patches/0004-armv7a-link-prebuilt-staic-libraries-of-ffmepg.patch=====

        diff --git a/android/ijkplayer/ijkplayer-armv7a/src/main/jni/ffmpeg/Android.mk b/android/ijkplayer/ijkplayer-armv7a/src/main/jni/ffmpeg/Android.mk
        index ec33908..3c525bd 100644
        --- a/android/ijkplayer/ijkplayer-armv7a/src/main/jni/ffmpeg/Android.mk
        +++ b/android/ijkplayer/ijkplayer-armv7a/src/main/jni/ffmpeg/Android.mk
        @@ -1,6 +1,42 @@
         LOCAL_PATH := $(call my-dir)
        +include $(CLEAR_VARS)
        +LOCAL_MODULE    := avcodec
        +LOCAL_SRC_FILES := $(MY_APP_FFMPEG_OUTPUT_PATH)/lib/libavcodec.a
        +LOCAL_EXPORT_C_INCLUDES := $(MY_APP_FFMPEG_INCLUDE_PATH)
        +LOCAL_C_INCLUDES += $(MY_APP_FFMPEG_INCLUDE_PATH)
        +include $(PREBUILT_STATIC_LIBRARY)
        +
        +include $(CLEAR_VARS)
        +LOCAL_MODULE    := avformat
        +LOCAL_SRC_FILES := $(MY_APP_FFMPEG_OUTPUT_PATH)/lib/libavformat.a
        +LOCAL_EXPORT_C_INCLUDES := $(MY_APP_FFMPEG_INCLUDE_PATH)
        +LOCAL_C_INCLUDES += $(MY_APP_FFMPEG_INCLUDE_PATH)
        +include $(PREBUILT_STATIC_LIBRARY)
        +
        +include $(CLEAR_VARS)
        +LOCAL_MODULE    := swscale
        +LOCAL_SRC_FILES := $(MY_APP_FFMPEG_OUTPUT_PATH)/lib/libswscale.a
        +LOCAL_EXPORT_C_INCLUDES := $(MY_APP_FFMPEG_OUTPUT_PATH)/include
        +LOCAL_C_INCLUDES += $(MY_APP_FFMPEG_INCLUDE_PATH)
        +include $(PREBUILT_STATIC_LIBRARY)
        +
        +include $(CLEAR_VARS)
        +LOCAL_MODULE    := avutil
        +LOCAL_SRC_FILES := $(MY_APP_FFMPEG_OUTPUT_PATH)/lib/libavutil.a
        +LOCAL_EXPORT_C_INCLUDES := $(MY_APP_FFMPEG_INCLUDE_PATH)
        +LOCAL_C_INCLUDES += $(MY_APP_FFMPEG_INCLUDE_PATH)
        +include $(PREBUILT_STATIC_LIBRARY)
        +
        +include $(CLEAR_VARS)
        +LOCAL_MODULE    := avfilter
        +LOCAL_SRC_FILES := $(MY_APP_FFMPEG_OUTPUT_PATH)/lib/libavfilter.a
        +LOCAL_EXPORT_C_INCLUDES := $(MY_APP_FFMPEG_INCLUDE_PATH)
        +LOCAL_C_INCLUDES += $(MY_APP_FFMPEG_INCLUDE_PATH)
        +include $(PREBUILT_STATIC_LIBRARY)
         
         include $(CLEAR_VARS)
        -LOCAL_MODULE := ijkffmpeg
        -LOCAL_SRC_FILES := $(MY_APP_FFMPEG_OUTPUT_PATH)/libijkffmpeg.so
        -include $(PREBUILT_SHARED_LIBRARY)
        
        +LOCAL_MODULE    := swresample
        +LOCAL_SRC_FILES := $(MY_APP_FFMPEG_OUTPUT_PATH)/lib/libswresample.a
        +LOCAL_EXPORT_C_INCLUDES := $(MY_APP_FFMPEG_INCLUDE_PATH)
        +LOCAL_C_INCLUDES += $(MY_APP_FFMPEG_INCLUDE_PATH)
        +include $(PREBUILT_STATIC_LIBRARY)

        diff --git a/android/ijkplayer/ijkplayer-java/src/main/java/tv/danmaku/ijk/media/player/IjkMediaPlayer.java b/android/ijkplayer/ijkplayer-java/src/main/java/tv/danmaku/ijk/media/player/IjkMediaPlayer.java
        index 985f848..7ad3fe4 100755
        --- a/android/ijkplayer/ijkplayer-java/src/main/java/tv/danmaku/ijk/media/player/IjkMediaPlayer.java
        +++ b/android/ijkplayer/ijkplayer-java/src/main/java/tv/danmaku/ijk/media/player/IjkMediaPlayer.java
        @@ -165,7 +165,6 @@ public final class IjkMediaPlayer extends AbstractMediaPlayer {
                         if (libLoader == null)
                             libLoader = sLocalLibLoader;
         
        -                libLoader.loadLibrary("ijkffmpeg");
                         libLoader.loadLibrary("ijksdl");
                         libLoader.loadLibrary("ijkplayer");
                         mIsLibLoaded = true;

        diff --git a/ijkmedia/ijkplayer/Android.mk b/ijkmedia/ijkplayer/Android.mk
        index 92372ab..6c9270d 100644
        --- a/ijkmedia/ijkplayer/Android.mk
        +++ b/ijkmedia/ijkplayer/Android.mk
        @@ -26,7 +26,7 @@ ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)
         LOCAL_CFLAGS += -mfloat-abi=soft
         endif
         LOCAL_CFLAGS += -std=c99
        -LOCAL_LDLIBS += -llog -landroid
        +LOCAL_LDLIBS += -llog -landroid -lm -lz
         
         LOCAL_C_INCLUDES += $(LOCAL_PATH)
         LOCAL_C_INCLUDES += $(realpath $(LOCAL_PATH)/..)
        @@ -59,8 +59,8 @@ LOCAL_SRC_FILES  += ijkavformat/ijkurlhook.c
         LOCAL_SRC_FILES  += ijkavformat/ijklongurl.c
         LOCAL_SRC_FILES  += ijkavformat/ijksegment.c
         
        -LOCAL_SHARED_LIBRARIES := ijkffmpeg ijksdl
        -LOCAL_STATIC_LIBRARIES := android-ndk-profiler ijksoundtouch
        +LOCAL_SHARED_LIBRARIES := ijksdl
        +LOCAL_STATIC_LIBRARIES := avformat avcodec swscale swresample avfilter avutil android-ndk-profiler ijksoundtouch
         
         LOCAL_MODULE := ijkplayer
         include $(BUILD_SHARED_LIBRARY)

        diff --git a/ijkmedia/ijksdl/Android.mk b/ijkmedia/ijksdl/Android.mk
        index 08ebbab..4f6d804 100644
        --- a/ijkmedia/ijksdl/Android.mk
        +++ b/ijkmedia/ijksdl/Android.mk
        @@ -70,8 +70,8 @@ LOCAL_SRC_FILES += android/ijksdl_vout_android_nativewindow.c
         LOCAL_SRC_FILES += android/ijksdl_vout_android_surface.c
         LOCAL_SRC_FILES += android/ijksdl_vout_overlay_android_mediacodec.c
         
        -LOCAL_SHARED_LIBRARIES := ijkffmpeg 
        -LOCAL_STATIC_LIBRARIES := cpufeatures yuv_static ijkj4a
        +LOCAL_STATIC_LIBRARIES := avformat avcodec swscale swresample avfilter avutil cpufeatures yuv_static
        +LOCAL_SHARED_LIBRARIES := ijkj4a
         
         LOCAL_MODULE := ijksdl
         include $(BUILD_SHARED_LIBRARY)
        -- 

        //下面改动是防止c代码在编译的时候过度优化导致调试的时候看不到变量的值,将-O3的优化级别改为-O0
        diff --git a/android/ijkplayer/ijkplayer-armv7a/src/main/jni/Application.mk b/android/ijkplayer/ijkplayer-armv7a/src/main/jni/Application.mk
        index 3e16f98d..4e4ec08a 100644
        --- a/android/ijkplayer/ijkplayer-armv7a/src/main/jni/Application.mk
        +++ b/android/ijkplayer/ijkplayer-armv7a/src/main/jni/Application.mk
        @@ -24,8 +24,8 @@ NDK_TOOLCHAIN_VERSION=4.9
         APP_PIE := false
 
         APP_STL := stlport_static
        -
        -APP_CFLAGS := -O3 -Wall -pipe \
        +#-O3
        +APP_CFLAGS := -O0 -Wall -pipe \
             -ffast-math \
             -fstrict-aliasing -Werror=strict-aliasing \
             -Wno-psabi -Wa,--noexecstack \
  1. 将ijkplayer-android/android/ijkplayer/ijkplayer-armv7a/src/main/obj/local/armeabi-v7a路径添加到 Run -> Edit Configurations -> Debugger -> Symbol Directories中

  2. File -> Project Structure -> SDK Location -> Android NDK location中添加ndk路径,ndk要r12+版本以上(https://github.com/googlesamples/android-ndk/wiki)r14版本以下包括14(ijkplayer-ios/android/contrib/tools/do-detect-env.sh),版本非常重要!!!
    版本号的查看在${ANDROID_NDK}/RELEASE.TXT(r10e或者以下版本)或者${ANDROID_NDK}/source.properties(r10e以上版本),本人用的是r14b

  3. gradle版本为4.1,即在android/ijkplayer/gradle/wrapper/gradle-wrapper.properties中distributionUrl=https://services.gradle.org/distributions/gradle-4.1-all.zip

  4. (非必须,报错时需要修改)在android/ijkplayer/ijkplayer-example/src/main/res/xml/settings.xml中android:entries="@@array/pref_entries_player"改为android:entries="@array/pref_entries_player"


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值