Archive for October, 2009

Android NDK – an introduction (How to work with NDK)

What is the Android NDK?

The Android NDK is a companion tool used only in conjunction with Android SDK which allows application developers to build performance-critical portions of their apps by use of native (C/C++) code.

This provide benefits in form of reuse of existing code and increased speed.

Contents of NDK:

  • A set of tools and build files for generating native code libraries from C and C++ sources.
  • A way to embed the corresponding native libraries into application package files (.apks) that can be deployed on Android devices.
  • A set of native system headers and libraries supported starting from Android 1.5 .
  • Documentation, samples, and tutorials.

Download NDK from http://developer.android.com/sdk/ndk/1.6_r1/index.html and uncompress it.Now run the script in <NDK>/build/host-setup.sh.This creates <NDK>/out/host/config.mk which configure NDK for system.

How to write an application using NDK?

Follow underneath steps to create our own application using NDK and make it running on emulator.

Write an Android Activity

You need to include following lines of extra code in our java file.

  • Call native method inside on create method.
  • Declare native method outside oncreate method.
  • Call to “System.loadLibrary” method inside a static initializer block to load our shared library object.

package com.example.FileLoaderActivity;

public class FileLoaderActivity extends Activity {

private static String TAG = “FileLoaderActivity”;

// The name of a test file to write

private static String FILENAME = “hello”;

// The full path to the test file

private static String PATH = “/data/data/com.example.FileLoaderActivity/” + FILENAME;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

……………

// Call the native method to read the file just written

byte[] bytes = loadFile(PATH);

…………..

}

public native byte[] loadFile(String fileName); static {

System.loadLibrary(“fileloader”);

}

}

Generating C/C++ header files For generating C/C++ header file for our shared library:

  • Build project which generates Java.class files under <project_home>/bin
  • Use javah tool to generate a C/C++ header file.

Issue the following command at the top of <project_home>, my command looks like:

javah -o fileloader.h -classpath bin com.example.FileLoaderActivity.FileLoaderActivity

The above command will generate a file called fileloader.h .

Creating our Shared Library

Create two folders which will contain source code and make files:

  • mkdir -p ~/workspace/<project_home>/jni foldercontain the source code for our shared library.
  • mkdir -p $NDK_HOME/apps/fileloader folder- “project definition” folder for our shared library

Now copy header file ‘hellondk.h’ from Eclipse project folder to ~/workspace/<project_home>/jni/

For building our NDK project, we need to create two Makefiles which will specify how and from where to pick source code and library file.

  • Android.mk
  • Application.mk

~/workspace/<project_home>/jni/Android.mk

Android.mk file describes source file to build system and its contents are :

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := fileloader

LOCAL_SRC_FILES := fileloader.c

LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog

include $(BUILD_SHARED_LIBRARY)

$NDK_HOME/apps/fileloader/Application.mk

Application.mk file describes modules that are needed by your application and it looks like:

APP_PROJECT_PATH := ~/workspace/FileLoaderActivity

APP_MODULES := fileloader

Creating Shared Library Source

Now create C source file for shared library which defines JNI method called by Java file.Signature of this Jni method should match with fileloader.h file generated by eclipse build system.

~/workspace/FileLoaderActivity/jni/fileloader.c

jbyteArray Java_com_example_FileLoaderActivity_FileLoaderActivity_loadFile

(JNIEnv* env, jobject thiz, jstring filename)

{

……………………..

}

Compiling our Shared Library

Change current working directory to $NDK_HOME and run command “make APP=fileloader”.You can find your shared library into folder $NDK_HOME/out/apps/fileloader/ directory.You can now run our FileLoaderActivity project from within Eclipse. Dont forget to push file “hello” mentioned in java code before running project.Here’s a screenshot of what you should see in the emulator.

device.png


You are NOT an iPhone developer

