編輯:關於Android編程
由於項目需要在NDK中使用網絡開發,對於c語言網絡開發來說,libcurl庫是個很不錯的選擇,但android系統中並沒有自帶該庫,所以就得自己移植了。
下面是移植步驟:
1. 下載curl源碼
我這裡下載的是curl-7.22.0,源碼下載地址為:http://curl.haxx.se/download.html
2. 准備android源碼編譯環境,
android源碼應已全部編譯過,具體細節這裡不詳述,我這裡使用的是android2.2 froyo源碼樹。
3. 在android中編譯curl
在最新的curl源碼裡其實已經帶有Android.mk這個編譯文件了,而且在這文件的開頭注釋部分比較詳細地介紹編譯方法。
1)拷貝curl源碼至android源碼樹下的external/curl
2)cd 到 external/curl目錄下,輸入(紅色字部分根據自己的環境做相應的更改):
復制代碼 代碼如下:
ANDROID_HOME=/home/braincol/workspace/android/froyo && \
NDK_HOME=/home/braincol/workspace/android/froyo/ndk && \
PATH="$ANDROID_HOME/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin:$PATH" \
./configure --host=arm-linux CC=arm-eabi-gcc --with-random=/dev/urandom \
CPPFLAGS="-I$NDK_HOME/platforms/android-8/arch-arm/usr/include \
-I $ANDROID_HOME/external/curl/include/ \
-I $ANDROID_HOME/external/curl/3rd/include \
-I $ANDROID_HOME/external/curl \
-I $ANDROID_HOME/out/target/product/generic/obj/STATIC_LIBRARIES/libcurl_intermediates \
-I $ANDROID_HOME/dalvik/libnativehelper/include/nativehelper \
-I $ANDROID_HOME/system/core/include \
-I $ANDROID_HOME/hardware/libhardware/include \
-I $ANDROID_HOME/hardware/libhardware_legacy/include \
-I $ANDROID_HOME/hardware/ril/include \
-I $ANDROID_HOME/dalvik/libnativehelper/include \
-I $ANDROID_HOME/frameworks/base/include \
-I $ANDROID_HOME/frameworks/base/opengl/include \
-I $ANDROID_HOME/frameworks/base/native/include \
-I $ANDROID_HOME/external/skia/include \
-I $ANDROID_HOME/out/target/product/generic/obj/include \
-I $ANDROID_HOME/bionic/libc/arch-arm/include \
-I $ANDROID_HOME/bionic/libc/include \
-I $ANDROID_HOME/bionic/libstdc++/include \
-I $ANDROID_HOME/bionic/libc/kernel/common \
-I $ANDROID_HOME/bionic/libc/kernel/arch-arm \
-I $ANDROID_HOME/bionic/libm/include \
-I $ANDROID_HOME/bionic/libm/include/arch/arm \
-I $ANDROID_HOME/bionic/libthread_db/include \
-include $ANDROID_HOME/system/core/include/arch/linux-arm/AndroidConfig.h \
-I $ANDROID_HOME/system/core/include/arch/linux-arm/ \
-D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -DANDROID -DNDEBUG -DNDEBUG -DHAVE_CONFIG_H" \
CFLAGS="-fno-exceptions -Wno-multichar -msoft-float -fpic -ffunction-sections \
-funwind-tables -fstack-protector -Wa,--noexecstack -Werror=format-security \
-fno-short-enums -march=armv5te -mtune=xscale -Wno-psabi -mthumb-interwork \
-fmessage-length=0 -W -Wall -Wno-unused -Winit-self -Wpointer-arith \
-Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point \
-g -Wstrict-aliasing=2 -finline-functions -fno-inline-functions-called-once \
-fgcse-after-reload -frerun-cse-after-loop -frename-registers -UDEBUG \
-mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 \
-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" \
LIBS="-nostdlib -Bdynamic -Wl,-T,$ANDROID_HOME/build/core/armelf.x \
-Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc \
-L$ANDROID_HOME/out/target/product/generic/obj/lib -Wl,-z,noexecstack \
-Wl,-rpath-link=$ANDROID_HOME/out/target/product/generic/obj/lib \
-lc -llog -lcutils -lstdc++ \
-Wl,--no-undefined $ANDROID_HOME/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/lib/gcc/arm-eabi/4.4.0/libgcc.a \
$ANDROID_HOME/out/target/product/generic/obj/lib/crtend_android.o \
-lm $ANDROID_HOME/out/target/product/generic/obj/lib/crtbegin_dynamic.o \
-L$ANDROID_HOME/external/curl/3rd/libs"
如果$ANDROID_HOME目錄下沒有ndk的開發包,那麼到google的官網上下載一個放進去就行了。
3)cd 到源碼根目錄 mmm extern/libcurl:
編譯完成之後,會生成靜態庫:out/target/product/generic/obj/STATIC_LIBRARIES/libcurl_intermediates/libcurl.a 。
4)如果要生成動態庫需要修改curl下的Android.mk :
復制代碼 代碼如下:
LOCAL_PRELINK_MODULE := false
LOCAL_MODULE:= libcurl
LOCAL_MODULE_TAGS := optional
# Copy the licence to a place where Android will find it.
# Actually, this doesn't quite work because the build system searches
# for NOTICE files before it gets to this point, so it will only be seen
# on subsequent builds.
ALL_PREBUILT += $(LOCAL_PATH)/NOTICE
$(LOCAL_PATH)/NOTICE: $(LOCAL_PATH)/COPYING | $(ACP)
$(copy-file-to-target)
#include $(BUILD_STATIC_LIBRARY)
include $(BUILD_SHARED_LIBRARY)
4. 在android中測試curl
1)在android froyo源碼樹中下建立一個mytest目錄,該目錄下再建立一個curltest目錄。
2)在目錄curtest下創建curl-test.cpp:
復制代碼 代碼如下:
#include
"curl/curl.h"#include
<stdio.h>;int
main() { CURL
*curl; CURLcode
res; curl_global_init(CURL_GLOBAL_ALL); curl
= curl_easy_init(); if
(curl) { curl_easy_setopt(curl,
CURLOPT_URL, "http://www.cnblogs.com/hibraincol/");
res
= curl_easy_perform(curl); if
(0!=res) { printf("curl
error: %d\n",
res); } curl_easy_cleanup(curl);
}
curl_global_cleanup(); return
0;}
3)在目錄curtest下創建Android.mk:
復制代碼 代碼如下:
#
curl test executable#LOCAL_PATH
:= $(call my-dir)include
$(CLEAR_VARS) LOCAL_C_INCLUDES
+= \ $(LOCAL_PATH)/3rd/include LOCAL_SRC_FILES:=
curl-test.cpp #
No shared libraries.LOCAL_SHARED_LIBRARIES
:= #
No static libraries.LOCAL_STATIC_LIBRARIES
:= libcurl LOCAL_MODULE
:= curl-testinclude
$(BUILD_EXECUTABLE)
4)把libcurl的頭文件拷貝到curtest目錄下的3rd/include目錄下:
cp -rf out/target/product/generic/obj/include/libcurl/curl mytest/curltest/3rd/include
5)到android源碼樹的根目錄下:mmm /mytest/curltest/
復制代碼 代碼如下:
braincol@ubuntu:~/workspace/android/froyo$ mmm mytest/curltest/
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=2.2
TARGET_PRODUCT=generic
TARGET_BUILD_VARIANT=eng
TARGET_SIMULATOR=
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
HOST_ARCH=x86
HOST_OS=linux
HOST_BUILD_TYPE=release
BUILD_ID=MASTER
============================================
make: Entering directory `/home/braincol/workspace/android/froyo'
target thumb C++: curl-test <= mytest/curltest/curl-test.cpp
mytest/curltest/curl-test.cpp:2:19: warning: extra tokens at end of #include directive
target Executable: curl-test (out/target/product/generic/obj/EXECUTABLES/curl-test_intermediates/LINKED/curl-test)
target Non-prelinked: curl-test (out/target/product/generic/symbols/system/bin/curl-test)
target Strip: curl-test (out/target/product/generic/obj/EXECUTABLES/curl-test_intermediates/curl-test)
Install: out/target/product/generic/system/bin/curl-test
make: Leaving directory `/home/braincol/workspace/android/froyo'
可以看到在out/target/product/generic/system/bin/下生成了curl-test這個測試程序。
6)將curl-test拷貝到真機或者模擬器中運行。
a.我這裡是在android真機的根目錄下建立了一個test目錄.
b.然後通過adb push將curl-test拷貝到該目錄下,並更改curl-test為可執行權限:chmod 777 curl-test.
c. adb shell 進入shell控制台,然後cd到test目錄下, ./curl-test 執行,可以看到打印出的網頁源碼,移植成功。
這樣在之後的android app開發中,如果需要用到libcurl的庫,就可以直接out/target/product/generic/obj/include/libcurl/curl裡的頭文件和out/target/product/generic/obj/STATIC_LIBRARIES/libcurl_intermediates/libcurl.a拿到app工程中去用就行了。
Starting an Activity 開啟一個ActivityThis lesson teaches you to 這節課教給你Understand the Life
一、基本使用1. NavigationView 在 design 庫中,添加依賴(最新的是 23.2.0);compile com.android.support:des
一、簡介這個題目是別人面試UC優視集團Android逆向工程師一職位的面試題,相比較前面的面試題1,增加了一些難度。 二、題目分析1.使
簡單實現的短信發送器,效果截圖如下: 其中的java代碼如下:package com.mxy.smssend; import java.util.ArrayList;