2012年4月11日 星期三

Android JNI debugging...


1. Add debug symbols when compiling...
add "LOCAL_CFLAGS := -g" in /jni/Android.mk file -


2. Set .apk file debuggable ...
in AndroidManifest.xml, set Debuggable = true













3. In cygwin, add adb into PATH variable (if you're using windows) ...
Modify /home//.bash_profile
example:

4. Clean the project ...
1) in eclipse -
2) in cygwin, ndk-build clean

5. compile the .c files

6. In windows cmd.exe, type: "android update project -p . -s"

7. in eclipse IDE, press the debug button to start debugging ...
Debug configurations must be set properly first.
8. When the application is up and running, type ndk-gdb in cygwin shell...
You will see the following...

Reference:







2012年4月5日 星期四

out of memory exception:

Well, "outofMemoryExceptions" should not be handled, because technically, they can't.
Instead of catching these kind exceptions at low level in the code, they should be thrown up the higher layer, i.e. UI level, so that UI layer is aware of the problem, and user is informed that the app is about to exit.


public class LowLevelClass
{
public void complicatedMethod() throws OutOfMemeoryException
{
throw new OutOfMemeoryException("huh, not so good.");
}
}

public class MiddleLevelClass
{
public void doSomethingNotSoComplicated() throws OutOfMemeoryException
{
LowLevelClass low = new LowLevelClass();
try
{
low.complicatedMethod();
}
catch(OutOfMemeoryException oom)
{
doSomethingNecessary();
throw oom;
}
}
}

public class UILevelClass
{
public void showView throws OutOfMemeoryException
{
MiddleLevelClass mid = new MiddleLevelClass();
try
{
mid.doSomethingNotSoComplicated();
}
catch(OutOfMemeoryException oom)
{
App.reportException(oom);
showDialog("Not enough memory! This App is about to close. Please restart.", oom.getMessage());
}
}
}

[Java] Null pointer handlings

In my previous project, I used to do this when handling the null pointers -
if (obj != null)
{
doSomething();
}


But I was told to do something like this to have a better result -

try
{
doSomething();
}
catch(NullPointerException npe)
{
App.reportException(npe);
}

Unless, the object is allow to have null as a legitimate return value, we can do this -

Bitmap bitmap = ResourceLoader.get(file);
if (bitmap == null)
{
loadBitmapInAnotherWay(file);
}

Well, all these code are conceptually working. You will have to work it out in your own way, according to your architecture. Copying and Pasting these code into yours will just bring you compiling errors. :)