Skip to the main content.

Did you know?

 

RTI is the world’s largest DDS supplier and Connext is the most trusted software framework for critical systems.

Success-Plan-Services-DSSuccess-Plan Services

Our Professional Services and Customer Success teams bring extensive experience to train, problem-solve, mentor, and accelerate customer success.

Learn more

Developers

From downloads to Hello World, we've got you covered. Find all of the tutorials, documentation, peer conversations and inspiration you need to get started using Connext today.

Try the Connectivity Selection Tool ⇢

Resources

RTI provides a broad range of technical and high-level resources designed to assist in understanding industry applications, the RTI Connext product line and its underlying data-centric technology.

Company

RTI is the infrastructure software company for smart-world systems. The company’s RTI Connext product is the world's leading software framework for intelligent distributed systems.

Contact Us

News & Events
Cooperation

5 min read

Building Connext Applications Using Android Studio

Building Connext Applications Using Android Studio

Android Studio is the official IDE for Android application development, based on IntelliJ IDEA. The first stable build was released in December 2014, starting from version 1.0. Android Studio is designed specifically for Android development and it is available for download on Windows, Mac OS X and Linux at http://developer.android.com/tools/studio/index.html. This section will describe how to use Android Studio to build a Connext application. It assumes that Android Studio is correctly installed.

Create an Android Project

This example uses and IDL file to define the types. The IDL file will be created and added once the project is created. The Android App built in this section can interoperate any other RTI Connext application using the same IDL file, same topic name, and compatible QoS settings. If a different IDL file is used, then note that the following steps will need to be altered slightly as the IDL file name, "HelloWorld" is used in completing fields and naming other files in this example. This example shows only the creation of the publisher application. These steps can be repeated to create a HelloWorldSubscriber application. If the subscriber and the publisher will be installed on the same device, make sure the subscriber's Application, Project, and Package names are different than the publisher's.

From the Android Studio menu bar, select File -> New Project.

File -> New Project

In the displayed New Project dialog, fill in the Application name, Company domain, and project location. The dialog will select the default project location and uses the application name to create a sub-directory. You can leave the project location and use the default. After you completed all the fields click next.

Configure your new project

Select the form factors and click next. For this example we create an Application for a Nexus 7 tablet and selected an API level which makes it compatible with most of the Android devices. You can select different API levels based on your application preferences.

Select the form factors

Choose Blank Activity since we are not creating any UI for this application. For your application you might want to use some of the other templates based on your target application. Click Next.

Add an activity

Last we select the name for the activity and some other elements. Edit the Activity Name as shown below. There is no need to edit any of the other fields since they are derived from the Activity Name. Click Finish.

activity-name.png

After the project has been created, Android Studio will display the Project as shown below. At this point, HelloWorldPublisherActivity.java is the only source file in the project.

hello-world.png

Edit the Project Source Code

Edit the generated Java class (HelloWorldPublisherActivity) by adding the two lines shown below. This will create the simplest Android application with Connext. More sophisticated Android code can and probably would be used for a real application. Real Android applications should never call an endless loop from the main activity since this will stop the application from responding.

Android Studio shows HelloWorldPublisher in red text because that class does not yet exist in the project. Save the changes to HelloWorldPublisherActivity.java.

error-in-red.png

Create the HelloWorld.idl file in the project's java folder. Right click on the java folder and select New -> File.

create-idl.png

The destination directory dialog box should already show the right location ..\src\main\java. Click OK.

idl-dest-dir.png

Enter HelloWorld.idl as file name and click OK.

idl-file-name.png

Android Studio will open the newly created file for editing. Add the text as shown below and save the file.

const long HELLODDS_MAX_PAYLOAD_SIZE = 8192;
const long HELLODDS_MAX_STRING_SIZE  =   64;
struct HelloWorld {
    string<HELLODDS_MAX_STRING_SIZE>             prefix;
    long                                         sampleId;
    sequence<octet, HELLODDS_MAX_PAYLOAD_SIZE>   payload;
};
edit-idl.png

Open a terminal window/command prompt. Before generating the example code make sure that NDDSHOME environment variable is set correctly to the Connext installation directory and that the PATH environment variable includes $NDDSHOME/scripts.

Change the directory to your applications java directory (HelloWorldPublisher\app\src\main\java) and run rtiddsgen as follow to generate the example files

