在Android ndk中集成POCO库 [英] integrate POCO library in android ndk

查看:1146
本文介绍了在Android ndk中集成POCO库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图开发使用 POCO套接字库 android NDK 的跨平台应用程序。但我不知道如何整合/使用 POCO库 android NDK 使用gradle在工作室。

i am trying to develop cross platform app ( on native level ) that uses POCO socket library with android NDK. but i dont know how to integrate/use POCO library in android NDK using gradle in studio.

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

i am using com.android.tools.build:gradle-experimental:0.7.0 gradle version. And following is my 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库,但不知道在 build.gradle 文件中要改变什么,以使用其在ndk中的C ++函数。我试图在网上搜索,但找不到任何材料或相关的官方指南。

i have downloaded POCO library but dont know what to alter in build.gradle file to use its C++ function in ndk. i tried to hunt over net but couldn't find any material or official guide related to that.

推荐答案

t试图使用POCO库,但我成功地使用非boost asio库。
看看我做了什么。

Well, I haven't tried to use POCO library, but I successfuly used non-boost asio library. Look, what I've done.

将asio文件放入目录如下

Put asio files to directory as follows

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

然后我使用 gradle-experimental插件并写了 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'
}

此外,我有java文件与jni函数调用我的本机方法和应用程序启动时do

Also I have java files with jni functions to call my native methods and at application start I do

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。

As you can see I've load manually built version of openssl. But it's not necessary. I added it, cause android openssl library is old and to show how to load shared libraries with gradle. I hope, having this example, you can load POCO library, or just use non-boost asio.

这篇关于在Android ndk中集成POCO库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