After interviewing several iPhone developers here in Bangalore, I am really frustrated. We already have a team of iPhone developers (who are the best) and now we are trying to add a couple more people to the team. But we have not been able to hire even 1 person after more than 40 interviews. And we have interviewed candidates from all types of companies, big (the top 10) and small.

Well, lets get this straight, you are NOT an iPhone developer if:

  • you have no idea why you write “nonatomic” and “retain” in all properties. And you’ve never tried to find out.
  • you cannot tell the difference between release and autorelease
  • you hear the terms “main run loop” or “event loop” for the first time during the interview
  • you think all classes are either views or controllers
  • you have never written code for “applicationDidReceiveMemoryWarning” and “didReceiveMemoryWarning”
  • you do not know if you should release objects you created by through “copy” or “mutableCopy”
  • the terms build configurations and provisioning profiles do not ring a bell
  • you have never even seen the iPhone HIG (Human Interface Guidelines) document
  • you think both “viewDidLoad” and “loadView” are same methods
  • you have never used breakpoints and the debugger
  • you have never used the Instruments Application to monitor memory or network usage
  • you do not know how to read crash logs or how to add symbols to crash logs
  • even after programming for iPhone, you do not know about the Model-View-Controller pattern or the delegate pattern

And in case you are just starting off with iPhone programming, it would be wise to know the basics of both objective-C and the iPhone SDK. Don’t skip reading at least the following:


How we fixed production push notifications not working while sandbox works

We have faced the issue of push notifications working seamlessly over sandbox but never work on production. This has happened more than once.

I still have not investigated the root cause but the following fixes it.

We did not do anything extra or change any code or project build configuration. Here is what we did:

  1. Restored our iPhone/iPod Touch devices by erasing everything and reinstalling the OS. Did not sync the device or restore old data to avoid putting in the provisioning profiles again.
  2. Deleted all certificates and private keys from keychain on the mac (all including the Apple provider certificates)
  3. Removed all provisioning profiles from xcode.
  4. From the developer portal, we deleted the adhoc and dev provisioning profiles
  5. From the developer portal, we revoked the push SSL certificates for both dev and production versions
  6. Then we regenerated the push SSL certificates, both production and dev. Note that we did not download them from the wizard itself at this time.
  7. We then waited for a few minutes (5-10 mins) and then generated the provisioning profile. Not downloaded yet.
  8. We again waited a few minutes (5-10 mins) and then downloaded the certificates (sandbox and production) and the profiles (dev and adhoc)
  9. We then generated the single PEM file from the certificates and the key only for the production version. We did not even install the sandbox version certificates in our keychain.
  10. Then installed the adhoc provisioning profile in xcode and the device
  11. Started the application and tested a push notification
  12. It worked

They key difference is that we generate and download the dev profiles and sandbox certificates but do not use them at all. We only use the production certificates and the adhoc profile. That seems to always work.


Android 1.6 Gestures

Android 1.6 platform introduces entirely new gestures framework that finally lets developers build gestures into their apps using the new GestureBuilder tool included in the SDK. Gestures framework provides application developers with a framework for creating, storing, loading, and recognizing gestures and associating them with specific actions.

Need of Gestures:
Touch screens are a great way to interact with applications on mobile devices. With a touch screen, users can easily tap, drag, fling, or slide to quickly perform actions in their favorite applications. But it’s not always that easy for developers. With Android, it’s easy to recognize simple actions, like a swipe, but it’s much more difficult to handle complicated gestures, which also require developers to write a lot of code. In such a situation we can use gestures.This API, located in the new package android.gesture.

We decided to try it out and see how it works. For sample we made a list and used the ‘drag to dustbin’ gesture to delete individual list items.

Step 1: Creating / defining our own gestures using GestureBuilder

GestureBuilder

Step 2: Copy the gesture file created by GestureBuilder from SD card and put it into ‘res/raw’ folder of your project.

Step 3: Write code to handle the event generated by your gesture

step1.png

step2.png


© Copyright 2009 TechJini Solutions Private Limited. All Rights Reserved
iDream theme by Templates Next | Powered by WordPress