在Android ndk中集成POCO库

时间:2016-05-30 08:12:48

标签: android c++ android-studio android-ndk poco-libraries

我正在尝试开发使用POCO socket库和android NDK的跨平台应用(在本机级别)。但我不知道如何在工作室中使用gradle在POCO library中集成/使用android NDK

我正在使用com.android.tools.build:gradle-experimental:0.7.0 gradle版本。 以下是我的build.gradle

apply plugin: 'com.android.model.application'
model {
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.2"

        defaultConfig {
            applicationId "com.ndkproto"
            minSdkVersion.apiLevel = 22
            targetSdkVersion.apiLevel = 23
            versionCode = 1
            versionName = "1.0"
        }
        buildTypes {
            release {
                minifyEnabled = false
                proguardFiles.add(file('proguard-android.txt'))
            }
        }

        ndk {
            moduleName = "ndk-proto-jni"
            stl "stlport_static"
            ldLibs.addAll(['android', 'log'])
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'
}

我已下载POCO library但不知道在build.gradle文件中要更改什么以在ndk中使用其C ++函数。我试图通过网络搜捕但找不到与之相关的任何材料或官方指南。

1 个答案:

答案 0 :(得分:0)

好吧,我没有尝试过使用POCO库,但我成功地使用了非增强asio库。 看,我做了什么。

将asio文件放入目录,如下所示

java
jni
prebuilt
    asio
        1.10.6
            asio_files_and_directories
    openssl
        armeabi
            include
            lib
        x86
            include
            lib

然后我使用了gradle-experimental plugin并写了build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle-experimental:0.7.0'
    }
}

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

import org.gradle.internal.os.OperatingSystem;

final APP_ABIS = ["x86", "armeabi"]

model {
    repositories {
        libs(PrebuiltLibraries) {
            openssl {
                binaries.withType(SharedLibraryBinary) {
                    if (targetPlatform.getName() == "armeabi" || targetPlatform.getName() == "x86") {
                        headers.srcDir file("src/main/prebuilt/openssl/${targetPlatform.getName()}/include")
                        sharedLibraryFile = file("src/main/prebuilt/openssl/${targetPlatform.getName()}/lib/libssl_1_0_0.so")
                    }
                }
            }
            opencrypto {
                binaries.withType(SharedLibraryBinary) {
                    if (targetPlatform.getName() == "armeabi" || targetPlatform.getName() == "x86") {
                        headers.srcDir file("src/main/prebuilt/opencrypto/${targetPlatform.getName()}/include")
                        sharedLibraryFile = file("src/main/prebuilt/openssl/${targetPlatform.getName()}/lib/libcrypto_1_0_0.so")
                    }
                }
            }
        }
    }

    android {
        compileSdkVersion = 23
        buildToolsVersion = "23.0.2"

        defaultConfig.with {
            minSdkVersion.apiLevel 16
            targetSdkVersion.apiLevel 23
            versionCode 1
            versionName "1.0"
        }
    }

    android.sources.main {
        jni {
            source {
                srcDir file("src/main/jni")
            }

            exportedHeaders {
                srcDir file("src/main/prebuilt/asio/1.10.6/include")
            }

            dependencies {
                library "openssl" linkage "shared"
                library "opencrypto" linkage "shared"
            }
        }

        jniLibs {
            dependencies {
                library "openssl"
                library "opencrypto"
           }
        }
    }

    android.ndk {
        moduleName "ndk_module_with_asio"

        cppFlags.add("-std=c++11")
        cppFlags.add("-fexceptions")
        cppFlags.add("-frtti")
        cppFlags.add("-D__STDC_CONSTANT_MACROS")
        cppFlags.add("-DASIO_STANDALONE")

        abiFilters.addAll(APP_ABIS)

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

        stl "gnustl_static"
    }

    android.buildTypes {
        debug {
            minifyEnabled = false
            proguardFiles.add(file('proguard-project.txt'))
            ndk.with {
                debuggable = true
            }
        }
        release {
            minifyEnabled = false
            proguardFiles.add(file('proguard-project.txt'))
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.google.guava:guava:19.0'
}

此外,我有带有jni函数的java文件来调用我的本机方法,并且在应用程序启动时我会这样做

try {
    System.loadLibrary("ssl_1_0_0");
    System.loadLibrary("crypto_1_0_0");
    System.loadLibrary("ndk_module_with_asio");
}
catch( UnsatisfiedLinkError ex ) {
    System.out.println("Native.java System.loadLibrary error occured: " + ex);
}

正如您所看到的,我已经加载了手动构建的openssl版本。但这没有必要。我添加了它,导致android openssl库很旧,并显示如何使用gradle加载共享库。 我希望,有了这个例子,你可以加载POCO库,或者只使用非boost asio。