diff --git a/modules/mogo-module-common/CMakeLists.txt b/modules/mogo-module-common/CMakeLists.txt index e66e013af7..33493ce303 100644 --- a/modules/mogo-module-common/CMakeLists.txt +++ b/modules/mogo-module-common/CMakeLists.txt @@ -1,44 +1,33 @@ -# 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. +# 更多关于在Android Studio中使用CMake的信息,请阅读文档 +# https://d.android.com/studio/projects/add-native-code.html +# 设置构建本机库所需的CMake的最小版本。 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. +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -frtti -fexceptions") -add_library( # Sets the name of the library. - method-hook-lib +# 指定的源文件生成链接文件,然后添加到工程中去 +add_library( + # 设置库的名称。对应 System.loadLibrary("native-lib"); 使用的名称 + method-hook-lib + # 将库设置为共享库。 + SHARED + # 提供了到源文件的相对路径。 + src/main/cpp/method-hook-lib.cpp) - # Sets the library as a shared library. - SHARED +# 搜索指定的预构建库并将路径存储为变量。 +# 因为CMake在搜索路径中默认包含系统库, +# 所以你只需要指定你想要添加的公共NDK库的名称, +# CMake在完成它的构建之前会验证这个库是否存在。 +find_library( + # 设置path变量的名称。 + log-lib + # 指定你想要CMake定位的NDK库的名称。 + log) - # Provides a relative path to your source file(s). - src/main/cpp/method-hook-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. - method-hook-lib - - # Links the target library to the log library - # included in the NDK. - ${log-lib} ) \ No newline at end of file +# 为native-lib添加ffmpeg编译连接库 +target_link_libraries( + # 指定目标库。 + method-hook-lib + # 将目标库链接到包含在NDK中的日志库。 + ${log-lib}) \ No newline at end of file diff --git a/modules/mogo-module-common/build.gradle b/modules/mogo-module-common/build.gradle index 783a180702..23ea152ed1 100644 --- a/modules/mogo-module-common/build.gradle +++ b/modules/mogo-module-common/build.gradle @@ -14,7 +14,7 @@ android { externalNativeBuild { cmake { - cppFlags "-std=c++11 -frtti -fexceptions" + cppFlags "" } } } diff --git a/modules/mogo-module-common/src/main/cpp/Api.h b/modules/mogo-module-common/src/main/cpp/Api.h new file mode 100644 index 0000000000..e61958e771 --- /dev/null +++ b/modules/mogo-module-common/src/main/cpp/Api.h @@ -0,0 +1,16 @@ +// +// Created by on 2020-05-08. +// + +#ifndef API_H +#define API_H + +#include + +#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,"Native_RtmpPusher",__VA_ARGS__) +#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,"Native_RtmpPusher",__VA_ARGS__) +#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,"Native_RtmpPusher",__VA_ARGS__) +#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,"Native_RtmpPusher",__VA_ARGS__) + + +#endif //API_H diff --git a/modules/mogo-module-common/src/main/cpp/method-hook-lib.cpp b/modules/mogo-module-common/src/main/cpp/method-hook-lib.cpp index dd5111ea23..477a2c1a3f 100644 --- a/modules/mogo-module-common/src/main/cpp/method-hook-lib.cpp +++ b/modules/mogo-module-common/src/main/cpp/method-hook-lib.cpp @@ -1,7 +1,7 @@ #include #include +#include "Api.h" -// // Created by donghongyu on 12/10/20 1:34 PM. // 源码地址 https://github.com/pqpo/methodhook // 方法hook jni类 @@ -22,7 +22,10 @@ static struct { * @param destMethodObj 替换的方法对象 * @return */ -static jlong methodHook(JNIEnv *env, jclass type, jobject srcMethodObj, jobject destMethodObj) { +static jlong methodHook(JNIEnv *env, + jclass type, + jobject srcMethodObj, + jobject destMethodObj) { // 反射:获取了Java对象的在native层的 ArtMethod指针 void *srcMethod = reinterpret_cast(env->FromReflectedMethod(srcMethodObj)); void *destMethod = reinterpret_cast(env->FromReflectedMethod(destMethodObj)); @@ -45,7 +48,10 @@ static jlong methodHook(JNIEnv *env, jclass type, jobject srcMethodObj, jobject * @param methodPtr * @return */ -static jobject methodRestore(JNIEnv *env, jclass type, jobject srcMethod, jlong methodPtr) { +static jobject methodRestore(JNIEnv *env, + jclass type, + jobject srcMethod, + jlong methodPtr) { // 要还原的旧方法 指针 int *backupMethod = reinterpret_cast(methodPtr); // 获取了Java对象的在native层的 ArtMethod指针