Building OpenCV4Android from trunk
Version 1 (Alexander Smorkalov, 2014-10-02 11:43 am)
1 | 1 | h1. Building the trunk OpenCV for Android |
|
---|---|---|---|
2 | 1 | ||
3 | 1 | {{>toc}} |
|
4 | 1 | ||
5 | 1 | The new android build is based on android-cmake project originally developed by Ethan Rublee. |
|
6 | 1 | You can find the latest version of his project at http://code.google.com/p/android-cmake/ |
|
7 | 1 | ||
8 | 1 | h2. Prerequisites |
|
9 | 1 | ||
10 | 1 | * svn client |
|
11 | 1 | * cmake tool http://www.cmake.org/ |
|
12 | 1 | * Android NDK (revision !r5 or newer) http://developer.android.com/sdk/ndk/index.html |
|
13 | 1 | * OpenCV trunk http://code.opencv.org/svn/opencv/trunk/opencv. You can checkout sources with the following command: |
|
14 | 1 | <pre>svn co http://code.opencv.org/svn/opencv/trunk/opencv</pre> |
|
15 | 1 | ||
16 | 1 | h2. Additional requirements for Java wrapper and Android samples |
|
17 | 1 | ||
18 | 1 | * JDK 6 http://www.oracle.com/technetwork/java/javase/downloads/index.html |
|
19 | 1 | * Android SDK http://developer.android.com/sdk/index.html |
|
20 | 1 | ** "Android SDK Tools, revision 14" or newer is recommended |
|
21 | 1 | ** "SDK platform Android 2.2, API 8" or newer package installed (for the Java API and tests) |
|
22 | 1 | ** "SDK platform Android 3.0, API 11" or newer for the samples (they can run on Android 2.2 but require API 11 at compile time) |
|
23 | 1 | * Apache Ant 1.8.0 or newer http://ant.apache.org |
|
24 | 1 | * Python 2.6 or newer (but not 3.x) http://www.python.org/ |
|
25 | 1 | ||
26 | 1 | h2. Windows additional prerequisites |
|
27 | 1 | ||
28 | 1 | At the moment OpenCV cross compilation under @cygwin@ is *not supported*. |
|
29 | 1 | ||
30 | 1 | However you can cross-compile OpenCV on Windows using '''native port of make'''. |
|
31 | 1 | Android NDK !r7 and newer already includes the make executable. |
|
32 | 1 | If you need to build OpenCV with older NDK, then you can get it from NDK !r7 or use one of alternative downloads. These two are tested to work for OpenCV: |
|
33 | 1 | * http://gnuwin32.sourceforge.net/packages/make.htm |
|
34 | 1 | * http://code.google.com/p/mingw-and-ndk/ (see "make-3.7z" under the Downloads section) |
|
35 | 1 | ||
36 | 1 | h1. OpenCV build |
|
37 | 1 | ||
38 | 1 | By default OpenCV and the most of Android samples are configured for modern @ARM-v7a@ architecture. |
|
39 | 1 | If you want to compile OpenCV for emulator or older device, or want to build with NEON support please read the [[Trunk_OpenCV_for_Android#Android-targets]] section for instructions. |
|
40 | 1 | ||
41 | 1 | h2. Linux (Ubuntu) and Mac OS (10.6.7) |
|
42 | 1 | ||
43 | 1 | Export full path to your copy of NDK |
|
44 | 1 | <pre> |
|
45 | 1 | export ANDROID_NDK=~/android-ndk-r7 |
|
46 | 1 | </pre> |
|
47 | 1 | ||
48 | 1 | Alternatively to build with standalone toolchain extracted from NDK you can export toolchain location |
|
49 | 1 | <pre> |
|
50 | 1 | export ANDROID_STANDALONE_TOOLCHAIN=~/android-toolchain |
|
51 | 1 | </pre> |
|
52 | 1 | ||
53 | 1 | If you have not declared any of these variables then build scripts will try to find NDK under the @/opt@ folder |
|
54 | 1 | (so if you have placed your NDK to @/opt/android-ndk-r7b/@ then it will be detected automatically). |
|
55 | 1 | ||
56 | 1 | There is a @scripts@ folder in the @android@ folder for running cmake with the proper cache |
|
57 | 1 | variables set. It is recommended that you use this to setup a cmake build directory. |
|
58 | 1 | <pre> |
|
59 | 1 | cd opencv/android |
|
60 | 1 | sh ./scripts/cmake_android.sh |
|
61 | 1 | </pre> |
|
62 | 1 | ||
63 | 1 | You should now see a build directory, that is ready to be made |
|
64 | 1 | <pre> |
|
65 | 1 | cd build |
|
66 | 1 | make -j8 |
|
67 | 1 | </pre> |
|
68 | 1 | ||
69 | 1 | That will build most of the OpenCV modules, except for those that don't make sense on android - @gpu@, etc.. |
|
70 | 1 | ||
71 | 1 | h3. Troubleshooting |
|
72 | 1 | ||
73 | 1 | If you get an error saying `install_name_tool not found`, then: |
|
74 | 1 | # search for the file @install_name_tool@ on your file system (usually it is in @/usr/bin/@); |
|
75 | 1 | # open the file @/opt/local/share/cmake-2.8/Modules/CMakeFindBinUtils.cmake@; |
|
76 | 1 | # after the line @FIND_PROGRAM(CMAKE_INSTALL_NAME_TOOL NAMES install_name_tool HINTS ${_CMAKE_TOOLCHAIN_LOCATION})@ add the line |
|
77 | 1 | <pre> |
|
78 | 1 | set( CMAKE_INSTALL_NAME_TOOL <path to install_name_tool>) |
|
79 | 1 | </pre> |
|
80 | 1 | e.g. something like |
|
81 | 1 | <pre> |
|
82 | 1 | set( CMAKE_INSTALL_NAME_TOOL /usr/bin/install_name_tool) |
|
83 | 1 | </pre> |
|
84 | 1 | Next remove your @build@ folder and restart from the @./scripts/cmake_android.sh@ script. |
|
85 | 1 | ||
86 | 1 | h2. Windows (native) |
|
87 | 1 | ||
88 | 1 | Define the following environment variables: |
|
89 | 1 | * @ANDROID_NDK@ - full path to the Android NDK. This path should not have tailing slash. |
|
90 | 1 | * @CMAKE_EXE@ - full path to cmake executable (including executable name). |
|
91 | 1 | * @MAKE_EXE@ - full path to native port of make (including executable name). This path should not contain spaces. |
|
92 | 1 | ||
93 | 1 | Instead of modifying your environment you can create file @opencv\android\scripts\wincfg.cmd@ and put all settings into this file. OpenCV provides a template for this file named @opencv\android\scripts\wincmd.cfg.tmpl@. Please note that settings from @wincfg.cmd@ takes precedence over global environment variables. |
|
94 | 1 | ||
95 | 1 | Then open Windows console (@cmd.exe@) and go to @opencv\android@ folder. Run script @cmake_android.cmd@ from the @scripts@ folder: |
|
96 | 1 | <pre> |
|
97 | 1 | cd opencv\android\ |
|
98 | 1 | scripts\cmake_android.cmd |
|
99 | 1 | </pre> |
|
100 | 1 | When script finishes you will have OpenCV compiled. |
|
101 | 1 | ||
102 | 1 | h3. Troubleshooting |
|
103 | 1 | ||
104 | 1 | First, make sure that paths to OpenCV sources, Android NDK and @make.exe@ do not contain spaces and other suspicious symbols. |
|
105 | 1 | ||
106 | 1 | If @make@ fails with message similar to |
|
107 | 1 | <pre> |
|
108 | 1 | *** target pattern contains no `%'. Stop. |
|
109 | 1 | </pre> |
|
110 | 1 | Then you are probably using @cygwin@ tools instead of their native ports. Remove @cygwin@ from your @PATH@, delete @build@ directory and rebuild. |
|
111 | 1 | ||
112 | 1 | Or if @make@ fails with Java related messages, then make sure your version of python is a Windows app instead of the @cygwin@ one, such as "ActiveState Python v2.7":http://www.activestate.com/activepython/downloads. |
|
113 | 1 | ||
114 | 1 | ||
115 | 1 | h1. Building console "Hello World" with OpenCV |
|
116 | 1 | ||
117 | 1 | This application is not usual Android application. This is a Unix console which may be used as a template for development. Console application is much more convenient for development, debugging and performance optimization stages. Then, if you are finished with your CV algorithm, you can switch to normal Android development with Java + NDK. |
|
118 | 1 | ||
119 | 1 | h2. Prerequisities |
|
120 | 1 | ||
121 | 1 | First of all you need an Android device with root privileges. The goal is to have ability to run console applications on Android device using @adb shell@. Even @chmod@ is not available by default (for example on Nexus One). You have the following options: |
|
122 | 1 | * Use emulator |
|
123 | 1 | * Enable root access to your device. Use ''Google'':http://google.com to find instructions for your device. |
|
124 | 1 | * Use developer device, for example NVidia Tegra devkits have root access by default. |
|
125 | 1 | ||
126 | 1 | If the device allows you to do @chmod@ and run executables, then follow the instructions below. |
|
127 | 1 | ||
128 | 1 | Second, you need OpenCV compiled for your device. *This sample is configured for emulator. It is different from default settings for OpenCV.* Read [[Trunk_OpenCV_for_Android#Android-ABIs]] to know how to build OpenCV for emulator. |
|
129 | 1 | Also you need some free space on sdcard to run the @HelloAndroid@ application - it will write small image to @/mnt/sdcard@. *Please note*, that even with emulator you need a sdcard. You can create it with @AVD Manager@ from Android SDK. |