http://docs.opencv.org/doc/tutorials/introduction/android_binary_package/android_dev_intro.html#android-dev-intro


Introduction into Android Development

This guide was designed to help you in learning Android development basics and seting up your working environment quickly. It was written with Windows 7 in mind, though it would work with Linux (Ubuntu), Mac OS X and any other OS supported by Android SDK.

If you encounter any error after thoroughly following these steps, feel free to contact us viaOpenCV4Androiddiscussion group or OpenCVQ&A forum. We’ll do our best to help you out.

Preface

Android is a Linux-based, open source mobile operating system developed by Open Handset Alliance led by Google. See theAndroid home sitefor general details.

Development for Android significantly differs from development for other platforms. So before starting programming for Android we recommend you make sure that you are familiar with the following key topis:

  1. Javaprogramming language that is the primary development technology for Android OS. Also, you can findOracle docs on Javauseful.
  2. Java Native Interface (JNI)that is a technology of running native code in Java virtual machine. Also, you can findOracle docs on JNIuseful.
  3. Android Activityand its lifecycle, that is an essential Android API class.
  4. OpenCV development will certainly require some knowlege of theAndroid Cameraspecifics.

Quick environment setup for Android development

If you are making a clean environment install, then you can tryTegra Android Development Pack(TADP) released byNVIDIA.

Note

Starting theversion 2.0the TADP package includesOpenCV for TegraSDK that is a regularOpenCV4Android SDKextended with Tegra-specific stuff.

When unpacked, TADP will cover all of the environment setup automatically and you can skip the rest of the guide.

If you are a beginner in Android development then we also recommend you to start with TADP.

Note

NVIDIA‘s Tegra Android Development Pack includes some special features forNVIDIA’s Tegra platformbut its use is not limited toTegradevices only.

  • You need at least1.6 Gbfree disk space for the install.

  • TADP will download Android SDK platforms and Android NDK from Google’s server, so Internet connection is required for the installation.

  • TADP may ask you to flash your development kit at the end of installation process. Just skip this step if you have noTegra Development Kit.

  • (UNIX) TADP will ask you forrootin the middle of installation, so you need to be a member ofsudogroup.

Manual environment setup for Android development

Development in Java

You need the following software to be installed in order to develop for Android in Java:

  1. Sun JDK 6(Sun JDK 7 is also possible)

    VisitJava SE Downloads pageand download an installer for your OS.

    Here is a detailedJDKinstallation guidefor Ubuntu and Mac OS (only JDK sections are applicable for OpenCV)

    Note

    OpenJDK is not suitable for Android development, since Android SDK supports only Sun JDK. If you use Ubuntu, after installation of Sun JDK you should run the following command to set Sun java environment:

    sudo update-java-alternatives --set java-6-sun
  2. Android SDK

    Get the latestAndroidSDKfromhttp://developer.android.com/sdk/index.html

    Here is Google’sinstall guidefor the SDK.

    Note

    You can choose downloadingADT Bundle packagethat in addition to Android SDK Tools includes Eclipse + ADT + NDK/CDT plugins, Android Platform-tools, the latest Android platform and the latest Android system image for the emulator - this is the best choice for those who is setting up Android development environment the first time!

    Note

    If you are running x64 version of Ubuntu Linux, then you need ia32 shared libraries for use on amd64 and ia64 systems to be installed. You can install them with the following command:

    sudo apt-get install ia32-libs

    For Red Hat based systems the following command might be helpful:

    sudo yum install libXtst.i386
  3. Android SDK components

    You need the following SDK components to be installed:

    • Android SDK Tools, revision 20or newer.

      Older revisions should also work, but they are not recommended.

    • SDK Platform Android 3.0(API11).

      The minimal platform supported by OpenCV Java API isAndroid 2.2(API8). This is also the minimum API Level required for the provided samples to run. See the<uses-sdkandroid:minSdkVersion="8"/>tag in theirAndroidManifest.xmlfiles. But for successful compilation thetargetplatform should be set to Android 3.0 (API 11) or higher. It will not prevent them from running on Android 2.2.

      SeeAdding Platforms and Packagesfor help with installing/updating SDK components.

  4. Eclipse IDE

    Check theAndroid SDK System Requirementsdocument for a list of Eclipse versions that are compatible with the Android SDK. For OpenCV 2.4.x we recommendEclipse 3.7 (Indigo)orEclipse 4.2 (Juno). They work well for OpenCV under both Windows and Linux.

    If you have no Eclipse installed, you can get it from theofficial site.

  5. ADT plugin for Eclipse

    These instructions are copied fromAndroid Developers site, check it out in case of any ADT-related problem.

    Assuming that you have Eclipse IDE installed, as described above, follow these steps to download and install the ADT plugin:

    1. Start Eclipse, then selectHelp ‣ Install New Software...

    2. ClickAdd(in the top-right corner).

    3. In theAdd Repositorydialog that appears, enter “ADT Plugin” for the Name and the following URL for the Location:

      https://dl-ssl.google.com/android/eclipse/

    4. ClickOK

      Note

      If you have trouble acquiring the plugin, try using “http” in the Location URL, instead of “https” (https is preferred for security reasons).

    5. In theAvailable Softwaredialog, select the checkbox next toDeveloper Toolsand clickNext.

    6. In the next window, you’ll see a list of the tools to be downloaded. ClickNext.

      Note

      If you also plan to develop native C++ code with Android NDK don’t forget to enableNDK Pluginsinstallations as well.

    7. Read and accept the license agreements, then clickFinish.

      Note

      If you get a security warning saying that the authenticity or validity of the software can’t be established, clickOK.

    8. When the installation completes, restart Eclipse.

