Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android and reflection

Tags:

android

I get the impression that Android supports reflection. But does it really? How sensible would it be to rely on reflection heavily? What's the penalty?

like image 664
Wilfred Springer Avatar asked Nov 18 '09 09:11

Wilfred Springer


People also ask

What is Reflection in Android?

Reflection allows programmatic access to information about the fields, methods and constructors of loaded classes, and the use of reflected fields, methods, and constructors to operate on their underlying counterparts, within encapsulation and security restrictions.

What is reflection in Kotlin?

Reflection is a set of language and library features that allows you to introspect the structure of your program at runtime.

What is Java reflection?

Reflection is a feature in the Java programming language. It allows an executing Java program to examine or "introspect" upon itself, and manipulate internal properties of the program. For example, it's possible for a Java class to obtain the names of all its members and display them.

What is reflection in computer science?

In computer science, reflective programming or reflection is the ability of a process to examine, introspect, and modify its own structure and behavior.


2 Answers

It is supported, and even recommended in the situation where you want compatibility with multiple versions of the Android OS in one apk file. This article from the official Android Developers Blog describes how to build an app that requires only some early version of the API, and uses reflection to invoke new APIs if they are available:

Backward compatibility for Android applications

like image 123
Chris Boyle Avatar answered Sep 28 '22 20:09

Chris Boyle


Android supports reflection.

Once you've got a prototype running, you can benchmark and determine your bottlenecks.

If its reflection, then consider trying to cache interfaces and such to make it a one-off cost, rather than continually resolving the same interfaces from the same instances repeatedly.

like image 44
Will Avatar answered Sep 28 '22 18:09

Will