【问题标题】:Loading shared libraries with Gradle 2.2.1使用 Gradle 2.2.1 加载共享库
【发布时间】:2015-02-10 14:26:28
【问题描述】:

我正在尝试在我的 Android 应用程序中使用共享库 libavcodec-56.so,但我找不到方法。我正在使用 Gradle 2.2.1 和 Android Studio 1.0。到目前为止,我所做的如下:

-我使用 NDK 工具链从源代码构建 libavcodec-56.so。

-我将 libavcodec-56.so 复制到 src/main/jniLibs/armeabi 中

-我能够在项目中创建一个 .c 文件并使用 private native void nameOfTheNativeMethod() 与 java 文件进行通信

-我可以加载一些外部库,例如ldLibs "m"

但是,如果我尝试使用 #include <libavcodec/avcodec.h> 我会收到错误消息

No such file or directory
     #include <libavcodec/avcodec.h>

我的 gradle 文件如下所示:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
        ndk {
            moduleName "ffmpeg"
            cFlags "-std=c99"   
            ldLibs "log",  "m" 
        }
    }

    sourceSets.main {
        jni.srcDirs = ["src/main/jni"];
        //jniLibs.srcDirs = ['src/main/jniLibs'];
    }


    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'

}

有什么想法吗?

提前致谢

【问题讨论】:

    标签: android-ndk ffmpeg android-gradle-plugin


    【解决方案1】:

    当前对 gradle android 插件的 NDK 支持不完整,现已弃用。本地本地库之间不能有本地依赖关系。

    目前使用 gradle 的唯一解决方案是直接使用 ndk-build 和你自己的 Makefile 来生成你所有的库,用这样的 build.gradle 文件:

    import org.apache.tools.ant.taskdefs.condition.Os
    
    apply plugin: 'com.android.library'
    
    android {
        compileSdkVersion 21
        buildToolsVersion "21.1.2"
    
        defaultConfig {
            minSdkVersion 15
            targetSdkVersion 21
            versionCode 1
            versionName "1.0"
        }
    
        sourceSets.main {
            jni.srcDirs = [] //disable automatic ndk-build call
            jniLibs.srcDir 'src/main/libs' //integrate your libs from libs instead of jniLibs
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    
        // call regular ndk-build(.cmd) script from app directory
        task ndkBuild(type: Exec) {
            if (Os.isFamily(Os.FAMILY_WINDOWS)) {
                commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
            } else {
                commandLine 'ndk-build', '-C', file('src/main').absolutePath
            }
        }
    
        tasks.withType(JavaCompile) {
            compileTask -> compileTask.dependsOn ndkBuild
        }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:21.0.3'
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-10
      • 1970-01-01
      • 2014-03-04
      • 2019-04-17
      • 1970-01-01
      • 2013-08-05
      • 2015-03-02
      相关资源
      最近更新 更多