This document provides information on Android Hyperloop requirements, classes, instantiation, methods and fields, casting, interfaces, creating your own classes, and using 3rd party libraries.
See Hyperloop Requirements for software requirements for using Hyperloop with Android.
Make sure to add the Hyperloop module to the
<modules> section of the
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 (it’s not an instance of a
View, but the
View class itself).
Once you have the Class reference returned from
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.
Please refer to our hyperloop-examples app in those code-level examples. For example, to use a
View, the activity needs to be set:
See animateview.js (Github) for an working sample.
Methods and fields
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:
Creating your own classes
Let's create a simple custom subclass of
android.view.View, and instantiate an instance of it:
android.view.Viewwhich 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):
Accessing native XML Layouts
In native Android development, you have many situations where you are inflating native XML layouts in your source-code. In Hyperloop, you can do the same with just a few lines of code:
Using 3rd party libraries
As of Titanium 9.0.0, Hyperloop can reference libraries via gradle by adding a
build.gradle file to the
platform/android (Classic) or
app/platform/android (Alloy) folder of your app. This is the preferred way of adding library dependencies.
An example can be found here: hyperloop-examples - /app/platform/android/build.gradle
The below is an example
build.gradle file which provides access to the Google "Material Components" library.
You can add 3rd party library files such as JARs and AARs directly to Hyperloop.
Place the JAR and AAR files into the
platform/android (Classic) or
app/platform/android (Alloy) folder of your app. Hyperloop will load the Java classes from these libraries and generate the necessary bindings. All Java classes within the JARs and AARs will be merged into your app. For AARs, the build system will also extract its resource files, assets, *.so libraries, and merge its
AndroidManfiest.xml into your app.
Make sure to check out more Android third-party libraries (like Localytics, SSDP, Shimmer) in our Hyperloop Sample App.