• 编译libcurl android


    http://my.oschina.net/zhoulc/blog/111929

    http://blog.csdn.net/vrix/article/details/7683212先贴一个博文,不过没成功。。。。

    利用源码编译的 : http://www.tmy13.com/?p=24

    加openssl编译的: http://piggyq.com/pen/compile-libcurl-and-openssl-on-android-using-ndk.html

    ——————————————————————————————————

    参考:终于编译成功curl,里面有上篇文章的摘要。

    其实就是缺少.configure出的配置文件,但是该文件是通用的,可以提供下载:Here

    里面有具体编译方法的说明,这里不具体再细讲了,这不过不是使用ndk-builder,而是ndk-builder.cmd。

    测试的话,使用eclipse建立Android工程:

    1.新建Android application project,名称叫:curltest,命名空间是:package com.mtterra.curltest,入口Activity是curltest,布局文件是main.xml。

    2.把上面压缩包里面的curltest.java拷贝到src,main.xml拷贝到layout,libcurltest.so拷贝到libs\armeabi,jni拷贝到项目root下,

    3 AndroidManifest.xml添加访问网络的权限:

    <uses-permission android:name=”android.permission.INTERNET”/>

    ——————————————————————————————————
    利用android-ndk编译libcurl动态库:
    1 建立目录 android-ndk/apps/curljni/
    2 在curljni中建立Application.mk文件 和project文件夹
    #Application.mk
    APP_PROJECT_PATH := $(call my-dir)/project
    APP_MODULES := curljni libcurl
     
    3 /project/jni
    jni文件夹中 建立Android.mk,放入curl源码和curljni.c 文件。
    #Android.mk
    LOCAL_PATH:= $(call my-dir)
    
    CFLAGS := -Wpointer-arith -Wwrite-strings -Wunused -Winline \
     -Wnested-externs -Wmissing-declarations -Wmissing-prototypes -Wno-long-long \
     -Wfloat-equal -Wno-multichar -Wsign-compare -Wno-format-nonliteral \
     -Wendif-labels -Wstrict-prototypes -Wdeclaration-after-statement \
     -Wno-system-headers -DHAVE_CONFIG_H
    
    include $(CLEAR_VARS)
    include $(LOCAL_PATH)/curl/lib/Makefile.inc
    
    
    LOCAL_SRC_FILES := $(addprefix curl/lib/,$(CSOURCES))
    LOCAL_CFLAGS += $(CFLAGS)
    LOCAL_C_INCLUDES += $(LOCAL_PATH)/curl/include/ $(LOCAL_PATH)/curl/lib
    
    LOCAL_COPY_HEADERS_TO := libcurl
    LOCAL_COPY_HEADERS := $(addprefix curl/include/curl/,$(HHEADERS))
    
    LOCAL_MODULE:= libcurl
    
    include $(BUILD_STATIC_LIBRARY)
    
    # Build shared library now
    # curltest
    
    include $(CLEAR_VARS)
    
    LOCAL_MODULE := curljni
    LOCAL_SRC_FILES := curljni.c
    LOCAL_STATIC_LIBRARIES := libcurl
    LOCAL_C_INCLUDES += $(LOCAL_PATH)/curl/include $(LOCAL_PATH)/curl/lib
    include $(BUILD_SHARED_LIBRARY) 

    //curljni.c

    #include <string.h>
    #include <jni.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include "curl_config.h"
    #include "curl/curl.h"
    
    typedef struct pageInfo_t {
      char *data;
      int  len;
    } pageInfo_t;
    
    static size_t
    HTTPData(void *buffer, size_t size, size_t nmemb, void *userData) {
      int len = size * nmemb;
      pageInfo_t *page = (pageInfo_t *)userData;
    
    
      if (buffer && page->data && (page->len + len < (16 * 1024)) ) {
        memcpy(&page->data[page->len], buffer, len);
        page->len += len;
      }
      return len;
    }
    
       //Inteface funciton that will recieve web page fom Java jstring
    Java_com_curljni_curljni_JNIGetWebpage( JNIEnv* env, jobject entryObject, jstring webpageJStr )
    {
      pageInfo_t page;
      CURL *curl;
      CURLcode res;
      char *buffer;
    
      const jbyte *webpage;
      webpage = (*env)->GetStringUTFChars(env, webpageJStr, NULL);
      if (webpage == NULL) {
          return NULL; /* OutOfMemoryError already thrown */
      }
    
      page.data = (char *)malloc(16 * 1024);
      page.len = 0;
      if (page.data)
        memset(page.data, 32, 16 * 1024);
    
      buffer = (char *)malloc(1024);
    
      curl = curl_easy_init();
      if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, webpage);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, HTTPData);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &page);
    
        res = curl_easy_perform(curl);
            /* always cleanup */
        curl_easy_cleanup(curl);
        (*env)->ReleaseStringUTFChars(env, webpageJStr, webpage);
        if(res == 0) {
          if (buffer) {
            page.data[page.len < 256 ? page.len : 256] = '\0';
        sprintf(buffer, "pagedata(%d): %s. done.\n", page.len, page.data);
            return (*env)->NewStringUTF(env, buffer);
          }
        }
        sprintf(buffer, "Result error:%d", res);
        return (*env)->NewStringUTF(env, buffer);
       } else {
          return (*env)->NewStringUTF(env, "Unable to init cURL");
       }
    } 

    4 在android-ndk目录中 执行 make APP=curljni -B

    在project/libs/armeabi/生成 libcurljni.so 文件。

    5 建立测试工程的时候注意命名。android:name="com.curljni.curljni"

     
    ---------------------------------------------------------------------------
    Summary of the steps to take for this port:
      - Lots of searches and view the current state of curl porting
      - Setup source repository (I used Linux, you could try windows using cygwin) 
      - Create curl_config.h under the full android source tree
      - Untar curltest.tar.gz under $NDKROOT or Create NDK JNI App on Windows
      - Cross-link or add to eclipse project to build Java front-end
      - Unpack cURL into $NDKROOT/apps/curltest/jni/curl
      - Copy curltest/jni/curlbuild.h to curltest/jni/curl/include/curl
      - Copy curltest/jni/curl_config.h to curltest/jni/curl/lib
      - Build and test 
     
    我的总结:
    编译curl时最麻烦又容易出错的是.configure,其实它的输出只是个curl_config.h文件。可以从别处拿来。第二个麻烦的地方是 Android.mk的编写。有幸的上文有给出。作者打了个包在这里:http://mtterra.com/files /curltest.tar.gz
     
    实践证明,有了这2个文件,libcurl是可以在cygwin+ndk的环境下编出来的。不过本文不是用我常用的ndk-build来编的,而是把目录放在ndk/apps目录下。用 make APP=curltest -B V=1 方式来编译.刚才试了一下。用$NDK/ndk-build -B V=1也可以编成功。(前提是用make方式编译过一遍,大概是由于有些文件要用make生成一次。)
     
  • 相关阅读:
    转:C/C++基本数据类型所占字节数
    转:为什么C++中空类和空结构体大小为1?
    转:内存字节对齐
    转:100层楼扔两个鸡蛋的问题
    移动互联网
    辗转相除法
    斐波那契数列
    error C2783: 无法为“T”推导 模板 参数
    创新工场和海豚浏览器宣讲会启示
    转:快速排序的一个小问题没想明白,求助各位
  • 原文地址:https://www.cnblogs.com/hbf369/p/3049521.html
Copyright © 2020-2023  润新知