Thursday 17 April 2014

NDKHello2 Tutorial

Hi,
This is my Second NDKHello2 Demo project for newbies

SourceCode Link:https://github.com/JohnnyIqbal/NDKHello2
Please add appcompat library to run this project

In this demo I added a folder src in jni folder and tried to compile.

I followed this process to make this project.

1. Create NativeLib.java file

2. Open terminal and head to project directory
  cd bin/classes   
and generate header files of NativeLib.java using this command
 javah -jni com.iqbal.ndkhello.ndk.NativeLib  
3. Copy NativeLib.h file in jni folder (Create if not exist in project root)
or you can use this command
 javah -jni -o ../../jni/NativeLib.h com.iqbal.ndkhello.ndk.NativeLib  

4. Create NativeLib.c file and copy method signature in NativeLib.c
and don't foreget to include NativeLib.h


 Method Signature eg:  
 JNIEXPORT jstring JNICALL Java_com_iqbal_ndkhello_ndk_NativeLib_getHello2  
this is the declared method in header files and change to method defination

 #include"NativeLib.h"  
 JNIEXPORT jstring JNICALL Java_com_iqbal_ndkhello_ndk_NativeLib_getHello (JNIEnv *env, jclass clz){ return (*env)->NewStringUTF(env,"Hello World2"); }  

Note:NewStringUTF sometimes this function shows error means red line to suppress this error please update your CDT.

5. Add new folder in jni called src add some c files and header files as I did main.c and main.h

6. Call main.h function in NativeLib.c by including main.h header files.
 #include"src/main.h"  
 JNIEXPORT jint JNICALL Java_com_iqbal_ndkhello2_ndk_NativeLib_getAdd(  
   JNIEnv *env, jclass clz, jint a, jint b) {  
 int c=add(a,b); //this function declared in main.h and defiened in main.c  
 return c;  
 }  

7.Now create Android.mk file and write these lines to build
 LOCAL_PATH:=$(call my-dir)  
 include $(CLEAR_VARS)   
 LOCAL_MODULE:=hello2   
 LOCAL_SRC_FILES:=NativeLib.c src/main.c  
 include $(BUILD_SHARED_LIBRARY)  

8.Create Application.mk to suppress the Warnning or error android-19 is larger than minsdkversion 8 and write this lines
 APP_PLATFORM:=android-8  
android-8 is my minsdkversion so be sure about this change according to minsdkversion in AndroidManifest.xml


9.Open terminal head to project root and fire this command
 ndk-build  

10. Now run your Android NDKHello Project.


How to sync contacts Android

Hi,

Source Code Link: https://github.com/JohnnyIqbal/ContactSyncBG

Please add appcomapt library to run this project.

This is ContactSyncBackground Demo as you see whatsapp,viber or other apps

I followed this process to achieve syncing in background

1. Create AuthenticatorService and ContactsSyncAdapter Service class.
AuthenticatorService Class is for when you create syncing account then this service run
so you can perform any operation during creating account or add account in setting.

2. To create account
SyncUtils.CreateAccount(instance, user, pass);

3. When account created successfully then you want to sync your contacts, ContactsSyncAdapterService is run when you perform syncing either manually(using setting) or automatically.

4. To perform syncing automatically
 AccountManager am = AccountManager.get(instance);  
  Account[] acclist = am.getAccounts();  
  for (Account acc : acclist) {  
   if (acc.type.equals(SyncUtils.ACCOUNT_TYPE)) {  
   // You need pass bundle to sync  
   Bundle b = new Bundle();  
   b.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);  
   b.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);  
   ContentResolver.requestSync(acc, SyncUtils.AUTHORITY, b);  
   Log.i("MainActivity", "Request to sync Contacts");  
   }  
  }  
5. After firing this operation onPerformSync() method called in ContactsSyncAdapterService
   Here you can perform network syncing operation. I poupulated the contacts in json and post to server
   and server returning me intersection of contacts.
 
6. You need to register this service in Manifiest

  <service  
       android:name="com.iqbal.contactsyncbg.sync.ContactsSyncAdapterService"  
       android:exported="true"  
       android:process=":contacts" >  
       <intent-filter>  
         <action android:name="android.content.SyncAdapter" />  
       </intent-filter>  
       <meta-data  
         android:name="android.content.SyncAdapter"  
         android:resource="@xml/sync_contacts" />  
       <meta-data  
         android:name="android.provider.CONTACTS_STRUCTURE"  
         android:resource="@xml/contacts" />  
     </service>  
     <service  
       android:name="com.iqbal.contactsyncbg.sync.AuthenticatorService"  
       android:exported="true"  
       android:process=":auth" >  
       <intent-filter>  
         <action android:name="android.accounts.AccountAuthenticator" />  
       </intent-filter>  
       <meta-data  
         android:name="android.accounts.AccountAuthenticator"  
         android:resource="@xml/authenticator" />  
     </service>  
 7. Be sure of Account Type what you defined in sync_contacts and ContactSyncAdapter must be same.
  Otherwise you will get this exception caller uid is different than the authenticator's uid to fix
  this exception you need to check account type.
     
 8. At last you need to add permission
 <uses-permission android:name="android.permission.READ_CONTACTS" />  
 <uses-permission android:name="android.permission.WRITE_CONTACTS" />  
 <uses-permission android:name="android.permission.GET_ACCOUNTS" />  
 <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />  
 <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />  
 <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />  
 <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />  

Wednesday 16 April 2014

Hello NDK

Source Code Link: https://github.com/JohnnyIqbal/NDKHello
Please add appcompat library to run this project

This is my First NDKHello Demo project for newbies

Create Simple Android Application Project.
then

1. Create NativeLib.java file

2. Open terminal and head to project directory
 cd bin/classes and  
generate header files of NativeLib.java using this command
  javah -jni com.iqbal.ndkhello.ndk.NativeLib  

3. Copy NativeLib.h file in jni folder (Create if not exist in project root)
or you can use this command
  javah -jni -o ../../jni/NativeLib.h com.iqbal.ndkhello.ndk.NativeLib  

4. Create NativeLib.c file and copy method signature in NativeLib.c
and don't foreget to include NativeLib.h

Method Signature eg:
 JNIEXPORT jstring JNICALL Java_com_iqbal_ndkhello_ndk_NativeLib_getHello  
 (JNIEnv *, jclass);  

this is the declared method in header files and change to method defination
  #include"NativeLib.h"  
 JNIEXPORT jstring JNICALL Java_com_iqbal_ndkhello_ndk_NativeLib_getHello  
  (JNIEnv *env, jclass clz){  
  return (*env)->NewStringUTF(env,"Hello World");  
 }  

Note:NewStringUTF sometimes this function shows error means red line to suppress this error please update your CDT.

5.Now create Android.mk file and write these lines
 LOCAL_PATH := $(call my-dir)  
 include $(CLEAR_VARS)  
 LOCAL_MODULE := hello  
 LOCAL_SRC_FILES := NativeLib.c  
 include $(BUILD_SHARED_LIBRARY)  

6.Create Application.mk to suppress the Warnning or error android-19 is larger than minsdkversion 8....
and write this lines
 APP_PLATFORM:=android-8  

Note:android-8 is my minsdkversion so be sure about this change according to minsdkversion in AndroidManifest.xml

7.Open terminal head to project root and fire this command
 ndk-build  

8. Now run your Android NDKHello Project.

Reference:
http://www.ntu.edu.sg/home/ehchua/programming/android/Android_NDK.html

Things after Kotlin Android Extensions

Things after Kotlin Android Extensions(KTX) I remembered in MVVM when I have to declare ViewModel, I initialize View Model like this. p...