Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Android NDK Beginner`s Guide - Second Edition

You're reading from   Android NDK Beginner`s Guide - Second Edition Discover the native side of Android and inject the power of C/C++ in your applications

Arrow left icon
Product type Paperback
Published in Apr 2015
Publisher Packt
ISBN-13 9781783989645
Length 494 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Sylvain Ratabouil Sylvain Ratabouil
Author Profile Icon Sylvain Ratabouil
Sylvain Ratabouil
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Setting Up Your Environment FREE CHAPTER 2. Starting a Native Android Project 3. Interfacing Java and C/C++ with JNI 4. Calling Java Back from Native Code 5. Writing a Fully Native Application 6. Rendering Graphics with OpenGL ES 7. Playing Sound with OpenSL ES 8. Handling Input Devices and Sensors 9. Porting Existing Libraries to Android 10. Intensive Computing with RenderScript 11. Afterword Index

Time for action – setting up an Android device

Device configuration is dependent on your target OS. To do so:

  1. Configure your device driver on your OS if applicable:
    • If you use Windows, installation of a development device is manufacturer-specific. More information can be found at http://developer.android.com/tools/extras/oem-usb.html with a full list of device manufacturers. If you have a driver CD with your Android device, you can use it. Note that the Android SDK also contains some Windows drivers under $ANDROID_SDK\extras\google\usb_driver. Specific instructions are available for Google development phones, Nexus One, and Nexus S at http://developer.android.com/sdk/win-usb.html.
    • If you use OS X, simply connecting your development device to your Mac should be enough to get it working! Your device should be recognized immediately without installing anything. Mac's ease of use is not a legend.
    • If you are a Linux user, connecting your development device to your Distribution (at least on Ubuntu) should be enough to get it working too!
  2. If your mobile device runs Android 4.2 or later, from the application list screen, go to Settings | About phone and tap several times on Build Number at the end of the list. After some efforts, Developer options will magically appear in your application list screen.

    On Android 4.1 devices and earlier, Developer options should be visible by default.

  3. Still on your device, from the application list screen, go to Settings | Developer options and enable Debugging and Stay awake.
  4. Plug your device into your computer using a data connection cable. Beware! Some cables are charge-only cables and will not work for development! Depending on your device manufacturer, it may appear as a USB disk.

    On Android 4.2.2 devices and later, a dialog Allow USB debugging? appears on the phone screen. Select Always allow from this computer to permanently allow debugging and then click on OK.

  5. Open Command Prompt and execute the following:
    adb devices
    
    Time for action – setting up an Android device

    On Linux, if ????????? appears instead of your device name (which is likely), then adb does not have proper access rights. A solution might be to restart adb as root (at your own risk!):

    sudo $ANDROID_SDK/platform-tools/adb kill-server
    sudo $ANDROID_SDK/platform-tools/adb devices
    

    Another solution to find your Vendor ID and Product ID may be needed. Vendor ID is a fixed value for each manufacturer that can be found on the Android developer website at http://developer.android.com/tools/device.html (for example, HTC is 0bb4). The device's Product ID can be found using the result of the lsusb command in which we look for the Vendor ID (for example, here 0c87 is HTC Desire product ID):

    lsusb | grep 0bb4
    
    Time for action – setting up an Android device

    Then, with root privilege, create a file /etc/udev/rules.d/51-android.rules with your Vendor ID and Product ID and change file rights to 644:

    sudo sh -c 'echo SUBSYSTEM==\"usb\", SYSFS{idVendor}==\"<Your Vendor ID>\", ATTRS{idProduct}=\"<Your Product ID>\", GROUP=\"plugdev\", MODE=\"0666\" > /etc/udev/rules.d/52-android.rules'
    sudo chmod 644 /etc/udev/rules.d/52-android.rules
    

    Finally, restart the udev service and adb:

    sudo service udev restart
    adb kill-server
    adb devices
    
  6. Launch Eclipse and open the DDMS perspective (Window | Open Perspective | Other...). If working properly, your phone should be listed in the Devices view.

    Tip

    Eclipse is a compound of many views, such as the Package Explorer View, the Debug View, and so on. Usually, most of them are already visible, but sometimes they are not. In that case, open them through the main menu by navigating to Window | Show View | Other…. Views in Eclipse are grouped in Perspectives, which store workspace layout. They can be opened by going to Window | Open Perspective | Other…. Beware that some contextual menus may be available only in some perspectives.

What just happened?

Our Android device has been switched into development mode and connected to our workstation through the Android Debug Bridge daemon. ADB gets started automatically the first time it is called, either from Eclipse or the command line.

We also enabled the Stay awake option to stop automatic screen shutdown when the phone charges, or when developing with it! And, more important than anything, we discovered that HTC means High Tech Computer! Jokes apart, connection process can be tricky on Linux, although little trouble should be encountered nowadays.

Still having trouble with a reluctant Android device? That could mean any of the following:

  • ADB is malfunctioning. In that case, restart the ADB deamon or execute it with administrative privilege.
  • Your development device is not working properly. In that case, try restarting your device or disabling and re-enabling development mode. If that still does not work, then buy another one or use the emulator.
  • Your host system is not properly set up. In that case, check your device manufacturer instructions carefully to make sure the necessary driver is correctly installed. Check hardware properties to see whether it is recognized and turn on USB storage mode (if applicable) to see whether it is properly detected. Please refer to your device documentation.

    Tip

    When the charge-only mode is activated, SD card files and directories are visible to the Android applications installed on your phone but not to your computer. On the opposite side, when disk drive mode is activated, those are visible only from your computer. Check your connection mode when your application cannot access its resource files on an SD card.

More about ADB

ADB is a multi-facet tool which is used as a mediator between the development environment and devices. It is composed of:

  • A background process running on emulators and devices to receive orders or requests from your workstation.
  • A background server on your workstation communicating with connected devices and emulators. When listing devices, the ADB server is involved. When debugging, the ADB server is involved. When any communication with a device happens, the ADB server is involved!
  • A client running on your workstation and communicating with devices through the ADB server. The ADB client is what we interacted with to list devices.

ADB offers many useful options among which some are in the following table:

Command

Description

adb help

To get an exhaustive help with all options and flags available

adb bugreport

To print the whole device state

adb devices

To list all Android devices currently connected including emulators

adb install [-r] <apk path>

To install an application package. Append -r to reinstall an already deployed application and keep its data

adb kill-server

To terminate the ADB daemon

adb pull <device path> <local path>

To transfer a file to your computer

adb push <local path> <device path>

To transfer a file to your device or emulator

adb reboot

To restart an Android device programmatically

adb shell

To start a shell session on an Android device (more on this in Chapter 2, Starting a Native Android Project)

adb start-server

To launch the ADB daemon

adb wait-for-device

To sleep until a device or emulator is connected to your computer (for example, in a script)

ADB also provides optional flags to target a specific device when several are connected simultaneously:

-s <device id>

To target a specific device by its name (device name can be found with adb devices)

-d

To target the current physical device if only one is connected (or an error message is raised)

-e

To target the currently running emulator if only one is connected (or an error message is raised)

For example, to dump the emulator state when a device is connected at the same time, execute the following command:

adb -e bugreport

This is only an overview of what ADB can do. More information can be found on the Android developer website at http://developer.android.com/tools/help/adb.html.

You have been reading a chapter from
Android NDK Beginner`s Guide - Second Edition
Published in: Apr 2015
Publisher: Packt
ISBN-13: 9781783989645
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image