Building Android on Mac OS X 10.6

最近入手一台 MacBook Pro, 剛好公司也在跑 Android 的 project, 想說沒理由不能在上面編譯整個 Android source code, 所以在某個下班的晚上開始 survey 相關的資訊. 過程如何就不再贅述, 都是很辛酸的 compile error, 這邊整理完整步驟, 希望可以對有興趣在 Mac OS 10.6 之後的版本上 build Android 的朋友有所幫助.

事前準備:

a. 安裝 Xcode, 記得點選 10.4 SDK compatibility option.

b. 安裝 J2SE 5.0 (Android build system 會 check 版本)

很不幸的, 在 Snow Leopard 上預設安裝的是 Java SE 6, 所以得另外找 package. 最新版本可以從 Mac OS X 10.5 update 6 取出, 推薦使用 Pacifist.

  1. 首先從 Finder 進入 System -> Library -> Frameworks -> JavaVM.framework -> Versions 把 1.5 與 1.5.0 這兩個 link 刪除.
  2. 用 Pacifist 將下載的 update pkg 開啟, 點入 Contents -> System -> Library -> Frameworks -> JavaVM.framework -> Versions
  3. 點選安裝 1.5 與 1.5.0 到 default location.
  4. 將步驟 1 中, 1.6.0 目錄內的 Headers 資料夾 copy 一份到 1.5.0 裡面, 這是 jni 的 header files, 不知道為什麼 1.5 沒有.
  5. 開啟 Applications -> Utilities -> Java Preferences, 將 Java Applications 裡面的 J2SE 5.0 64-bit 拉到最上面. 如果忘了做這步, 一開始就沒法編譯了. 如果不小心拉到 32-bit, 那麼會產生 ‘could not load clearsilver-jnilib’ 的錯誤.

如此一來便可使用 J2SE 5.0 在 Mac OS X 10.6 上編譯 Android. 其他要安裝的套件跟 get code 流程可以參考官方網站的說明, 另外記得編譯環境所在的 partition 得是 case sensitive filesystem, 如果不是 (default 應該都不是), 可以透過 Disk Utility 建立一個 disk image 拿來使用編譯 Android.

c. apply patch

網路上很多網頁都寫著要另外 apply 四組 patch, 否則會有 compile error:

基本上前三組都已經 commit 到 repository 所以不用做, 最後一個 patch 則是為了 compile 過而作的, 個人認為 host 改用 32 bit 編譯即可避開 compile error 的問題 (反正也能跑, 只是 tools 為 32bit executable 而非 64bit), 所以建議另外修改 build/core/combo/darwin-x86.mk 與 build/core/config.mk:

