Bulk Emailing

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Tuesday, 17 September 2013

Java Reflection as Android API

Posted on 08:37 by Unknown
Have you ever tried or wished to re-use or access source code which is part of a different Android APK ? If "Yes", then this article is for you. 
Well, I must agree its not a very common use case and when it comes to API in Android, we rather prefer JAR or Remote Service to expose functionalists. Java Reflection can be an alternate and useful API approach to expose functionalists from an APK or Application.

Here I'm going to explain a design approach where we can define API for Android using Java Reflection. For this example, Host.apk is an Application which hosts one API method which we are going to access from another application Client.apk.

Host.apk includes- HostApi.java which defines API method getMeta(). This method returns some pre-defined Text appended to calling Application's Package name.

package com.pras.host;

import android.content.Context;

public class HostApi {

     Context mContext;

     public HostApi(Context context) {
         this.mContext = context;
     }

     public String getMeta() {
         String pkgName = mContext.getPackageName();
         return "HostKey-" + pkgName;
     }
}

To access above API method getMeta(), we need to write a HostApi Proxy which access HostApi.java using Reflection. Here is the code snippet-

package com.pras.client; 

import java.lang.reflect.Constructor; 
import java.lang.reflect.Method; 
import android.content.Context;

public class HostApiProxy {

     private final String REMOTE_PACKAGE = "com.pras.host";
     private final String REMOTE_CLASS = "com.pras.host.HostApi";
     private final String REMOTE_METHOD = "getMeta";

     Context mContext;

     public HostApiProxy(Context context) {
         this.mContext = context;
     }

     public String getMeta() {
         String metaData = null;
         try {
              Class hostApiClass = getRemoteClass(mContext, REMOTE_PACKAGE,
                       REMOTE_CLASS);
              Object hostApiInstance = getRemoteInstance(hostApiClass,
                       new Class[] { Context.class }, new Object[] { mContext });
              metaData = (String) invokeRemoteMethod(hostApiInstance,
                       REMOTE_METHOD, new Class[] {}, new Object[] {});
         } catch (Exception ex) {
              ex.printStackTrace();
         }
         return metaData;
     }

     public Class getRemoteClass(Context context, String remotePackage,
              String remoteClass) throws Exception {

         // Get remote Application Context and request to include Source
         Context remoteContext = context.createPackageContext(remotePackage,
                  Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
         // Get Class loader associated to Remote Context
         ClassLoader loader = remoteContext.getClassLoader();
         Class cls = loader.loadClass(remoteClass);
         return cls;
     }

     public Object getRemoteInstance(Class remoteClass, Class[] args,
              Object[] argValues) throws Exception {
         Constructor constructor = remoteClass.getConstructor(args);
         return constructor.newInstance(argValues);
     }

     public Object invokeRemoteMethod(Object instance, String methodName,
              Class[] args, Object[] argValues) throws Exception {
         Method method = instance.getClass().getDeclaredMethod(methodName, args);
         // Make sure its accessible (if it is Private method)
         method.setAccessible(true);
         return method.invoke(instance, argValues);
     }
}
 

I have defined 3 utility methods- getRemoteClass(), getRemoteInstance() and invokeRemoteMethod() which help to get reference of remote Class stored in Host.apk and invoke getMeta() method. The important point to note here; to load the remote class, we need to get the instance of Class Loader of the respective Application's context. In Android, each application Context has its own Class Loader which is responsible to load/manage classes included inside the Application or APK.

Above approach and Reflection methods can be used to define any API. So, if you like this design approach, give a 2nd thought during designing your next API. Please find the complete Source and APK (Host.apk and Client.apk).
Email ThisBlogThis!Share to XShare to Facebook
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Weekend at Yeouido (Han River Cruise)
    Yeouido is one of the Han River Cruise boarding points. It has a wonderful river side park (Yeouido P...
  • CityWeather
    Update: Release 1.1 has been uploaded. It will now provide weekly forecast of your selected cities. Download   CityWeather is an Android...
  • Few Multi-Threading Implementations
    Sequential Producer & Consumer In my first example, I will demonstrate an implementation to solve ...
  • Accessing Yahoo Finance API
    Since last few days I was wondering the right set of Web Service to read Country wise Stock Exchange index information . I found a bunch of ...
  • Regular Expression in JavaScript
    I was struggling to get familiar with JavaScript's Regular expression handling and thought my learning might help others. Here I'll...
  • Alphanumeric Random Number
    Sometime basic things becomes tricky :-) Was trying to find out ways to generate some alphanumeric random number whose size (i.e. number of ...
  • Binary Search Tree in Java
    This post is a part of my continuous effort to just implement few of the important data structures in ...
  • QuickSort in Java
    This post is a part of my continuous effort to just implement few of the important data structures in Java. Please find my previous post on ...
  • InDroid- Access to Android's Hidden API
    I have started one project " InDroid " to expose and share Hidden and Internal methods and variables of Android platform which are...
  • Image Geo Tagging for Android
    " onMap " is an Image Geo Tagging application, which can read EXIF header of images stored in your Android device and Map them ov...

Blog Archive

  • ▼  2013 (6)
    • ▼  September (2)
      • Android Looper and Toast from WorkerThread
      • Java Reflection as Android API
    • ►  May (1)
    • ►  April (1)
    • ►  February (1)
    • ►  January (1)
  • ►  2012 (4)
    • ►  July (2)
    • ►  March (1)
    • ►  January (1)
  • ►  2011 (11)
    • ►  November (1)
    • ►  October (2)
    • ►  August (1)
    • ►  June (1)
    • ►  April (2)
    • ►  March (3)
    • ►  January (1)
  • ►  2010 (27)
    • ►  December (2)
    • ►  November (3)
    • ►  September (2)
    • ►  August (4)
    • ►  July (4)
    • ►  June (7)
    • ►  May (5)
Powered by Blogger.

About Me

Unknown
View my complete profile