Native development in C++

You need the following software to be installed in order to develop for Android in C++:

  1. Android NDK

    To compile C++ code for Android platform you needAndroidNativeDevelopmentKit(NDK).

    You can get the latest version of NDK from thedownload page. To install Android NDK just extract the archive to some folder on your computer. Here areinstallation instructions.

    Note

    Before start you can read official Android NDK documentation which is in the Android NDK archive, in the folderdocs/. The main article about using Android NDK build system is in theANDROID-MK.htmlfile. Some additional information you can find in theAPPLICATION-MK.html,NDK-BUILD.htmlfiles, andCPU-ARM-NEON.html,CPLUSPLUS-SUPPORT.html,PREBUILTS.html.

  2. CDT plugin for Eclipse

    If you selected for installation theNDKpluginscomponent of Eclipse ADT plugin (see the picture above) your Eclipse IDE should already haveCDTplugin(that meansC/C++DevelopmentTooling). There are several possible ways to integrate compilation of C++ code by Android NDK into Eclipse compilation process. We recommend the approach based on EclipseCDTBuilder.

Android application structure

Usually source code of an Android application has the following structure:

  • rootfolderoftheproject/
    • jni/
    • libs/
    • res/
    • src/
    • AndroidManifest.xml
    • project.properties
    • ...otherfiles...

Where:

  • thesrcfolder contains Java code of the application,

  • theresfolder contains resources of the application (images, xml files describing UI layout, etc),

  • thelibsfolder will contain native libraries after a successful build,

  • and thejnifolder contains C/C++ application source code and NDK’s build scriptsAndroid.mkandApplication.mkproducing the native libraries,

  • AndroidManifest.xmlfile presents essential information about application to the Android system (name of the Application, name of main application’s package, components of the application, required permissions, etc).

    It can be created using Eclipse wizard orandroidtool from Android SDK.

  • project.propertiesis a text file containing information about target Android platform and other build details. This file is generated by Eclipse or can be created withandroidtool included in Android SDK.

Note

BothAndroidManifest.xmlandproject.propertiesfiles are required to compile the C++ part of the application, since Android NDK build system relies on them. If any of these files does not exist, compile the Java part of the project before the C++ part.

Android.mkandApplication.mkscripts

The scriptAndroid.mkusually has the following structure:

 1 2 3 4 5 6 7 8 910
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := <module_name>LOCAL_SRC_FILES := <list of .c and .cpp project files><some variable name> := <some variable value>...<some variable name> := <some variable value>include $(BUILD_SHARED_LIBRARY)

This is the minimal fileAndroid.mk, which builds C++ source code of an Android application. Note that the first two lines and the last line are mandatory for anyAndroid.mk.

Usually the fileApplication.mkis optional, but in case of project using OpenCV, when STL and exceptions are used in C++, it also should be created. Example of the fileApplication.mk:

123
APP_STL := gnustl_staticAPP_CPPFLAGS := -frtti -fexceptionsAPP_ABI := all

