Compile and debug the android ndk project using eclipse in Ubuntu

Source: Internet
Author: User
Tags gdb debugger

I. Development Environment Construction:
1. First, install CDT for Eclipse:
 Choose [help]-> [install newsoftware...],
 The "Install" window is displayed,
 Click Add to bring up the "add repository" window,
 Enter CDT in name: text box,
 Enter http://download.eclipse.org/tools/cdt/releases/juno in location: Text Box
 Click OK to save,
 
 Select work with: In the following drop-down box:
 CDT-http://download.eclipse.org/tools/cdt/releases/juno
 
 Select both cdt main features and cdt optional features,
 Click Next> and click Install as prompted!
 


2. Then, install Sequoyah for Eclipse:
 Choose [help]-> [install newsoftware...],
 The "Install" window is displayed,
 Click Add to bring up the "add repository" window,
 In the Name: text box, enter Sequoyah,
 Enter http://download.eclipse.org/sequoyah/updates/2.0 in location: Text Box
 Note: You can get the latest address at http://www.eclipse.org/sequoyah/downloads.
 Click OK to save,
 
 Select work with: In the following drop-down box:
 Sequoyah-2.0 http://download.eclipse.org/sequoyah/updates/2.0
 
 Note: You must remove the check box "group items by category,
 Otherwise it will be displayed as "there are no categorized items "!!!
 
 Select [Sequoyah Android localization Editor] and [Sequoyah Android native code support,
 Click Next> and click Install as prompted!


 
3. Finally, set the Native Development ndk location:
 Choose [Window]> [Preferences],
 The "Preferences" window is displayed,
 In the left-side pane, choose Android> Local Development,
 In the right pane, click the Browse... button and select the directory after ndk decompression,
 Click the Apply button in the lower-right corner to save the settings.



4. comment out the last sentence of the ndk-GDB script under the ndk directory!
 # $ Gdbclient-x 'native _ path $ gdbsetup'

5. Add the full path of the android-ndk-r8b and Android-sdks/platform-tools to the PATH environment variable:
 $ Sudo gedit/etc/environment
 Note: after adding the variable, use $./etc/environment to reload the new path variable value.
 Because ndk-build, ndk-GDB, ADB and other scripts or tools need to be called.

Ii. Development Environment usage:


1. Create or import an android project testjnipro, modify the Java code, and reference a local library function, for example:
 @ Override
 Public void oncreate (bundle savedinstancestate ){
     Super. oncreate (savedinstancestate );
     Setcontentview (R. layout. activity_orange );
     New alertdialog. Builder (this). setmessage (sayhello (). Show ();
 }

 Public native string sayhello (); // Local Database Function
 Static
 {
     System. loadlibrary ("nativecode ");
 }
 
2. Right-click the project name and choose [Android tools]> [add nativesupport...] From the shortcut menu.
 The "add Android local support" window is displayed,
 Check whether the ndk position is correct,
 Modify the library name (excluding lib and. So) in the text box lib *. So to add the name you want (such as nativecode ),
 Click Finish,
 A jni directory should be added to the project directory, which contains two file libraries named. cpp and Android. mk.
 (For example, nativecode. cpp)



 
3. Enter the JNI directory on the terminal and run the following command to create a JNI-style header file:
 $ Javah-classpath ../bin/classeswzh. nsc. testjniproactivity
 Note: The testjniproactivity class references the local library function, so it is used to create a JNI-style header file.

4. Add the code to the nativecode. cpp file:
 # Include "wzh_nsc_orangeactivity.h"
 
 Jniexport jstring jnicall
 Java_wzh_nsc_orange_orangeactivity_sayhello (jnienv * ENV,
                                   Jobject this)
 {
     Return env-> newstringutf ("Hello, I'm an demo! ");
 }

5. compile the project. If the following error occurs:
 Method 'newstringutf' could not beresolvedSource generation file name. c/Testjnic/JNILine 6Semanticerror
 Type 'jnienv' cocould not beresolved        Source generation file name. c/Testjnic/JNILine4Semantic Error
 Type 'jobobject' cocould not beresolved       Source generation file name. c/Testjnic/JNILine4Semantic Error
 Type 'jstring' cocould not beresolved       Source generation file name. c/Testjnic/JNI
 
 Solution:
 Right-click the project name and choose [properties] From the shortcut menu.
 The "properties for project name" window appears,
 In the left pane of the window, select C/C ++ general-> pathsand symbols,
 Select the des option page in the right window,
 In the language column
 Gnu c and gnu c ++ add absolute path/android-ndk-r8b/platforms/Android-8/arch-arm/usr/include


6. Create an android Virtual Machine and run it.

7. Right-click the project name and choose [run as]> [1 Android Application] from the menu.
 To test whether the above program can run normally.
 Note: if an error similar to the following occurs on the Problems option page:
 Make: *** no rule tomake target 'Project name_scd. mk '.Stop.Project name [discovery options] Page in projectpropertiesC/C ++ Problem
 
 Solution:
 Right-click the project name and choose [properties] From the shortcut menu.
 The "properties for project name" window appears,
 In the left pane of the window, select C/C ++ build,
 Select the buildersettings option page in the right window,
 Remove the check box of use default buildcommand,
 Modify build command: ndk-build in the following text box is the full path!
 Select the behaviour option page in the right window,
 After modifying build (incrementalbuild), remove v = 1 to make it an empty text box!

 NOTE: If online debugging still persists, right-click it and choose Delete!



8. Modify the project configuration file androidmanifest. xml and set the debuggable flag to true.


9. Enter the project directory in the terminal and run the ndk-GDB command,
 New files such as app_process (executable program) and GDB. Setup (configuration file) will be generated in OBJ/local/armeabi.
 Note: When performing this step, make sure that the program is running in the simulator.
 
 Right-click the project name and choose [debug as] or [run]> [debug events...] From the shortcut menu.
 The "Debug configurations" window is displayed,
 Double-click C/C ++ application in the left pane of the window. The project name default option is displayed. Click to select it,
 Select the main option page in the right window,
 Click the Browse... button,
 
 Modify C/C ++ application: the text box below is/obj/local/armeabi/app_process under the project directory.
 Note: This file exists for debugging. If you cannot find this file, you should first run ndk-GDB in the project directory.
 
 Project: select the current project in the text box below. You do not need to modify the project. The current project is used by default.
   Modify  Check the selectconfiguration using 'C/C ++ application' check box;  Disableauto build single worker is selected;


   Click using GDB (DSF) CreateProcess launcher-select other...
 The Select preferredlauncher window is displayed,
 Check the use configurationspecific settings change workspace settings... check box,
 Select launchers: Standard create process launcher in the list box
 


 Select the debugger option page in the right window,
 Modify Debugger: the drop-down list box option is gdbserver.

 Remove the check box before stop on startup at: Main.
 Modify the value of gdb debugger: in the main option page of the debugger Options Group:
 Absolute directory/android-ndk-r8b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/ARM-Linux-androideabi-GDB
 Note: many files in this path are targeted at other platforms.
 
 Modify the value of the text box after GDB command file: On the main option page of the debugger Options Group:
 /Obj/local/armeabi/gdb2.setup under the project directory
 Note: copy/obj/local/armeabi/GDB. setup in the project directory and name it gdb2.setup,
 Remove target remote: 5039 from the last line of gdb2.setup.
    
 The reason for pointing to GDB. setup is not set:
 Each time ndk-GDB is called, a new GDB. Setup will be generated to overwrite the modified GDB. setup.
 
 In the main option page of the debugger Options Group
 Check the verbose Console mode and use full file path to set breakpoints check boxes,
 Note: commands can be used in the eclipse console to interact with GDB only after verbose consolemode is checked.
 
 On the connection option page under the debugger Options Group
 Modify type: the value of the drop-down list box is TCP.
 Modify host name or IPaddress: the value of the text box is localhost.
 Modify port number: the value of the text box is 5039.
 (Note: This value is set for port number because target remote: 5039 is the last row of GDB. setup .)
 
 Click Apply in the lower-right corner of the current window to save the application.

 Note: You must click the debug button. Otherwise, testjnipro_default will not appear in the menu!
 
10. Now you can set a breakpoint for the code. You must set a breakpoint before and after calling the local library function!
 Note: It is necessary to set aside time for running ndk-GDB.
  Start formal debugging:
  First, run the Java debugging of the project. The program stops running at the breakpoint before calling the local library function,
  In this case, run the terminal to enter the project directory and run ndk-GDB without any output,
  Then start debug of C/C ++, that is, the one configured above:
  Right-click the project name and choose [debug as] or [run]> [debug events...] From the shortcut menu.
  The "Debug configurations" window is displayed,
  Double-click C/C ++ application in the left window to display the project name default option!

  
  The android simulator can normally use the permissions of superusers,
  When debugging with a real machine device, make sure that your real machine device is already root,
  Otherwise, some permission errors may occur, and even cause inexplicable problems...

For codes with no errors or warnings that can be compiled successfully by ndk-build, eclipse prompts the following errors:
"Symbol is not resolved"
"Member declaration not found"
"Invalid template argument"
"Invalid arguments"
"Method cannot be resolved"
And so on.

My solution may not be perfect, but at least it will let you click to run or debug through Eclipse:
1. Right-click your android project and select Properties;
2. Under C/C ++ General, click the code analysis item;
3. Select the Use Project Settings option box;
4. compile C ++-related files under syntax and semantic errors, but eclipse reports errors from errors to warnings.

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.