无法使用gradle-experimental构建预构建的静态库

时间:2016-04-05 12:19:41

标签: android-studio android-ndk android-gradle

我的项目依赖于ffmpeg。我将放置一些build.gradle文件:

  apply plugin: 'com.android.model.application'

    model{
    // defining libavcodec library
    repositories {
        libs(PrebuiltLibraries) {
            avcodec {
                headers.srcDir "src/main/jni/include/ffmpeg"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("src/main/jni/lib/android/libavcodec.a")
                }
            }
        }
    }

    // defining libavformat library
    repositories {
        libs(PrebuiltLibraries) {
            avformat {
                headers.srcDir "src/main/jni/include/ffmpeg"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("src/main/jni/lib/android/libavformat.a")
                }
            }
        }
    }

    // defining libavutil library
    repositories {
        libs(PrebuiltLibraries) {
            avutil {
                headers.srcDir "src/main/jni/include/ffmpeg"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("src/main/jni/lib/android/libavutil.a")
                }
            }
        }
    }

    // defining libx264 library
    repositories {
        libs(PrebuiltLibraries) {
            x264 {
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("src/main/jni/lib/android/libx264.a")
                }
            }
        }
    }

    // defining libssl library
    repositories {
        libs(PrebuiltLibraries) {
            ssl {
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("src/main/jni/lib/android/libssl.a")
                }
            }
        }
    }

    // defining libcrypto library
    repositories {
        libs(PrebuiltLibraries) {
            crypto {
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("src/main/jni/lib/android/libcrypto.a")
                }
            }
        }
    }

    android {
        compileSdkVersion = 23
        buildToolsVersion = "23.0.2"

        defaultConfig.with {
            applicationId = "pro.net.sa"
            minSdkVersion.apiLevel = 14
            targetSdkVersion.apiLevel = 23
            versionCode = 1
            versionName = "1.0"

        }
    }

    android.ndk {

        moduleName = "mymodule"

        toolchain = "clang"

        toolchainVersion = "3.8"

        cppFlags.add("-std=c++11")


        cppFlags.add("-fexceptions")
        cppFlags.add("-frtti")
        cppFlags.add("-Wno-deprecated-declarations")
        cppFlags.add("-Wno-deprecated-register")
        cppFlags.add("-D ASIO_STANDALONE")

        CFlags.add("-I" + file("src/main/jni/androidplayer").absolutePath)
        .........

        ldLibs.addAll(["log" , "android", "z","OpenMAXAL", "dl"])

        stl = "gnustl_shared"
        debuggable = true
        platformVersion = 14
    }

    android.lintOptions{
        ignoreWarnings = true
    }
    android.productFlavors {
         create("arm7") {
            ndk.abiFilters.add("armeabi-v7a")
        }           
    }

    android.sources{
        main{
            jni{
                dependencies{
                    library "avcodec" linkage "static"
                    library "avformat" linkage "static"
                    library "avutil" linkage "static"
                    library "x264" linkage "static"
                    library "ssl" linkage "static"
                    library "crypto" linkage "static"
                }
            }
        }
    }
    android.buildTypes {
        release {
            minifyEnabled = false
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'
    compile files('src/main/libs/Logger.jar')
    compile files('src/main/libs/universal-image-loader-1.9.4.jar')
    compile files('src/main/libs/androidLogger.jar')
}

我一直在低于错误:

Error:error: undefined reference to 'avcodec_pix_fmt_to_codec_tag'
Error:error: undefined reference to 'avpriv_find_pix_fmt'
Error:error: undefined reference to 'ff_raw_pix_fmt_tags'
Error:error: linker command failed with exit code 1 (use -v to see invocation) 

我认为我所做的一切都是正确的,但却无法理解为什么错误会不断发生。有人可以提供帮助吗?我使用的是Android Studio 1.5.1,gradle 2.10,gradle-experimental 0.7.0-alpha5

编辑:我在cppFlags:

之后添加了下一行
        ldFlags.add("${file("src/main/jni/lib/android/libavcodec.a")}".toString())
        ldFlags.add("${file("src/main/jni/lib/android/libavformat.a")}".toString())
        ldFlags.add("${file("src/main/jni/lib/android/libavutil.a")}".toString())
        ldFlags.add("${file("src/main/jni/lib/android/libx264.a")}".toString())
        ldFlags.add("${file("src/main/jni/lib/android/libssl.a")}".toString())
        ldFlags.add("${file("src/main/jni/lib/android/libcrypto.a")}".toString())

然后我收到了下一个错误:

/home/user/android-ndk-r11b/sources/cxx-stl/gnu-libstdc++/4.9/include/bits/atomic_base.h
Error:(411, 16) error: cannot compile this atomic library call yet
Error:(411, 16) error: cannot compile this atomic library call yet
Error:(411, 16) error: cannot compile this atomic library call yet

1 个答案:

答案 0 :(得分:3)

你不应该为不同的abi使用不同的静态库吗?

ldFlags.add("${file("src/main/jni/lib/android/$abi/libavcodec.a")}".toString())

而不是

ldFlags.add("${file("src/main/jni/lib/android/libavcodec.a")}".toString())

Here是如何做这样的事情的例子。