zoukankan      html  css  js  c++  java
  • android开发-c++代码调用so库

    Android项目的CMakeLists.txt代码如下,so文件放在项目的$Project/app/src/main/jniLibs/$arch下,
    $arch替换为arm64-v8a armv7a等
    cmake_minimum_required(VERSION 3.4.1)
    set(ARCH arch-arm64)
    set(libs_DIR /Users/musictom/libs/android/${ARCH}/usr/lib)
    set(libs_include_DIR /Users/musictom/libs/android/${ARCH}/usr/include)

    add_library(ghttp-lib SHARED IMPORTED)
    set_target_properties(ghttp-lib PROPERTIES IMPORTED_LOCATION
    ${libs_DIR}/libghttp.so)

    add_library( # Sets the name of the library.
    native-lib
    # Sets the library as a shared library.
    SHARED
    # Provides a relative path to your source file(s).
    src/main/cpp/native-lib.cpp )
    target_link_libraries( # Specifies the target library.
    native-lib my-lib ghttp-lib pocofoundation-lib poconet-lib pocoutil-lib

    # Links the target library to the log library
    # included in the NDK.
    ${log-lib} )

    app.gradle如下:
    apply plugin: 'com.android.application'

    android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
    applicationId "com.example.musictom.andcpp"
    minSdkVersion 21
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    externalNativeBuild {
    cmake {
    cppFlags "-std=c++11 -frtti -fexceptions -I/Users/musictom/libs/android/arch-arm64/usr/include/"
    }
    }
    ndk {
    // Specifies the ABI configurations of your native
    // libraries Gradle should build and package with your APK.
    //'x86', 'x86_64',
    abiFilters 'arm64-v8a'
    }
    // 设置so文件路径 so文件如果不在jniLibs目录下,需要在这里指定目录
    //sourceSets {
    // main {
    // jniLibs.srcDirs = ['src/main/jni/arm64-v8a']
    // }
    //}
    }
    buildTypes {
    release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    }
    externalNativeBuild {
    cmake {
    path "CMakeLists.txt"
    }
    }
    }

    dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
    testCompile 'junit:junit:4.12'
    }

    native-lib.cpp代码如下:
    #include <jni.h>
    #include <string>

    #include <ghttp/ghttp.h>

    #include <Poco/Net/HTTPClientSession.h>
    #include <Poco/Net/HttpRequest.h>
    #include <Poco/Net/HttpResponse.h>
    #include <Poco/StreamCopier.h>
    #include <Poco/Net/HTMLForm.h>
    #include <Poco/Net/NetException.h>
    #include <Poco/BinaryReader.h>
    #include <Poco/URI.h>
    #include <string>

    std::string get_ghttp() {
    /* This is the http request object */
    ghttp_request *request = NULL;
    /* Allocate a new empty request object */
    request = ghttp_request_new();
    /* Set the URI for the request object */
    ghttp_set_uri(request, "http://www.fuhan.org/wechat/verify");
    /* Close the connection after you are done. */
    ghttp_set_header(request, http_hdr_Connection, "close");
    /* Prepare the connection */
    ghttp_prepare(request);
    /* Process the request */
    ghttp_process(request);
    /* Write out the body. Note that the body of the request may not be null terminated so we have to be careful of the length. */
    //fwrite(ghttp_get_body(request), ghttp_get_body_len(request), 1, stdout);
    char* buf = ghttp_get_body(request);
    std::string s = buf;
    /* Destroy the request. This closes any file descriptors that may be open and will free any memory associated with the request. */
    ghttp_request_destroy(request);
    return s;
    }

    extern std::string aa();
    extern "C"
    JNIEXPORT jstring JNICALL
    Java_com_example_musictom_andcpp_MainActivity_stringFromJNI(
    JNIEnv* env,
    jobject /* this */) {
    std::string hello = "Hello from C++";
    hello = aa();
    std::string result;
    std::string responseText;

    if (false) {

    try {
    Poco::URI url("http://www.fuhan.org");
    Poco::Net::HTTPClientSession session(url.getHost(), url.getPort());
    //Poco::Net::HTTPClientSession s("www.cnblogs.com");

    //Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, "/auroratony/archive/2012/06/06/2537516.html");
    Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, "/wechat/verify",
    Poco::Net::HTTPRequest::HTTP_1_1);
    //request.set("/wechat/verify", "");

    //Poco::Net::HTMLForm form;
    //form.add("entry1", "value1");
    //form.prepareSubmit(request);
    session.sendRequest(request);
    Poco::Net::HTTPResponse response;

    std::istream &rs = session.receiveResponse(response);

    //Poco::StreamCopier copier;
    //copier.copyToString(rs, responseText);
    std::istreambuf_iterator<char> eos;
    std::string s(std::istreambuf_iterator<char>(rs), eos);
    responseText = s;
    }
    catch (Poco::Net::NetException &ex) {
    result = ex.displayText();
    responseText = "error:" + result;
    }
    }
    else {
    responseText = get_ghttp();
    }


    return env->NewStringUTF(responseText.c_str());
    }
  • 相关阅读:
    nginx uwsgi django
    ubuntu config proxy
    jdbc调用sparksql
    jdbc调用sparksql on yarn
    JDK错误
    JDK错误
    docker错误
    docker错误
    Django网站直接使用supervisor部署
    Django网站直接使用supervisor部署
  • 原文地址:https://www.cnblogs.com/yuanxiaoping_21cn_com/p/6719871.html
Copyright © 2011-2022 走看看