Build Android on Mac OS X

macOS android build development

Disclaimer: it's translation of my article that was written in Russian some time ago. All mentioned versions of software are outdated now. But anyway, it can be helpful :).

For one of my projects I needed to improve original Android sources to create firmware for specific hardware. The version of Android was chosen quite old — AOSP 4.0.4 but it was necessary because stable version of sources for our hardware was based on this version. The next issues was my work environment. Besides Android development I'm working with iOS so I have to use Mac OS X and Xcode as development environment.

The main issue in my case was broken AOSP build system for outdated versions of OS. While building of master branch is simple task, building of older versions of Android under new Mac OS X demands some patience and fixing a few bugs in build system.

My work environment looks like this:

  • AOSP 4.0.4 r1.1 (alongside with 4.0.3 and 4.1.1)
  • Mac OS X 10.7.5
  • Xcode 4.6.3 and command line tools
  • GNU Make 3.81

All essential moments and complications of AOSP build process is described in official documentation. The documentation gives us information that Mac OS 10.5 or 10.6 are necessary to build 4.0.x branch, also Xcode 3.14 is recommended version of Xcode. Additionally, in the "Known issues" section, mentioned that Mac OS 10.7 isn't supported for 4.0.x branch.

I was using Mac OS and Xcode for iOS development and I had no intention to move back on previous versions of software. As well as I wanted to solve this quest but not to avoid it.

Solution

The initial configuration is well described in official documentation and was set up on my computer: created case-sensitive disk image, installed necessary tools, downloaded sources of Android. And then I ran build with full-eng profile...

Bellow I give descriptions of the issues and solutions for them. They can appear fully or partially, it's related to used version of AOSP and current build environment. In addition to AOSP 4.0.4, 4.0.3 and 4.1.1 were checked too. There is the following trend: the more newer version of AOSP than less issues appear but first 2 issues have place everywhere. Separately necessary to note that emulator will be build correctly and can ne used without problems after all fixes described bellow.

To solve issue with compilator, you need to use the command listed bellow. The vital part is CC="gcc" CXX="g++", alongside with these parameters you can use other in any order or combination.

make CC="gcc" CXX="g++" -j4

So, will look on the issues what you can see while the building process (besides described in the documentation).

Issue 1
external/webkit/Source/WebCore/xml/XPathParser.cpp: In member function 'WebCore::XPath::Expression* WebCore::XPath::Parser::parseStatement(const WTF::String&, WTF::PassRefPtr<WebCore::XPathNSResolver>, WebCore::ExceptionCode&)':
external/webkit/Source/WebCore/xml/XPathParser.cpp:480:39: error: too many arguments to function 'int WebCore::XPath::xpathyyparse()'
out/target/product/generic/obj/STATIC_LIBRARIES/libwebcore_intermediates/Source/WebCore/XPathGrammar.hpp:106:5: note: declared here
make: *** [out/target/product/generic/obj/STATIC_LIBRARIES/libwebcore_intermediates/Source/WebCore/xml/XPathParser.o] Error 1
Solution 1

You need to apply the patch https://bugs.webkit.org/show_bug.cgi?id=92264. But this patch will be applied with minor error because of difference in context. It's necessary to check rej file and remove a few of old lines which cause the error. The target directory for patch is /external/webkit/.

Important

While project linking, on final stage of building, you can see the following issues:

prebuilt/darwin-x86/toolchain/arm-linux-androideabi-4.4.x/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld: out/target/product/generic/obj/STATIC_LIBRARIES/libwebcore_intermediates/libwebcore.a(CSSParser.o): in function WebCore::CSSParser::parseMediaQuery(WebCore::MediaList*, WTF::String const&):external/webkit/Source/WebCore/css/CSSParser.cpp:621: error: undefined reference to 'cssyyparse(WebCore::CSSParser*)'