diff –git a/core/combo/darwin-x86.mk b/core/combo/darwin-x86.mk
index 2150960..1335969 100644
— a/core/combo/darwin-x86.mk
+++ b/core/combo/darwin-x86.mk
@@ -65,6 +65,9 @@ define transform-host-o-to-shared-lib-inner
$(HOST_CXX) \
-dynamiclib -single_module -read_only_relocs suppress \
$(HOST_GLOBAL_LD_DIRS) \
+       $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
+               $(HOST_GLOBAL_LDFLAGS) \
+       ) \
$(PRIVATE_ALL_OBJECTS) \
$(call normalize-target-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) \
$(call normalize-target-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
@@ -81,6 +84,9 @@ $(HOST_CXX) \
-Wl,-dynamic -headerpad_max_install_names \
$(HOST_GLOBAL_LD_DIRS) \
$(call normalize-target-libraries,$(PRIVATE_ALL_SHARED_LIBRARIES)) \
+       $(if $(PRIVATE_NO_DEFAULT_COMPILER_FLAGS),, \
+               $(HOST_GLOBAL_LDFLAGS) \
+       ) \
$(PRIVATE_ALL_OBJECTS) \
$(PRIVATE_LDLIBS) \
$(call normalize-target-libraries,$(PRIVATE_ALL_WHOLE_STATIC_LIBRARIES)) \
diff –git a/core/config.mk b/core/config.mk
index 4662a38..f9ab828 100644
— a/core/config.mk
+++ b/core/config.mk
@@ -296,6 +296,11 @@ TARGET_GLOBAL_CPPFLAGS += $(TARGET_RELEASE_CPPFLAGS)
PREBUILT_IS_PRESENT := $(if $(wildcard prebuilt/Android.mk),true)
+ifeq ($(HOST_OS),darwin)
+HOST_GLOBAL_CFLAGS += -arch i386
+HOST_GLOBAL_CPPFLAGS += -arch i386
+HOST_GLOBAL_LDFLAGS += -arch i386
+endif
# ###############################################################
# Collect a list of the SDK versions that we could compile against

還有 external/qemu/Makefile.android

diff –git a/Makefile.android b/Makefile.android
index 0b719e0..10831aa 100644
— a/Makefile.android
+++ b/Makefile.android
@@ -49,7 +49,7 @@ ifeq ($(HOST_ARCH),ppc)
endif
ifeq ($(HOST_OS),darwin)
-    MY_CFLAGS += -mdynamic-no-pic
+    MY_CFLAGS += -mdynamic-no-pic -I /Developer/usr/lib/gcc/i686-apple-darwin10/4.0.1/include/
# When building on Leopard or above, we need to use the 10.4 SDK
# or the generated binary will not run on Tiger.

這樣可以解決 qemu 編譯錯誤問題.

最後下 make 就可快快樂樂編譯 Android 囉. 個人的經驗中間一定會有一次因為 out of memory build failed, 只要再下一次 make 仍可 build 過, 切勿驚慌 :D

有任何問題歡迎提出指教 ;-)

2010-04-21 Update: 似乎忘了補上一點, J2SE 1.5 裝了之後, 應該要改 CurrentJDK 的連結. 切換到 /System/Library/Frameworks/JavaVM.frameworks/Versions/, 接著 sudo ln -shfv 1.5 CurrentJDK 才算是切換目前使用 JDK 版本, 日後要改用 1.6 也是用類似的方法.

This content is published under the Attribution-Noncommercial-Share Alike 3.0 Unported license.

Tags: , , ,

8 Responses to “Building Android on Mac OS X 10.6”

  1. shakalaca says:

    不過我比較好奇的是 ubuntu 64bit 的環境是不是可以完整 build 過 ? :-P

  2. apaul says:

    請受我一拜 Orz

  3. shakalaca says:

    看來要編譯 kernel 得另外安裝一些東西, 再整理一下..

  4. shakalaca says:

    啊 ! My fault, 看來 java 1.6 的 patch 還沒進來, 找時間在來測試看看 1.6 能否 build 過 :D

  5. [...] 二三街角 Nurdy, Engineer, Coding Monkey « Building Android on Mac OS X 10.6 [...]

  6. [...] Building Android on Mac OS X 10.6 « 二三街角 blog.23corner.com/2010/04/20/building-android-on-mac-os-x-10-6 – view page – cached 最近入手一台 MacBook Pro, 剛好公司也在跑 Android 的 project, 想說沒理由不能在上面編譯整個 Android source code, 所以在某個下班的晚上開始 survey 相關的資訊. 過程如何就不再贅述, 都是很辛酸的 compile error, 這邊整理完整步驟, 希望可以對有興趣在 Mac OS 10.6 之後的版本上 build Android 的朋友有所幫助. Tweets about this link Topsy.Data.Twitter.User['shakalaca'] = {"location":"Taipei, Taiwan","photo":"http://a3.twimg.com/profile_images/53395545/honta2_normal.jpg","name":"shakalaca","url":"http://twitter.com/shakalaca","nick":"shakalaca","description":"","influence":""}; shakalaca: “says http://tinyurl.com/2f6eqkd (Build Android on Mac OS X 10.6) // 看來是 ok 的, 有需要的朋友試看看吧 !! :D http://plurk.com/p/4toob7 ” 6 days ago view tweet retweet Filter tweets [...]

  7. [...] Building Android on Mac OS X 10.6 « 二三街角 [FAQ] Build Android on Snow Leopard | Jelly’s Blog [...]

Leave a Reply