Happy porting x86 application to android

31
Happy Porting x86 Application to Android 2010-07 Owen Hsu

description

This slide described how to port x86 application to arm-based android platform, and 2 examples are included

Transcript of Happy porting x86 application to android

Page 1: Happy porting x86 application to android

Happy Porting x86 Application to Android

2010-07

Owen Hsu

Page 2: Happy porting x86 application to android

Outline

• Motivation• Solutions• Examples

Page 3: Happy porting x86 application to android

Why Bother?

• Different instruction set – Most applications were programmed under x86 architecture, Android is run under ARM by default.

• Different variable types – There is NO standard C library in Android, it provides Bionic C library instead.

Page 4: Happy porting x86 application to android

Solutions?

• The Traditional Make way• The NDK way

Page 5: Happy porting x86 application to android

The Traditional Make Way

• Loop– 1. Configure– 2. Modify the Makefile– 3. Make– 4. Patch

• …

Page 6: Happy porting x86 application to android

The NDK Way

• 1. Configure• 2. Write Android Makefile• 3. Patch code• 4. Run

Page 7: Happy porting x86 application to android

Example

• Wget – Run as Android Executable

• Curl - Run as Android Shared Library

Page 8: Happy porting x86 application to android

Note

• These examples are made by NDK r3, the installation path is /usr/local/src/android-ndk-r3, it also marked as /PATH/TO/NDK in later slide

• The version of wget is 1.11.4, it is available in http://ftp.gnu.org/gnu/wget/wget-1.11.4.tar.gz, or in http://goo.gl/0inP

• The version of Curl is 7.20.0, it is available in http://curl.haxx.se/download/curl-7.20.0.tar.bz2, or in http://goo.gl/qGz6

Page 9: Happy porting x86 application to android

Wget Porting by NDK - 1. Configure

• 1.1 Create NDK apps– # mkdir –p /PATH/TO/NDK/apps/wget/project/jni– # cd /PATH/TO/NDK/apps/wget/project/jni

• 1.2 Copy wget source to NDK apps– # cp –Rf /usr/local/src/wget-1.11.4/* .

• 1.3 Disable most features, and create the Makefile– # ./configure --disable-opie --disable-digest --disable-ntlm --disable-debug --disable-largefile --disable-ipv6 --disable-nls --without-ssl

• Reference: Android_Makefile_Internal.pdf

Page 10: Happy porting x86 application to android

Wget Porting by NDK - 2. Write Android Makefile• 2.1 Create NDK Application.mk

– # vim /PATH/TO/NDK/apps/wget/Application.mk# begin codeAPP_PROJECT_PATH := $(call my-dir)/projectAPP_MODULES := wget # end code

• 2.2 Create NDK Android.mk– # vim /PATH/TO/NDK/apps/project/jni/Android.mk

# begin code include $(call all-subdir-makefiles)# end code

– # vim /PATH/TO/NDK/apps/project/jni/src/Android.mk# begin codeLOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := wgetLOCAL_SRC_FILES := alloca.c cmpt.c connect.c convert.c cookies.c ftp-basic.c ftp-ls.c ftp-opie.c ftp.c getopt.c hash.c host.c html-parse.c html-url.c http.c init.c log.c main.c netrc.c progress.c ptimer.c recur.c res.c retr.c safe-ctype.c snprintf.c spider.c url.c utils.c version.c xmalloc.cinclude $(BUILD_EXECUTABLE)# end code

Page 11: Happy porting x86 application to android

Wget Porting by NDK- 3. Patch code• 3.1 Test run

– # cd /PATH/TO/NDK– # make APP=wget V=1

• 3.2 Check out the error message– apps/wget/project/jni/src/sysdep.h:65: error: two or

more data types in declaration specifiers– apps/wget/project/jni/src/sysdep.h:203:3: error: #err

or "Cannot determine a 32-bit unsigned integer type"– apps/wget/project/jni/src/sysdep.h:211: error: confli

cting types for 'uintptr_t‘– apps/wget/project/jni/src/sysdep.h:216: error: confli