It happens in relation to changes into CSSParser. Here appears a strange mystery of linker (you won't see such issues for 4.1.1 version). To fix the issue you need to include any changes to CSSParser (but not a comments, something what compiler will count). I changed definition of this method from cssyyparse(void*) to cssyyparse(WebCore::CSSParser*) or in reverse order. Then you can start rebuild and all will be fine.

Issue 2
host SharedLib: libSR_Recognizer (out/host/darwin-x86/obj/lib/libSR_Recognizer.dylib)
Undefined symbols for architecture i386:
  "_canPushAudioIntoRecognizer", referenced from:
      _SR_RecognizerAdvanceImpl in RecognizerImpl.o
      _detectBeginningOfSpeech in RecognizerImpl.o
    ...
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
make: *** [out/host/darwin-x86/obj/lib/libSR_Recognizer.dylib] Error 1
Solution 2

Move definitions to file /external/srec/portable/include/PortExport.h from master branch to current branch.

#if defined(__APPLE_CC__)
#if __APPLE_CC__ >= 5621
#undef PINLINE
#define PINLINE
#endif
#endif
Issue 3
host Executable: triangleCM (out/host/darwin-x86/obj/EXECUTABLES/
triangleCM_intermediates/triangleCM)
Undefined symbols:
  "__dyld_func_lookup", referenced from:
      _promoteLocalToGlobal in libSDL.a(SDL_dlcompat.o)
      _dlcompat_init_func in libSDL.a(SDL_dlcompat.o)
    ...
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [out/host/darwin-x86/obj/EXECUTABLES/
triangleCM_intermediates/triangleCM] Error 1
Solution 3

Add library LOCAL_SDL_LDLIBS += /usr/lib/dylib1.o to /development/tools/emulator/opengl/tests/translator_tests/ for /GLES_V2/Android.mk and /GLES_CM/Android.mk.

Issue 4

Different definitions of the method strnlen in system and local files. Unfortunately, I didn't save log for this issue but you can understand that when see mentioning of strnlen.

Solution 4

Add condition for method strnlen to /external/elfutils/config-compat-darwin.h:

#if __MAC_OS_X_VERSION_MIN_REQUIRED < 1070
static inline size_t strnlen (const char *__string, size_t __maxlen)
#endif
Issue 5

Choosing version of SDK by version of Mac OS to build emulator. It's without log, similar to previous issue. But you will understand when see mentioning of qemu.

Solution 5

Replace condition for Mac OS version in /external/qemu/Makefile.android:

-    ifeq ($(filter 10.5 10.5.%,$(DARWIN_VERSION)),)
+    ifneq ($(filter 10.6 10.6.%,$(DARWIN_VERSION)),)

And add inclusion of libraries for Mac OS:

 ifeq ($(HOST_OS),darwin)
   QEMU_SYSTEM_LDLIBS += -Wl,-framework,Cocoa,-framework,QTKit,-framework,CoreVideo
+  ifneq ($(filter 10.7 10.7.%,$(DARWIN_VERSION)),)
+    # Lion/XCode4 needs to be explicitly told the dynamic library
+    # lookup symbols in the precompiled libSDL are resolved at
+    # runtime
+    QEMU_SYSTEM_LDLIBS += -undefined dynamic_lookup
+  endif
 endif
Issue 6

You can see the following issue if you've OS on drive with case-sensitive file system:

external/qemu/android/camera/camera-capture-mac.m:24:24: error: QTKit/QTkit.h: No such file or directory
Solution 6

The cause of the issue is simple - misprint in the name of .h file. Instead of the capital 'K' is used small 'k'. Usually, this type of the issues won't appeared because Mac OS installs on drive with case-insensitive file system. To fix the issue you just need to replace letter to the correct one.

Conclusion

It's better to build Android under Linux systems but not under Mac OS :). You can use virtual box or just use separate installation of Linux on your computer. As I see, Mac OS can only be a partial alternative for AOSP build environment. Especially considering that all variations of build environment are not checked when Android moves further.

Next Post Previous Post