android - 在 android ndk 中集成 POCO 库

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

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

我正在使用 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++ 函数。我试图在网上搜索,但找不到与此相关的任何 Material 或官方指南。

最佳答案

好吧,我还没有尝试使用 POCO 库,但我成功地使用了非 boost 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 文件来调用我的 native 方法,在应用程序启动时我会调用

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。

关于android - 在 android ndk 中集成 POCO 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37520721/

相关文章:

android - 将 .mp3 文件合并为一个文件 .mp3 Android

java - setOnEditorActionListener() 方法不适用于输入类型 textMultiline 以及键盘中的输入按钮

C++创建具有动态大小的数组

android - 打开 Flutter 应用后出现空白白屏

java - 如何单击 AutocompleteTextView 列表上的事件并查询 sqlite。(我有一个示例代码。)

android - 在 Android 上如何将对象从一个 Activity 传递到另一个 Activity?

c++ - OpenMP 虚假共享

c++ - 我如何找到简单哈希算法的冲突

使用 gridview 和 RecyclerView 滚动的 android 布局

android - 将多个变量值发送到另一个 Activity