Skip to end of metadata
Go to start of metadata

This document provides information on Android Hyperloop requirements, classes, instantiation, methods and fields, casting, interfaces, creating your own classes, and using 3rd party libraries.

Requirements

You’ll need to have the following minimum requirements to use Hyperloop for Android:

  • Titanium 6.0.0+
  • Android 4.0.0+

Configure the plugin in tiapp.xml:

Configure the module in tiapp.xml:

Classes

Classes in Hyperloop map to the underlying classes defined in Java. For example, if you have a class such as android.view.View defined, you would reference it using a standard require such as:

 

This will return the View class object. Meaning, it’s not an instance of a View, but the View class itself.

Once you have a the Class reference returned from require, you can call normal JavaScript property and functions against it. Remember, at this point calling functions or properties against the class object above will be accessing Class level (static) Java methods (not instance level).

For example, you could get the generated view id of the View using the example:

This is because generateViewId is defined as a static method.

Instantiation

Please refer to our hyperloop-examples app in those code-level examples. For example, to use a View, the activity needs to be set before (see here )

Methods and fields

Methods in Java are mapped to JavaScript functions. Fields in Java are mapped to JavaScript property accessors. static methods or fields (such as constants) will be attached to the class type. For example:

Would map to the following in JavaScript:

Method resolution

If a class has overloads for a method (multiple forms of the method with different signatures, but the same name), we will attempt to match the correct method to invoke on the Java side by matching the passed in arguments to the closest match. Typically, this involves matching the name, number of arguments and the ability to convert the passed in arguments (in-order) to the method’s parameter types. We are slightly more liberal in accepting numeric primitives than typical method resolution due to the conversion of JS Numbers.

Casting

Sometimes interfaces define generic return types such as Object and you will need to cast them to a different type to then reference methods and properties of the class. You can pass along the object you want to wrap to the constructor of the type you want to wrap it in. For example, suppose the result of the function returned an Object but you know the implementation is actually a View. You could use the following:

Be careful with casting: If you cast an object which is actually something different, you will experience an error and likely a crash.

You can also cast a Titanium UI Component into its equivalent. For example, this would work:

Interfaces

Interfaces may be implemented using a Javascript syntax similar to an anonymous Java class. Call the constructor of the interface type with a JS object that contains properties that match the interface method names, and corresponding values as function that implement them. For example, to create an instance that implements android.view.View.OnTouchListener:

Creating your own classes

Creating your own classes

Hyperloop provides you the ability to dynamically create your own Java classes at runtime. Once created, these classes can be used as normal in either Hyperloop or passed to native calls. We generate the custom subclass using the "extend" function of the type we want to extend, which takes a single JS Object as an argument containing the overriding method implementations (same as we did for interface implementations). The returned value is a new class type that subclasses the extended type. We can then use the constructor to generate instances of that subclass.

It's easiest to understand with an example - let's create a simple custom subclass of android.view.View, and instantiate an instance of it:

 

This will create a new class in the Java runtime which will extend  android.view.View  which is equivalent to the following code (though please note that we do  not  generate Java source, but instead generate Dalvik bytecode that gets loaded into the runtime as a class):

 

Using Third-party libraries

You can use Third-party libraries in Hyperloop such as JARs and AARs.

Place the JAR files into the platform/android folder of your app. Hyperloop will pick up the JAR files and will generate necessary bindings and include the JARs in your app.

Place the AAR files into the platform/android folder of your app. Hyperloop will pick up the AAR files and will generate necessary bindings, extract resources, extract and use the classes.jar, *.so file, etc.