How-to debug native code with Android

writing about global warming This is a step by step guide for executing and debugging native code on Android with Eclipse.

1. Prerequisites

http://www.siliconp.de/?university-entrance-essay university entrance essay The SDK version used for this guide is Froyo with the NDK r4b ( crystax release ).
Also Eclipse CDT plugin it’s very useful for our purposes, so install it.
Last plugin to install it’s the Sequoyah plugin for Eclipse.

2. Project setup

http://reformhistoricfrederick.com/master-thesis-facility-management/ master thesis facility management At this point let’s create a new Android project, name it “Example” and use the “com.darkwavegames.com” package name, add also an Activity name of your choice.
Select Android 2.2 as base SDK version and complete the project wizard.

http://robijyotisikshaniketan.com/frankenstein-research-paper/ frankenstein research paper Now you need to add the native support for the newly created project. Just right click on the project root element in the package explorer and select “add native support”.

http://www.gutailong.com/essay-on-eating-disorders-in-athletes/ essay on eating disorders in athletes In the next dialog write the path of your NDK folder and give also name for your library.
After this operation a new folder “jni” will be created with a .cpp file, header filer and an Android makefile, Android.mk, which can be edited to modify all the includes, linker and compiler options. In the Android.mk file you also need to specify all the source file you want to use within the LOCAL_SRC_FILE directive.

3. Debug

http://kaya-izolasyon.com/?i-need-someone-to-write-my-homework i need someone to write my homework In order to enable debug of native code in Android you have to face different problems, based also on the device and the firmware version, and if the native code is multi thread or single thread.
First of all you need to mofify the http://www.gokcentunc.com/essay-on-feminism/ essay on feminism AndroidManifest.xml file adding the attribute “debuggable” to true ( remember also to enable the “Debug USB” option under the Application device menu ).
At this point you can debug all the java code within your eclipse debugger, for for the native C debug you need more steps.

http://atakkumas.com.tr/dissertation-proquest/ dissertation proquest Continue reading

Reading resource files from native code

http://sweetsmilesdentistry.com/research-papers-service/ research papers service If you’re working with android-ndk and you want to open an asset included in your apk package, you should make a request via jni to the Resource manager in your java code.
Something like:

phd thesis in knowledge management

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public int[] openAsset( String path )
{
   AssetFileDescriptor ad = null;
   try
   {
      ad = getResources().getAssets().openFd( path );
      Integer off = (int) ad.getStartOffset();
      Integer len = (int) ad.getLength();
      int res[] = { off, len };
      ad.close();
      return res;
   } catch( IOException e ) {
      Log.e( TAG, e.toString() );
   }
   return null;
}

http://www.il-bandito.com/dma-thesis-help/ dma thesis help However this is not a optimal solution expecially when you have tons of file io operations, also because I prefer to limit the amount of calls between java and C++.
A better solution I’m using is to open the current apk application using the libzip library. Just download the library and compile as static library, then include it in your Android.mk make file under the http://www.dilmah.pl/?student-login-discovery-education student login discovery education LOCAL_STATIC_LIBRARIES section.
What you have to do first is to send the current application filename from java, once at startup, in this way:

http://asiagemtv.com/chemistry-a-level-coursework-help-lang-en/ Chemistry A Level Coursework Help Lang En

1
2
3
4
5
6
7
8
PackageInfo info = null;
try {
   info = getContext().getPackageManager().getPackageInfo("com.example.text", 0);
} catch( NameNotFoundException e ) {
   Log.e( TAG, e.toString() );
   return;
}
setAppName( info.applicationInfo.sourceDir );

Essays On Friendship and in the C++ code you will have the corresponding research proposal method setAppName method:

Literature Review About Customer Service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <zip .h>
#include <zipint .h>
#include <jni .h>

char packageName[1024] = {0};
zip   *pkg_zip;

JNIEXPORT void JNICALL Java_com_example_test_MySurfaceView_setAppName( JNIEnv * env, jobject obj, jstring pkgname )
{
   const char *buffer = env->GetStringUTFChars( pkgname, false );

   int error;
   pkg_zip = zip_open( buffer, 0, &error );
   strcpy( packageName, buffer );
   if( pkg_zip == NULL ){
      LOGE("Failed to open apk: %i", error );
   }
   env->ReleaseStringUTFChars( pkgname, buffer );
}

Uk Buy Essay

Now you have a reference to the zip package. In this way now you can query the zip package asking if a particular file exists:

1
2
3
4
5
6
7
8
9
bool FileExists(const char* fname)
{
   if( pkg_zip != NULL )
   {
      int result = zip_name_locate( pkg_zip, fname, 0);
      return result != -1;
   }
   return false;
}

And open any of the files included in your apk package:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
FILE *FileOpen( const char* fname )
{
   zip_file *zfile = zip_fopen( pkg_zip, fname, 0 );
   uint32_t offset = 0;
   uint32_t length = 0;

   if( zfile != NULL )
   {
      offset = zfile->fpos;
      length = zfile->bytes_left;
      zip_fclose( zfile );
      zfile = NULL;
   } else
   {
      return NULL;
   }

   FILE *fp = NULL;
   fp = fopen( packageName, "rb" );
   fseek( fp, offset, SEEK_SET );
   return fp;
}

Using this method you will have a FILE pointer to the whole apk file so you have to take care of the help with writing my essay offset and generic cialis pills length field when reading and seeking the file itself.