如何在gradle中包含来自另一个模块的本机库

时间:2014-11-21 11:10:55

标签: android android-ndk gradle

我正在使用Gradle,我有一个模块(android库),里面有一些本机的东西,我最终设法正确构建,这个额外的任务:

task buildNative(type: Exec, description: 'Compile JNI sources with NDK') {
    def ndkDir = project.plugins.findPlugin('com.android.library').getNdkFolder();
    commandLine "$ndkDir\\ndk-build.cmd",
            '-C', file('src/main/jni').absolutePath, //Change directory to his before continuing
            '-j', Runtime.runtime.availableProcessors(), //Might make it faster
            'NDK_LIBS_OUT="', file('src/main/jniLibs').absolutePath, '"', //Libs path
            'NDK_OUT="', file('build/ndkcustom/obj').absolutePath, '"' //Obj path
}

现在,该模块包含一个示例应用程序,当我运行它时(从.library更改为.application进行测试)它运行良好,所以一切都按照它应该包含。但是,我不想使用该示例应用程序,但希望在另一个模块中使用该模块中的natives / java类,这将是我的主要应用程序。只是包含该模块,我可以访问java类,但不能访问本机,所以当我运行时:

static {
    System.loadLibrary("native-activity");
}

它提供了一个错误,它无法加载库。如何包含本机库?

修改

我的项目gradle文件不包含任何特殊内容。

库的build.gradle:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.1"

    defaultConfig {
        applicationId "com.example.mobilesdkharness"
        minSdkVersion 14
        targetSdkVersion 15
        versionCode 1
        versionName "1.0"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    sourceSets {
        main {
            jni.srcDirs = [] //Disable automatic jni building
        }
    }

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

//Note: Using ndk r10c - might brake with future releases
task buildNative(type: Exec, description: 'Compile JNI sources with NDK') {
    def ndkDir = project.plugins.findPlugin('com.android.library').getNdkFolder();
    commandLine "$ndkDir\\ndk-build.cmd",
            '-C', file('src/main/jni').absolutePath, //Change directory to his before continuing
            '-j', Runtime.runtime.availableProcessors(), //Might make it faster
            'NDK_LIBS_OUT="', file('src/main/jniLibs').absolutePath, '"', //Libs path
            'NDK_OUT="', file('build/ndkcustom/obj').absolutePath, '"' //Obj path
}

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn buildNative
}

app的build.gradle(只包含include)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.1"

    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 15
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

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

dependencies {
    compile project(':mysdk')
}

0 个答案:

没有答案