Predict
Published in

Predict

Using Reflection in Android

When designing an Android Application, one of the important things to remember is the download size of your application. Huge download sizes discourage the users to download and install the application. The factor of size restriction becomes worse if you are designing a library, as developers will not want to bloat their application because of the size of your library.

Hence, to keep the library size less, it is important to bundle related functionality together while modularizing the other unrelated functionalities. This allows the developer to integrate your library to pick and choose the functionality they want to avail from your library while keeping their application sizes in check.

At Haptik, we divided the entire SDK into 3 major modules:

1. Core: which will enable the basic chat flows like “Daily Quiz”, “Reminders” etc in the client’s application.

2. Travel: along with the basic chat flow the developers can also enable the functionality to make travel bookings using the Haptik UI flow, by integrating the Travel module of Haptik. (If transactions are needed, Payments module needs to be integrated as well)

3. Payments: Haptik also offers options to make Payments over its platforms, like “Recharge” for phones operators etc.

Modularizing the functionalities simultaneously increases the complexity of integrating the library. The developers using your library will have to keep in mind that they should initialize all the modules of the library before using it. Failing to do so, may cause the library to crash.

To solve the above problem one of the approaches is using REFLECTION.

What is Reflection?

In object-oriented programming languages such as Java, reflection allows inspection of classes, interfaces, fields and methods at runtime without knowing the names of the interfaces, fields, methods at compile time. It also allows instantiation of new objects and invocation of methods ( Wikipedia ).

Let’s understand Reflection in Java with a simple example:

// With reflection to instantiate an object

Object foo = Class . forName ( "complete.classpath.and.Foo" ) . newInstance ( ) ;

// Alternatively you can also use the below:

Object foo = Foo . class . newInstance ( ) ;

Method m = foo . getClass ( ) . getDeclaredMethod ( "hello" , new Class < ? > [ 0 ] ) ;

Using Java Reflection you can check Java classes at runtime. Checking for classes is often the first thing you do when using Reflection. You can obtain the following information from the class:

  • Class Name
  • Class Modifies (public, private, synchronized etc.)
  • Package Info
  • Superclass
  • Implemented Interfaces
  • Constructors
  • Methods
  • Fields
  • Annotations, plus a lot more information related to Java classes.

For a full list, you should consult the for java.lang.Class. Doc

Why use “reflection” to create objects instead of the “new” operator in Java?

- “ new “ operator is used to create objects as shown in the below example:

Here, the limitation is that if you use new operator there is no way to define the type of the object at runtime.

Let’s see how we can do this using Reflection:

Since the type of object needs to be created at the runtime, the availability of the class at the runtime needs to be checked:

//this will get the public constructors for the class

//this will get all the declared constructors for the class

//this will return only the public methods

//this will return all the methods declared in the class

//this will get all the public fields for the class

//this will get all the declared fields for the class

//to get a specific method at the runtime pass the name of the method

//since the method in question is static the object on which it needs to be called is null

//else you can pass `clsObject` from above as the parameter to the method below

//take action as the module you were looking for exists

//add your fallback mechanism here

//add your fallback mechanism here

//add your fallback mechanism here

You can explore more details about using reflection to get more properties of a class here.

What if the access to a constructor or a field or a method is private?

You can access a “ private “ method using:

//to get a specific method at the runtime pass the name of the method

//since the method in question is static the object on which it needs to be called is null

//else you can pass `clsObject` from above as the parameter to the method below

/*the invoke(Object, obj, Object... args) method takes the object on which the method needs to be

* invoked as a parameter, and the arguments that are required by the method you are trying to invoke

* as the second argument*/

* It throws `NoSuchMethodException` which you will have to handle*/

So, you can check for the presence of the dependency in the build.gradle and internally initialize the class. If the class is not present you can also have a fallback mechanism.

To get access to the code for this project, visit:
https://github.com/rajas47ashtikar92/reflection-sample-app

We would love to know what you think about this approach and if there is any better way to tackle the problems of ever-growing sizes of mobile applications/SDKs. In our next blog, we will talk about some other things we did to reduce our android SDK size.

Want to develop an Intelligent Virtual Assistant solution for your brand?

GET IN TOUCH

Don’t forget to give us your 👏 !

--

--

--

where the future is written

Recommended from Medium

Is that an <H1> in your pocket or are you just pleased to see me?

Spotify API: HowTo on Setting Up with Rails and React

How To Create Video Overlays With CSS

CSS Video Overlays

Deploying Wordpress With AWS RDS

Use Apps Script to pull all Google Shared Drives and permissions into a Google Sheet

Docker Swarm, Kubernetes’s clever little borther

How to Change Repo Language in GitHub

GraphQL Codegen adds a new Apollo-Android and a new Java Backend plugins!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Haptik

Haptik

Global leaders in Conversational AI, powering Intelligent Virtual Assistants (IVA) that transform Customer Experience

More from Medium

Updating your application in the Google Play Store

React Native — An Incredible Mobile App Development Technology for Startups

React Native Tutorials -3

GSSoC at First Year | How were my 9 PRs merged? | Journey and experiences