在Android Studio中包含外部C ++库和本机NDK支持

时间:2015-09-12 13:06:53

标签: android android-studio gradle android-ndk

我正在尝试利用Android Studio中的(实验性)本机NDK支持。我是Gradle的新手。我试图构建的项目使用外部库,在编译项目时将其编译为静态库,然后在其中使用。我怎么能这样做?

详细说明 - 默认src/main/jni中有C ++代码,它可以正确编译,并且可以在jni文件夹下的Android Studio中正确显示。该项目使用外部C ++静态库 - 一些是编译的,一些不是。我需要正确构建项目和库,并尽可能使代码可见。

首先,有没有办法可以使用Android.mk文件?它们已经可以从以前的方案中导入库中,但现在它们似乎被忽略了。

我已尝试在srcDirs中使用android.source添加库'路径,它可以解决某些问题,但奇怪的是,编译器中找不到某些路径,编译停止。这是代码:

    android.sources {
        main {
            jni {
                source {
                    srcDirs += ["/../../../libPdf/"]

//                  aes library
                    srcDirs += ["/../../../libs/aes/include", "/../../../libs/aes"]

//                  jpeg library
                    srcDirs += ["/../../../libPdf/platform/Android", "/../../../libs/jpeg/jpeg-9"]
                }
            }
        }
    }

我还尝试将这些库添加为单独的模块,然后在项目build.gradle中编译它们中的每一个(我在此示例中看到了这一个:https://github.com/googlesamples/android-ndk/tree/master/hello-thirdparty)但我得到了这个即使我为库创建build.gradle文件也会出错:

  

错误:配置名称'默认'没找到。

我不知道我是否需要一个.iml文件库,我没有,我不知道如何创建一个.iml文件。我试图改变现有的一个把它放在库中,但发生了同样的错误。 以下是项目的build.gradle:

中的代码
apply plugin: 'com.android.model.library'
apply plugin: 'maven'

def jpeg_sdk_path = file(project(':jpeg').projectDir).absolutePath + "/jpeg-9"

model {

    android.buildTypes {
        release {
            consumerProguardFiles += file('proguard-project.txt')
        }
    }


    android.ndk {
        moduleName = "native"

        cppFlags += "-I${jpeg_sdk_path}".toString()
        ldLibs += ["jpeg"]

    }

    android {
        compileSdkVersion = 23
        buildToolsVersion = "21.1.2"

        defaultConfig.with {
            minSdkVersion.apiLevel = 14
            targetSdkVersion.apiLevel = 23
        }


        lintOptions.with {
            disable 'MissingTranslation'
        }

        //copy resources and stuff tasks

    }


}
dependencies {
    compile 'com.android.support:appcompat-v7:23.0.0'
    compile files('libs/jsoup-1.7.3.jar')
    compile project(':jpeg')
}

这是settings.gradle:

include ':libPDF'
include ':PDFViewer'
include ':jpeg'
project(':jpeg').projectDir = new File(settingsDir, '/../../../libs/jpeg')

还有更多的库要包含但是我现在只想导入一个 - jpeg,看它是否正常工作。我正在尝试制作本地模块,并以某种方式将它们从gradle连接到其他地方的代码。我仍然得到同样的错误 - 似乎找不到build.gradle文件。

有什么建议吗?提前谢谢。

修改 我设法将整个事情编译好但现在我得到了链接器错误。这是项目的build.gradle(缩短版本):

apply plugin: 'com.android.model.library'
apply plugin: 'maven'

def jpeg_sdk_path = file(project(':jpeg').projectDir).absolutePath + "/../../../../../../../libs/jpeg/jpeg-9"
//other defs of paths

model {

android.buildTypes {
    release {
        minifyEnabled = false
        consumerProguardFiles += file('proguard-project.txt')
    }
    debug {
        minifyEnabled = false
        consumerProguardFiles += file('proguard-project.txt')
    }
}

android.sources {
    main {
        jni {
            source {
                srcDirs += ["src/main/jni/"]
                //lcms2
                srcDirs += [lcms2_out_path + "/src"]


                //freetype library
                include aes_path + "/aes_modes.c"
                include aes_path + "/aescrypt.c"
                include aes_path + "/aeskey.c"
                include aes_path + "/aestab.c"

 //                  jbig2 library
                srcDirs += [jbig2_path + "/src/jbig2", jbig2_path + "/include"]

//                  sfntly library
                include sfntly_path + "/sample/chromium/subsetter_impl.cc"
                //many other includes here

//                  jpeg library
                srcDirs += [ jpeg_main_path + "/../../../../../../../libPdf/platform/Android"]

                include jpeg_sdk_path + "/jaricom.c"
                include jpeg_sdk_path + "/jcapimin.c"
                //many other includes here

                //native libpdf library
                include libpdf_path+ "/PdfCatalog.cpp"
                //many other includes here

                //openssl library
                srcDirs += [openssl_path + "/lib/Android/armeabi-v7a/include"]
            }
        }

        jniLibs{
            source{
                srcDirs += ['/src/main/libs']
                srcDirs += [freetype_path + '/platform/Android/obj/local']
                srcDirs += [libpdf_path]
            }
        }

        resources.source.srcDirs += ['src/main/res']
    }


}

android.ndk {
    moduleName = "native"

    CFlags += "-I${openjpeg_path}/prebuilt/armeabi-v7a/include".toString() //figure out how to include for other flavors
    CFlags += "-I${jpeg_main_path}/../../../../../../../libPdf/platform/Android".toString()
    CFlags += "-I${jpeg_sdk_path}".toString()
    //other CFlags here
    CFlags += "-DSFNTLY_NO_EXCEPTION"
    CFlags += "-DU_STATIC_IMPLEMENTATION"
    cppFlags += CFlags

    abiFilters += "armeabi-v7a"
    ldFlags += "-L${freetype_src_path }/../platform/Android/obj/local/armeabi-v7a".toString()
    ldLibs += ["log", "android", "EGL", "GLESv1_CM", "ft2"]
    //other ldFlags and ldLibs - for the static .a libraries only
    stl = "gnustl_static"

}

compileOptions.with {
    sourceCompatibility=JavaVersion.VERSION_1_7
    targetCompatibility=JavaVersion.VERSION_1_7
}

android {
    compileSdkVersion = 23
    buildToolsVersion = "21.1.2"

    defaultConfig.with {
        minSdkVersion.apiLevel = 14
        targetSdkVersion.apiLevel = 23
    }

    lintOptions.with {
        disable 'MissingTranslation'
        abortOnError = false
    }

    //tasks for moving resources and stuff here
}
}
dependencies {
compile 'com.android.support:appcompat-v7:23.0.0'
compile files('libs/jsoup-1.7.3.jar')
compile project(':jpeg')
compile project(':freetype')
compile project(':lcms2')
compile project(':jbig2')
compile project(':aes')
compile project(':openjpeg')
compile project(':sfntly')
}

现在我收到很多错误:

  

错误:(737,-1)Gradle:未定义引用' AndroidBitmap_getInfo'

我想我知道为什么会这样 - 我应该使用其他东西而不是include来将单个文件添加到源集。

我现在的问题是:如何将单个文件添加到源集中,以便编译器和链接器稍后使用它们?

0 个答案:

没有答案