cting types for 'intptr_t'– apps/wget/project/jni/src/wget.h:162: error: redefini

tion of typedef 'off_t'– apps/wget/project/jni/src/options.h:119: error: expec

ted specifier-qualifier-list before 'wgint'

Page 12: Happy porting x86 application to android

Wget Porting by NDK- 3. Patch code

• 3.3 Patch code– Patch src/sysdep.h – Patch src/utils.c– Patch src/wget.h

– See: http://goo.gl/k2gt

• 3.4 Make & Get executable file– # cd /PATH/TO/NDK– # make APP=wget V=1– Result

Executable : wgetInstall : wget => apps/wget/project/libs/armeabi

Page 13: Happy porting x86 application to android

Wget Porting by NDK- 4. Run

• 4.1 Start the emulator– # emulator –avd Donut

• 4.2 Upload the wget binary under /PATH/TO/NDK/apps/wget/project/libs/armeabi– # adb push wget /system/bin

• 4.3 Run the application– # adb shell chmod 755 /system/bin/wget– # adb shell /system/bin/wget http://goo.gl/AsP5

Page 14: Happy porting x86 application to android

Wget Porting Reference

• http://jacob.hoffman-andrews.com/android/wget/

Page 15: Happy porting x86 application to android

The Diff/Patch Tool HOWTO

• diff– Compare the difference between two files– Syntax:

• diff [OPTION] SOURCE DESTINATION • -r: recursive• -N: new file• -u: uniform format

• patch– Patch/restore file by patch-file– Syntax:

• patch [OPTION] < PATCH_FILE• -p0: from current directory• -p1: ignore the first level of directory• -R: reverse• -E: remove-empty-files

Page 16: Happy porting x86 application to android

Example of Patch Single File (1/4)

• 1. Create test file– # cat >> test0 << EOF

> 0000

> 0000

> EOF– # cat >> test1 << EOF

> 1111

> 1111

> EOF

Page 17: Happy porting x86 application to android

Example of Patch Single File (2/4)

• 2. Create patch file– # diff –uN test0 test1 > test1.patch– # less test1.patch--- test0 2009-06-04 18:19:07.000000000 +0800+++ test1 2009-06-04 18:19:13.000000000 +0800@@ -1,2 +1,2 @@-0000-0000+1111+1111

Page 18: Happy porting x86 application to android

Description of Patch file

• Description:– Header: --- old file, +++ new file– Hunk: @@ line number which is modified

+ new added - new deleted

• Example:// Header--- test0 2009-06-04 18:19:07.000000000 +0800+++ test1 2009-06-04 18:19:13.000000000 +0800

// Hunk @@ -1,2 +1,2 @@

-0000-0000+1111+1111

Page 19: Happy porting x86 application to android

Example of Patch Single File (3/4)

• 3. Patch the target file– # patch –p0 < test1.patch– # less test0

1111

1111

Page 20: Happy porting x86 application to android

Example of Patch Single File (4/4)

• 4. Reverse the patched file– # less test0

1111

1111– # patch –RE –p0 < test1.patch– # less test0

0000

0000

Page 21: Happy porting x86 application to android

Example of Patch Multiple Files

• 1. Create test file– # mkdir prj0– # cp test0 prj0– # cd prj0– # cat >> foo0 << EOF> prj0/foo0> EOF

– # cd ..– # mkdir prj1– # cp test1 prj1– # cd prj1– # cat >> foo1 << EOF> prj1/foo1> EOF

– # cd ..

Page 22: Happy porting x86 application to android

Example of Patch Multiple Files

• 2. Create patch file– # diff –ruN prj0 prj1 > prj1.patch– # less prj1.patch

