android ijkplayer local compilation and debugging on Mac

Posted by TheAngst on Wed, 05 Jan 2022 09:20:15 +0100

1. Local compilation

Set the environment variable, ndk to use version r10e

ANDROID_SDK
ANDROID_NDK

System Preferences - > Security and privacy - > General - > app s that can be downloaded from the following location are set to any source
Setting method: after the Terminal terminal is opened, enter at the command prompt

sudo spctl --master-disable
Then follow the official prompts  

2. Local commissioning

The ijkplayer-armv7a project is associated with Android MK compile script

Modify the file: ijkplayer / ijkplayer-armv7a / build gradle
Modification: do not specify the location of jni's lib Library folder

android {
    //Delete the following
    //sourceSets.main {
    //  jniLibs.srcDirs 'src/main/libs'
    //  jni.srcDirs = [] // This prevents the auto generation of Android.mk
    //}
}

Modified content: add native code editor translation script Android File path of MK

android {
    //Add the following
    externalNativeBuild {
        ndkBuild {
            path "src/main/jni/Android.mk"
        }
    }
}

Modified content: set the parameters of the compiled script

android {
    defaultConfig {
        //Add the following
        externalNativeBuild {
            ndkBuild {
                arguments "NDK_APPLICATION:=src/main/jni/Application.mk"
                abiFilters "armeabi-v7a"
            }
        }
    }
}

Modification: open the debug mode of the project

android {
    buildTypes {
        //Add the following
        debug {
            debuggable true
            jniDebuggable true
            ndk {
                debuggable true
            }
        }
    }
}

Modify the compilation script of ffmpeg

Modified file: ijkplayer / ijkplayer-armv7a / SRC / main / JNI / ffmpeg / Android mk
Modification: change the link mode of ffmpeg from dynamic library mode to static library mode

LOCAL_PATH := $(call my-dir)

#Delete ffmpeg dynamic library
#include $(CLEAR_VARS)
#LOCAL_MODULE := ijkffmpeg
#LOCAL_SRC_FILES := #$(MY_APP_FFMPEG_OUTPUT_PATH)/libijkffmpeg.so
#Include $(PREBUILT_SHARED_LIBRARY)

#Add ffmpeg static library as follows
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)

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)

Modify the compilation script of ijkplayer

Modified file: ijkplayer / ijkmedia / ijkplayer / Android mk
Modification: add two local libraries -lm-lz

LOCAL_CFLAGS += -std=c99
#LOCAL_LDLIBS += -llog -landroid
LOCAL_LDLIBS += -llog -landroid -lm -lz

Modification: change ffmpeg from dynamic link mode to static link mode

#LOCAL_SHARED_LIBRARIES := ijkffmpeg ijksdl
#LOCAL_STATIC_LIBRARIES := android-ndk-profiler ijksoundtouch
LOCAL_SHARED_LIBRARIES := ijksdl
LOCAL_STATIC_LIBRARIES := android-ndk-profiler ijksoundtouch avformat avcodec swscale swresample avfilter avutil

Modify ijksdl compilation script

Modified file: ijkplayer / ijkmedia / ijksdl / Android mk
Modification: change ffmpeg from dynamic link mode to static link mode

#LOCAL_SHARED_LIBRARIES := ijkffmpeg
#LOCAL_STATIC_LIBRARIES := cpufeatures yuv_static ijkj4a
LOCAL_STATIC_LIBRARIES := cpufeatures yuv_static ijkj4a avformat avcodec swscale swresample avfilter avutil

Shield the loading of ffmpeg dynamic library in java layer

Modify the file: ijkplayer / ijkplayer Java / SRC / main / Java / TV / danmaku / ijk / media / player / ijkmediaplayer java
Modified content: because ffmpeg adopts static library import, there is no need to load the dynamic library of ffmpeg.

public static void loadLibrariesOnce(IjkLibLoader libLoader) {
    synchronized (IjkMediaPlayer.class) {
        if (!mIsLibLoaded) {
            if (libLoader == null)
                libLoader = sLocalLibLoader;
            //The loading of ijkffmeg library is masked here
            //libLoader.loadLibrary("ijkffmpeg");
            libLoader.loadLibrary("ijksdl");
            libLoader.loadLibrary("ijkplayer");
            mIsLibLoaded = true;
        }
    }
}

example project synchronous modification

Modify the file: ijkplayer / ijkplayer example / build gradle
Modification: specify the cpu type as armv7a and enable the debug mode

android {
    //Specify armeabi-v7a
    defaultConfig {
        externalNativeBuild {
            ndkBuild {
                abiFilters "armeabi-v7a"
            }
        }
    }
    buildTypes {
        //Add the specification of debug mode
        debug {
            jniDebuggable true
        }
    }
}

Modified content: delete the dependencies of non armeabi-v7a projects

dependencies {
    //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')
}

ndk Library Association

Modify the file: ijkplayer / local properties
Modification: ijkplayer needs to rely on ndk for compilation, so we need to specify the local ndk directory address, and ensure that the ndk version must be between r12 and r14. Never use the ndk in the sdk.

ndk.dir=<Yours ndk route>

Turn off compilation optimization for debugging

Modified file: ijkplayer / ijkplayer-armv7a / SRC / main / JNI / application mk
Modification: c code may be over optimized during compilation, resulting in no variable value during debugging. We can change the optimization level of - O3 to - O0

APP_CFLAGS := 
    #Reduce the optimization level of O3 to O0
    #-O3 -Wall -pipe \
     -O0 -Wall -pipe \
    -ffast-math \
    -fstrict-aliasing -Werror=strict-aliasing \
    -Wno-psabi -Wa,--noexecstack \
    -DANDROID -DNDEBUG

Possible errors

If this error is found

All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html

In ijkplayer / ijkplayer example / build Add in gradle

android {
  defaultConfig {
    #This value needs to be the same as ijkplayer / build The versionCode in gradle is the same
    flavorDimensions "800800"
  }
}

The above configuration is completed, but breakpoint debugging is not available yet
 

Run -> Edit Configurations -> Debugger -> Symbol Directories
#     Add "ijkplayer-armv7a/" to Symbol Directories

 

Problems I encountered:
Using a higher version of gradle, c + + code can click trace, but debug can't hang up
With gradle version 4.6, c + + code can't click trace, but it can be debug ged
I tried many times and solved the problem with the 5.4.1-bin version
 

If there is no lib directory in apk when packaging
 

 

 

Topics: Java Android Apache