Note

We recommend settingAPP_ABI:=allfor all targets. If you want to specify the target explicitly, usearmeabifor ARMv5/ARMv6,armeabi-v7afor ARMv7,x86for Intel Atom ormipsfor MIPS.

Building application native part from command line

Here is the standard way to compile C++ part of an Android application:

Warning

We strongly reccomend usingcmd.exe(standard Windows console) instead of Cygwin onWindows. Use the latter if only you’re absolutely sure about, what you’re doing. Cygwin is not really supported and we are unlikely to help you in case you encounter some problems with it. So, use it only if you’re capable of handling the consequences yourself.

  1. Open console and go to the root folder of an Android application

    cd <root folder of the project>/
  2. Run the following command

    <path_where_NDK_is_placed>/ndk-build

    Note

    On Windows we recommend to usendk-build.cmdin standard Windows console (cmd.exe) rather than the similarbashscript inCygwinshell.

  3. After executing this command the C++ part of the source code is compiled.

After that the Java part of the application can be (re)compiled (using eitherEclipseorAntbuild tool).

Note

Some parameters can be set for thendk-build:

Example 1: Verbose compilation

<path_where_NDK_is_placed>/ndk-build V=1

Example 2: Rebuild all

<path_where_NDK_is_placed>/ndk-build -B

Building application native part fromEclipse(CDT Builder)

There are several possible ways to integrate compilation of native C++ code by Android NDK into Eclipse build process. We recommend the approach based on EclipseCDTBuilder.

Important

OpenCV for Android package since version 2.4.2 contains sample projects pre-configured CDT Builders. For your own projects follow the steps below.

  1. Define theNDKROOTenvironment variable containing the path to Android NDK in your system (e.g."X:\\Apps\\android-ndk-r8"or"/opt/android-ndk-r8").

    On Windowsan environment variable can be set viaMy Computer -> Properties -> Advanced -> Environment variables. On Windows 7 it’s also possible to usesetxcommand in a console session.

    On LinuxandMacOSan environment variable can be set via appending a"exportVAR_NAME=VAR_VALUE"line to the"~/.bashrc"file and logging off and then on.

    Note

    It’s also possible to define theNDKROOTenvironment variable within Eclipse IDE, but it should be done for every new workspace you create. If you prefer this option better than setting system environment variable, open Eclipse menuWindow -> Preferences -> C/C++ -> Build -> Environment, press theAdd...button and set variable name toNDKROOTand value to local Android NDK path.

  2. After that you need torestart Eclipseto apply the changes.

  3. Open Eclipse and load the Android app project to configure.

  4. Add C/C++ Nature to the project via Eclipse menuNew -> Other -> C/C++ -> Convert to a C/C++ Project.

    And:

  5. Select the project(s) to convert. Specify “Project type” =Makefileproject, “Toolchains” =OtherToolchain.

  6. OpenProject Properties -> C/C++ Build, uncheckUsedefaultbuildcommand, replace “Build command” text from"make"to

    "${NDKROOT}/ndk-build.cmd"on Windows,

    "${NDKROOT}/ndk-build"on Linux and MacOS.

  7. Go toBehaviourtab and change “Workbench build type” section like shown below:

  8. PressOKand make sure thendk-buildis successfully invoked when building the project.

  9. If you open your C++ source file in Eclipse editor, you’ll see syntax error notifications. They are not real errors, but additional CDT configuring is required.

  10. OpenProject Properties -> C/C++ General -> Paths and Symbolsand add the followingIncludepaths forC++:

    # for NDK r8 and prior:${NDKROOT}/platforms/android-9/arch-arm/usr/include${NDKROOT}/sources/cxx-stl/gnu-libstdc++/include${NDKROOT}/sources/cxx-stl/gnu-libstdc++/libs/armeabi-v7a/include${ProjDirPath}/../../sdk/native/jni/include
    # for NDK r8b and later:${NDKROOT}/platforms/android-9/arch-arm/usr/include${NDKROOT}/sources/cxx-stl/gnu-libstdc++/4.6/include${NDKROOT}/sources/cxx-stl/gnu-libstdc++/4.6/libs/armeabi-v7a/include${ProjDirPath}/../../sdk/native/jni/include

    The last path should be changed to the correct absolute or relative path to OpenCV4Android SDK location.

    This should clear the syntax error notifications in Eclipse C++ editor.