diff -ruN prj0/foo0 prj1/foo0--- prj0/foo0 2009-06-04 18:37:28.000000000 +0800+++ prj1/foo0 1970-01-01 08:00:00.000000000 +0800@@ -1 +0,0 @@-prj0/foo0diff -ruN prj0/foo1 prj1/foo1--- prj0/foo1 1970-01-01 08:00:00.000000000 +0800+++ prj1/foo1 2009-06-04 18:37:40.000000000 +0800@@ -0,0 +1 @@+prj1/foo1diff -ruN prj0/test0 prj1/test0--- prj0/test0 2009-06-04 18:31:55.000000000 +0800+++ prj1/test0 1970-01-01 08:00:00.000000000 +0800@@ -1,2 +0,0 @@-0000-0000diff -ruN prj0/test1 prj1/test1--- prj0/test1 1970-01-01 08:00:00.000000000 +0800+++ prj1/test1 2009-06-04 18:33:29.000000000 +0800@@ -0,0 +1,2 @@+1111+1111

Page 23: Happy porting x86 application to android

Example of Patch Multiple Files

• 3. Patch the target file – # cd prj0– # patch -p1 < ../prj1.patch– # ls

foo1 test1

Page 24: Happy porting x86 application to android

Example of Patch Multiple Files

• 4. Reverse the patched file– # ls

foo1 test1– # patch –RE –p1 < ../prj1.patch– # ls

foo0 test0

Page 25: Happy porting x86 application to android

Diff & Patch Reference

• http://www.xspace.idv.tw/bo_blog/read.php?97• http://www.linuxforums.org/articles/using-diff-and

-patch_80.html

Page 26: Happy porting x86 application to android

Curl Porting by NDK - 1. Configure• 1.1 Create NDK apps

– # mkdir –p /PATH/TO/NDK/apps/curl/project/jni– # cd /PATH/TO/NDK/apps/libcurl/project/jni

• 1.2 Copy curl source to NDK apps– # cp –Rf /usr/local/src/curl-7.20.0/* .

• 1.3 Disable most features, and create Makefile– # ./configure --disable-debug --disable-optimize --disable-

warnings --disable-curldebug --disable-ares --disable-dependency-tracking --disable-largefile --disable-libtool-lock --disable-ftp --disable-file --disable-ldap --disable-ldaps --disable-rtsp --disable-proxy --disable-dict --disable-telnet --disable-tftp --disable-pop3 --disable-imap --disable-smtp --disable-manual --disable-ipv6 --disable-verbose --disable-sspi --disable-crypto-auth --disable-cookies --disable-hidden-symbols --disable-soname-bump --without-ssl --without-zlib --without-gnutls --without-nss --without-ca-path --without-libssh2 --without-libidn

Page 27: Happy porting x86 application to android

Curl Porting by NDK - 1. Configure

• Notice:– Config with "--disable-nonblocking" will cause the abnormal data transfer

Page 28: Happy porting x86 application to android

Curl Porting by NDK - 2. Write Android Makefile

• Fortunately, a sample android.mk in source is included, we just need to do some modifications

• Two-libs format:– Compile first library as shared library– Compile second library as executable file

Page 29: Happy porting x86 application to android

Curl Porting by NDK- 3. Patch code• 3.1 Patch code

– Patch project/jni/lib/connect.c– Patch project/jni/lib/url.c– See: http://goo.gl/LHbR

• 3.2 Add C program to invoke Curl library

• 3.3 Make & Get shared library– # cd /PATH/TO/NDK– # make APP=c-call-curl V=1– Result:

SharedLibrary : libcurl.soInstall : libcurl.so => apps/c-call-curl/project/libs/armeabiCompile thumb : c-call-curl <= apps/c-call-curl/project/jni/simple.cExecutable : c-call-curlInstall : c-call-curl => apps/c-call-curl/project/libs/armeabi

Page 30: Happy porting x86 application to android

Curl Porting by NDK- 4. Run

• 4.1 Start the emulator– # emulator –avd Donut

• 4.2 Upload the Curl library & C program under /PATH/TO/NDK/apps/curl/project/libs/armeabi– # adb remount– # adb push c-call-curl /system/bin– # adb push libcurl.so /system/lib

• 4.3 Run the application– # adb shell chmod 755 /system/bin/c-call-curl– # adb shell /system/bin/c-call-curl

Page 31: Happy porting x86 application to android

Curl Porting Reference

• http://curl.haxx.se/mail/lib-2009-12/0071.html