android-ndk - android - ndk 不将预构建库复制到我的 apk

标签 android-ndk android-gradle-plugin

我可以构建 hello-libs google 示例代码,并且预构建库已正确复制到目标 apk。

但是在我的应用程序中,编译已通过但 apk 不包含预构建库。

两个问题:

  • 构建输出apk lib目录只包含arm64版本,但是我在build.gradle中定义了armeabi、armeabi-v7a和arm64。不知道为什么它不生效。
  • lib/ └── arm64-v8a └── libFFmpegWrapper.so
  • 预构建库根本没有包含在 apk lib 目录中,但是我的 build.gradle 中有 jniLibs 行,我检查了 hell-libs,它看起来是一样的。

  • 详细信息如下:

    安卓工作室版本:3.0.1
    操作系统:macosx 10.13.1

    我的文件目录结构如下:
    ├── app
    │   ├── libs
    │   └── src
    │       ├── main
    │       │   ├── cpp
    │       │   │   └── ffmpeg
    │       │   │       ├── arm64-v8a
    │       │   │       │   ├── bin
    │       │   │       │   ├── include
    │       │   │       │   ├── lib
    │       │   │       ├── armeabi
    │       │   │       │   ├── include
    │       │   │       │   ├── lib
    │       │   │       └── armeabi-v7a
    │       │   │           ├── include
    │       │   │           ├── lib
    │       │   ├── java
    │       │   │   └── com
    │       │   │       └── example
    │       │   │           └── ffmpegtest
    │       │   │               └── recorder
    

    构建.gradle
    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 26
        defaultConfig {
            applicationId "com.example.ffmpegtest"
            minSdkVersion 23
            targetSdkVersion 26
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            ndk {
                abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
            }
            externalNativeBuild {
                cmake {
                    cmake {
                        arguments '-DANDROID_PLATFORM=android-26',
                                '-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=gnustl_static'
                    }
                }
            }
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        sourceSets {
            main {
                // let gradle pack the shared library into apk
                jniLibs.srcDirs = ['src/main/cpp/ffmpeg']
            }
        }
        externalNativeBuild {
            cmake {
                path "CMakeLists.txt"
            }
        }
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:26.1.0'
        implementation 'com.android.support.constraint:constraint-layout:1.0.2'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.1'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    }
    

    CMakeList.txt
    cmake_minimum_required(VERSION 3.4.1)
    
    set(FFMPEG_DIR ${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg)
    
    add_library(avutil-55 SHARED IMPORTED)
    set_target_properties(avutil-55
        PROPERTIES IMPORTED_LOCATION
        ${FFMPEG_DIR}/${ANDROID_ABI}/lib/libavutil-55.so)
    
    add_library(avformat-57 SHARED IMPORTED)
    set_target_properties(avformat-57
        PROPERTIES IMPORTED_LOCATION
        ${FFMPEG_DIR}/${ANDROID_ABI}/lib/libavformat-57.so)
    
    add_library(avcodec-57 SHARED IMPORTED)
    set_target_properties(avcodec-57
        PROPERTIES IMPORTED_LOCATION
        ${FFMPEG_DIR}/${ANDROID_ABI}/lib/libavcodec-57.so)
    
    add_library(avfilter-6 SHARED IMPORTED)
    set_target_properties(avfilter-6
        PROPERTIES IMPORTED_LOCATION
        ${FFMPEG_DIR}/${ANDROID_ABI}/lib/libavfilter-6.so)
    
    add_library(swresample-2 SHARED IMPORTED)
    set_target_properties(swresample-2
        PROPERTIES IMPORTED_LOCATION
        ${FFMPEG_DIR}/${ANDROID_ABI}/lib/libswresample-2.so)
    
    add_library(swscale-4 SHARED IMPORTED)
    set_target_properties(swscale-4
        PROPERTIES IMPORTED_LOCATION
        ${FFMPEG_DIR}/${ANDROID_ABI}/lib/libswscale-4.so)
    
    set(FFMPEG_LIBS
        avutil-55
        avformat-57
        avcodec-57
        avfilter-6
        swresample-2
        swscale-4)
    
    include_directories(${FFMPEG_DIR}/${ANDROID_ABI}/include)
    
    set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -mfloat-abi=softfp -mfpu=neon -g -O0")
    
    add_library( # Sets the name of the library.
                 FFmpegWrapper
    
                 # Sets the library as a shared library.
                 SHARED
    
                 # Provides a relative path to your source file(s).
                 src/main/cpp/FFmpegWrapper.c
                 )
    
    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
                  )
    
    target_link_libraries( # Specifies the target library.
    
                           FFmpegWrapper
    
                           # Links the target library to the log library
                           # included in the NDK.
                           ${log-lib}
    
                            # ffmpeg library
                            ${FFMPEG_LIBS}
                           )
    

    最佳答案

    共享库目录结构应更改如下:

    src/main/cpp/
    └── ffmpeg
        ├── include
        └── lib
            ├── arm64-v8a
            ├── armeabi
            └── armeabi-v7a
    

    并将另一行代码更改为正确的位置。 CMakeList.txt 可能如下所示:
    cmake_minimum_required(VERSION 3.4.1)
    
    set(FFMPEG_DIR ${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg)
    
    # library directory structure
    # src/main/cpp/
    # └── ffmpeg
    #     ├── include
    #     └── lib
    #         ├── arm64-v8a
    #         ├── armeabi
    #         └── armeabi-v7a
    include_directories(${FFMPEG_DIR}/include)
    set(FFMPEG_LIBS
        avutil-55
        avformat-57
        avcodec-57
        avfilter-6
        swresample-2
        swscale-4)
    
    foreach(libname ${FFMPEG_LIBS})
        message(add lib ${libname})
        add_library(${libname} SHARED IMPORTED)
        set_target_properties(${libname} PROPERTIES IMPORTED_LOCATION
            ${FFMPEG_DIR}/lib/${ANDROID_ABI}/lib${libname}.so)
    endforeach()
    
    set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -mfloat-abi=softfp -mfpu=neon -g -O0")
    add_library(FFmpegWrapper SHARED src/main/cpp/FFmpegWrapper.c)
    find_library(log-lib log)
    target_link_libraries(FFmpegWrapper ${log-lib} ${FFMPEG_LIBS})
    

    build.gradle:
    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 26
        defaultConfig {
            ...
            ndk {
                abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a'
            }
            ...
        }
        ...
        sourceSets {
            main {
                // let gradle pack the shared library into apk
                jniLibs.srcDirs = ['src/main/cpp/ffmpeg/lib']
            }
        }
        externalNativeBuild {
            cmake {
                path "CMakeLists.txt"
            }
        }
    }
    
    dependencies {
       ...
    }
    

    关于android-ndk - android - ndk 不将预构建库复制到我的 apk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47687711/

    相关文章:

    gradle - 使用实现项目时,可传递引用不起作用

    android - 在 xml 中设置变量,就像在 android 中为 adMob 设置 list 占位符一样

    android - Android SDK Manager 下载器不能恢复支持吗?

    android - NDK编译多个库

    android - 无法启动 Android 虚拟 AVD 设备

    android - 如何使用 ARM NEON 优化循环 4D 矩阵 vector 乘法?

    android - 如何删除这些不需要的权限

    android - 在没有 Gradle 的情况下使用 multiDexEnabled,而是使用 Eclipse 构建过程

    android - 第三方库证书未添加到 apk

    android - linux下为android搭建openh264