Debugging and Testing

In this section we will give you some easy-to-follow instructions on how to set up an emulator or hardware device for testing and debugging an Android project.

AVD

AVD (Android Virtual Device) is not probably the most convenient way to test an OpenCV-dependent application, but sure the most uncomplicated one to configure.

  1. Assuming you already haveAndroid SDKandEclipse IDEinstalled, in Eclipse goWindow -> AVD Manager.

  2. Press theNewbutton inAVD Managerwindow.

  3. Create new Android Virtual Devicewindow will let you select some properties for your new device, like target API level, size of SD-card and other.

  4. When you click theCreate AVDbutton, your new AVD will be availible inAVD Manager.

  5. PressStartto launch the device. Be aware that any AVD (a.k.a. Emulator) is usually much slower than a hardware Android device, so it may take up to several minutes to start.

  6. GoRun -> Run/Debugin Eclipse IDE to run your application in regular or debugging mode.Device Chooserwill let you choose among the running devices or to start a new one.

Hardware Device

If you have an Android device, you can use it to test and debug your applications. This way is more authentic, though a little bit harder to set up. You need to make some actions for Windows and Linux operating systems to be able to work with Android devices. No extra actions are required for Mac OS. See detailed information on configuring hardware devices in subsections below.

You may also consult the officialAndroid Developers site instructionsfor more information.

Windows host computer

  1. Enable USB debugging on the Android device (viaSettingsmenu).

  2. Attach the Android device to your PC with a USB cable.

  3. Go toStart Menuandright-clickonComputer. SelectManagein the context menu. You may be asked for Administrative permissions.

  4. SelectDevice Managerin the left pane and find an unknown device in the list. You may try unplugging it and then plugging back in order to check whether it’s your exact equipment appears in the list.

  5. Try your luck installingGoogle USB driverswithout any modifications:right-clickon the unknown device, selectPropertiesmenu item –>Detailstab –>Update Driverbutton.

  6. SelectBrowse computer for driver software.

  7. Specify the path to<AndroidSDKfolder>/extras/google/usb_driver/folder.

  8. If you get the prompt to install unverified drivers and report about success - you’ve finished with USB driver installation.

    ` `
  9. Otherwise (getting the failure like shown below) follow the next steps.

  10. Againright-clickon the unknown device, selectProperties –> Details –> Hardware Idsand copy the line likeUSB\VID_XXXX&PID_XXXX&MI_XX.

  11. Now open file<AndroidSDKfolder>/extras/google/usb_driver/android_winusb.inf. Select eitherGoogle.NTx86orGoogle.NTamd64section depending on your host system architecture.

  12. There should be a record like existing ones for your device and you need to add one manually.

  13. Save theandroid_winusb.inffile and try to install the USB driver again.

    ` `

    ` `

  14. This time installation should go successfully.

    ` `

  15. And an unknown device is now recognized as an Android phone.

  16. Successful device USB connection can be verified in console viaadbdevicescommand.

  17. Now, in Eclipse goRun -> Run/Debugto run your application in regular or debugging mode.Device Chooserwill let you choose among the devices.

Linux host computer

By default Linux doesn’t recognize Android devices, but it’s easy to fix this issue. On Ubuntu Linux you have to create a new/etc/udev/rules.d/51-android.rulesconfiguration file that contains information about your Android device. You may find some Vendor ID’shereor executelsusbcommand to view VendorID of plugged Android device. Here is an example of such file for LG device:

SUBSYSTEM=="usb", ATTR{idVendor}=="1004",  MODE="0666", GROUP="plugdev"

Then restart your adb server (even better to restart the system), plug in your Android device and executeadb devicescommand. You will see the list of attached devices:

Mac OS host computer

No actions are required, just connect your device via USB and runadbdevicesto check connection.


更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. FFmpeg的Android平台移植—编译篇
  2. activity设置背景色为透明
  3. Android 通过WebView来播放flash在线视频
  4. Android Studio中设置ButterKnife、andro
  5. ViewPager的定时滚动,动态加载数据
  6. android Uri获取真实路径转换成File的方
  7. Android如何在java代码中设置margin
  8. android 加边框
  9. android panellistview 圆角实现代码
  10. 关于android分辨率兼容(屏幕适配)问题