integrate POCO library in android ndk

≯℡__Kan透↙ 提交于 2019-12-13 16:00:07

问题


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.

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'
}

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.


回答1:


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

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

Then I used gradle-experimental plugin and wrote 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'
}

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);
}

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.



来源:https://stackoverflow.com/questions/37520721/integrate-poco-library-in-android-ndk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!