rtiddsgen -language Java -package com.rti.example.helloworldpublisher -example armv7aAndroid2.3gcc4.8jdk HelloWorld.idl

Note that the -package argument is the same value as used when creating the project. If the rtiddsgen-generated java classes are in a different package to that of the project then "import" statements will need to be added to some java files. You can find the package name at the top of the HelloWorldPublisherActivity.java file.

If you run on Windows and do not have the Visual Studio compiler in your PATH environment variable add –ppDisable to the rtiddsgen command line to disable the preprocessor. Since the IDL file doesn't have any pre-processor statements running the pre-processor is not needed.

rtiddsgen.png

You will see the following un your command prompt window:

Running rtiddsgen version 5.1.0, please wait ...
Done

Done means that all the required type support files and an example files have been generated.

In Android Studio you should see all the additional classes which have been generated and the call to HelloWorldPublisher.main in HelloWorldPublisherActivity should now be shown as resolved. The USER_QOS_PROFILES.xml file, the HelloWorldSubsciber class, and the makefile is not used by this project. Those files can be deleted or kept for reference.

Add Connext Libraries

Resolve the Connext symbols in the generated java files by adding the Connext libraries to the project.

Copy nddsjava.jar from $NDDSHOME/class to the lib directory of your HelloWorldPublisher App. You should see the library being added as shown below.

add-connext-lib.png

The next step is to add the library to the build. Right click on it and select Add as library.

add-lib-to-build.png

The library needs to be added to the application. Click OK.

add-lib-to-app.png

If you open the build.gradle file you will see that the library has been added.

build-gradle-file.png

Connext is not pure Java, it also consists of some native libraries. When building the project, you will not see any complaints if these libraries are missing, but when you try to run the App, the LogCat panel will show errors such as:

com.rti.example.helloworldpublisher W/System.err: The library libnddsjava.so could not be loaded by Linux.
com.rti.example.helloworldpublisher W/System.err: Make sure that the library is in your LD_LIBRARY_PATH environment variable.

And you will see the following error screen on your Android device

error-screen.png

Below is how I added the native libraries to the Android Studio project.

Add a lib directory to your project with a subdirectory which contains the native Connext DDS libraries (libnddsc.so, libnddscore.so, and libnddsjava.so). for this example named the sub-directory armeabi-v7. This will look as follow in your project.

add-lib-dir.png

Once you have your .so files in that directory configuration, create a .zip file from the lib directory. I named the file lib_rti.zip. The .zip file will have lib/armeabi-v7/*.so as the contents.

create-zip-file.png

Rename the extension of the zip to .jar giving you a file names native-rti-libs.jar.

rename-zip-file.png

And then drag the .jar into your Android Studio project libs directory (same location as all your other jar libraries).

move-jar-file.png

Finally, add this to your projects build.gradle:

Compile fileTree(dir: 'libs', include: '*.jar')
update-gradle-file.png

Update the Android Manifest

Connext expects to use the Internet and Wi-Fi to access the Internet. It needs to be able to change some of the Wi-Fi settings. Connext also needs to access external storage if it is to read a USER_QOS_PROFILES.xml file (although the current example doesn't make use of that).

In the project Explorer, double-click the AndroidManifest.xml file to edit it. Add the following lines to the manifest file as sown below.

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
update-manifest.png

Run the Example

Before running the example make sure you have an Android device connected via USB or have a Virtual Android Device running.

Select Run->Run 'app' or click on the green triangle next to app in the toolbar

run-the-example.png

Choose the device you want the application to run on. The example below shows a Nexus 7 connected through USB and a virtual device which emulates a Nexus 7.

Choose the device you want the application to run on

Once it is running you will see the output being displayed in the logcat window.

logcat window

If you have a machine connected to the same network which has RTI Connext installed you can start rtiddspsy and see the samples being published.

rtiddspsy.png

The sample code doesn't fill the samples with any actual values. You can add this to your code in the HelloWorldPublisher class. Look for the comment "Modify the instance to be written"

modify-instance.png

The example used in here uses a loop to send DDS samples. A real Android application would, of course, have a UI to control the behavior and not have a loop sending samples with a delay between sending samples. A real Android application should never block in a loop. However, I hope this example was useful in showing how to use the RTI Connext libraries and build an application using Android Studio.