Android ndk r4b Development Environment

Source: Internet
Author: User

Original: http://yarin.blog.51cto.com/1130898/382109

 

Resource preparation
Note: This article takes the MAC system as an example, so the software to be prepared must be suitable for the Mac version.
Android SDK: android-sdk-mac_86
Android ndk: android-ndk-r4b-darwin-x86
Eclipse
ADT
CDT
Ant
Build an android SDK Development Environment
Steps for setting up the android SDK development environment:
1: JDK installation, which must be later than 1.5. JDK has been pre-installed in General Mac OS, and JDK 1.6 is mine. You can view it in terminal using "Java-version.
2: Install Eclipse IDE. you need to go to the eclipse official website to download eclipse 3.5.2. decompress the package to a proper path and run eclipse in the directory without compiling and installation. the app file can start eclipse.
3: Install the android SDK. Similar to eclipse, you only need to extract it to the appropriate directory. At the same time, you need to add the tools path under the directory to the path. Edit ~ /. Bash_profile, add: path =$ {path }:/ {Your-Android-SDK}/tools.
4: Install Android Developer Tools. open eclipse, click the Help menu, find install new sofeware, add a location in add site: http://dl-ssl.google.com/android/eclipse/, then Android ddms, Android development tools, and select and install. after the restart, you can select "eclipse-> preference settings", and then select the android item to specify the installation directory of the android SDK. as shown in.

5: Enable terminal and use "android list targets" to find that no SDK target is available. you need to open the android SDK and AVD manager window in the window menu of Eclipse, find avilable packages, and select related packages. as shown in.

6: Wait for the download. After the download is complete, you can find the package you have installed in install packages, as shown in.

7: Use "android create AVD -- name sdk_2_version -- Target 8" to create AVD. After the AVD is created, you can find the created AVD in "Virtual Devices", as shown in.

Now you can write helloworld to test the environment. Here, the author places eclipse and Android SDK in the "/developer/Android/" directory when setting up the SDK development environment, as shown in.

Build an android ndk Development Environment
As you can see from the above picture, we actually installed Android ndk in the same directory. The installation of ndk is very simple, that is, to extract the downloaded package to the specified location, after decompression, we usually need to configure the environment variables. Here we put the configuration of the environment variables together for configuration.
Set Environment Variables
Run "Pico. bash_profile" to open or create the. bash_profile file, and then enter the code such.

Run "Control + x" and enter "Y" to save and exit (you must exit and re-open ), the tools directory of the SDK and the installation directory of the ndk are added to the environment variables for ease of use. The android_ndk_root and android_sdk_root directories are also set.
Here we have completed the ndk environment, Android ndk R4, we do not need to use the "build/host-setup.sh" command, then we will introduce how to use ndk to compile. the examples provided by ndk are not placed under the apps directory, but in samples. For compatibility, project files can also be placed under the apps directory. next let's take a look at how to compile, run, and tune.
Compile the hello-JNI Project
Go to the "samples/Hello-JNI/" directory under the ndk and run the "ndk-build" command to start compilation. If it appears, it indicates that the compilation is successful. if it fails, it may be that the environment is not correctly set. Check and reset it.

Run the hello-JNI Project
Next, we will first generate the APK file, and use ant to generate the APK file. Because ant is pre-installed on the Mac system, we will be able to install it without installation, after installation, add the bin directory bought under the ant directory to the environment variable.
Use "CD .. /"command to return the previous directory of the hello-JNI directory, and then use the" android update project -- path hello-JNI "command to update the hello-JNI project, which is to generate a build. XML file to compile and generate the APK file. after the project is updated, enter the root directory of the project and run the CD hello-JNI command. Next, compile the APK file and run the "ant debug" command, if so, the compilation is successful.

Now you can find a generated APK file in "samples/Hello-JNI/bin". Now you can connect to your mobile phone or simulator to test the file.
Tune the hello-JNI Project
At the beginning of Android ndk R4, we can use GDB to perform one-step debugging on JNI code. We will not talk about GDB debugging. Search for it online. Here we will look at how to debug it.
Go to the "samples/Hello-JNI/" directory and run the "ndk-GDB" command. Then, whether the hello-JNI project of your mobile phone or simulator has been started, the terminal also enters the gdb debugging interface, as shown in ., you must set debug to true in XML for debugging.

Integrate ndk development in eclipse
Because most people may use Notepad for some code, why not use a good ide? Here we use eclipse to compile the ndk program. Before we start, we need to make some necessary configurations.
1. Install CDT to enable eclipse to edit the C ++ program.
2: We recommend that you install the anyedit plugin.
In logs.
3: Create an ndk project. Here we will use hello-JNI as an example to demonstrate it. We will not create a new project separately and import hello-JNI to eclipse. follow these steps:
Right-click the project and select Properties"
Select "builders" in the list on the left"
Click "new..." on the right ..." Button
In the list box, select "program" as the configuration type. Click "OK" to go to the next configuration dialog box.
Name "ndk builder"
Select "/bin/bash" as the location. If cygwin is used in windows, enter "C:/cygwin/bin/bash.exe", which indicates that cygwin is installed in C:/cygwin, this path can be changed as needed.
"Working directory" is "/bin". Similarly, if cygwin is used on Windows, enter "C:/cygwin/bin ".
"Arguments" is:
-- Login-c "CD $ android_ndk_root/samples/Hello-JNI & ndk-build"
Make sure that there are two connection numbers before login, which are enclosed by quotation marks (-C. Android_ndk_root is the ndk installation path .... As shown in.

Next, open the "refresh" tab:
Select refresh resources upon completion"
Select Specific resources"
Click "specify resources" and select the lib directory of your project.
Select "recursively include sub-Folders"
As shown in.

Next, open the "build options" tab:
Select "allocate console"
Select "launch in background"
Select "run the builder after a clean"
Select "run the builder during manual builds"
Select "run the builder during auto builds"
Select specify working set of relevant resources"
Click "specify resources", select the JNI directory of your project, and include all the files.
As shown in.

Click "OK. After the configuration is completed, eclipse automatically calls ndk to compile the C ++ code in ecisplse and outputs the compilation information on the console, as shown in. then the generated so file is automatically updated.

The processing here is not necessary, but for the convenience of development. of course, there may be better methods for project requirements. As the project progresses, we will update some problems encountered during development. write it here first. Thank you for your support!
In fact, if OpenGL ES program is developed in ndk, you can use tools such as Vc in Windows to develop it first, and then use ndk to compile it. This makes debugging much easier.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.