Skip to content

how to configure rosjava apps with gradle

darrenl edited this page Jun 20, 2016 · 3 revisions

Development environment

Two ways to develop Android apps with rosjava.

1. Catkin development environment with rosjava installation

You can build the multiple rosjava packages in one shot; running catkin_make, at the catkin workspace.
Refer to this link.

2. Any environment with no rosjava installation

Build scripts pull in maven to your project, requiring no rosjava core library. Supports development on Windows.
Refer to this link.

Android Studio

installaton

Download and install Android Studio.

Prepare the Android SDK

Prepare any of the following APIs.

Android SDK Versions

  • API 10 (Gingerbread)
  • API 13 (Honeycomb)
  • API 15 (Ice Cream)
  • API 18 (Jellybean)

Create new projects

catkin

$ mkdir -p ~/YOUR_WORKSPACE/src
$ cd src
# catkin_create_android_pkg [package name] [dependecy 1] [dependecy 2] ..
$ catkin_create_android_pkg test_pkg android_core rosjava_core std_msgs
$ cd test_pkg
# catkin_create_android_project [-t TARGET_VERSION] [-p ANDROID_PACKAGE_NAME] [-a AUTHOR] [NAME]
$ catkin_create_android_project -t 13 -p com.github.irvs.ros_tms.test_project test_project
$ cd ../..
$ catkin_make

Then the package has CMakeLists.txt

# CMakeLists.txt
cmake_minimum_required(VERSION 2.8.3)
project(android_extras)
find_package(catkin REQUIRED rosjava_build_tools)
catkin_android_setup(assembleRelease uploadArchives)
catkin_package()

no rosjava

Create a new project on Android Studio.

Rosjava dependencies

1. Edit the subproject's build.gradle

Utilize remote dependencies.

Add repositories.

repositories {
    maven {
        url 'https://github.com/rosjava/rosjava_mvn_repo/raw/master'
    }
    mavenCentral()
}

Add dependecies.
Some "exclude group:" are a workaround for this issue

dependencies {
    compile ('org.ros.android_core:android_honeycomb_mr2:[0.1,0.2)') {
        exclude group: 'junit'
        exclude group: 'xml-apis'
    }
    // e.g. official msgs
    compile 'org.ros.rosjava_messages:std_msgs:[0.1,)'
    compile 'org.ros.rosjava_messages:sensor_msgs:[0.1,)'
    compile 'org.ros.rosjava_messages:geometry_msgs:[0.1,)'
}

Add packagingOptions (license)

android.packagingOptions {
      exclude 'META-INF/LICENSE.txt'
      exclude 'META-INF/NOTICE.txt'
}

2. Edit AndroidManifest.xml

Add the permission below.

<uses-permission android:name="android.permission.INTERNET" />

Add MasterChooser and NodeMainExecutor

<!-- To set up the connection to the master -->
<activity android:name="org.ros.android.MasterChooser" />

<!-- To manage to execute nodes like the node handler -->
<service android:name="org.ros.android.NodeMainExecutorService" >
    <intent-filter>
        <action android:name="org.ros.android.NodeMainExecutorService" />
    </intent-filter>
</service>

3. Sync the project

Sync your project with gradle files.

Minimal codes

MainActivity class

package ...;

import ...

// Do not extend Activity, but RosActivity!
public class MainActivity extends RosActivity{

    public MainActivity() {
        super("Example", "Example");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void init(NodeMainExecutor nodeMainExecutor) {
    	// A unique part for ROS
    }
}

Add a node class

public class ExampleNode implements AbstractNodeMain {
	// To be seen by RosCore
	@Override
    public GraphName getDefaultNodeName() {
        return GraphName.of("ExampleNode");
    }

    @Override
    public void onStart(final ConnectedNode connectedNode) {
        // Define any publishers, subscribers, servers or clients..
    }

    @Override
    public void onShutdown(Node node) {
        //
    }

    @Override
    public void onShutdownComplete(Node node) {
        //
    }
}

Generate a instanse of the node class

public class MainActivity extends RosActivity{
	...

	@Override
	protected void init(NodeMainExecutor nodeMainExecutor) {
	    ExampleNode exampleNode = new ExampleNode(this);

	    NodeConfiguration nodeConfiguration = NodeConfiguration.newPublic(
	            InetAddressFactory.newNonLoopback().getHostAddress());
	    nodeConfiguration.setMasterUri(getMasterUri());

	    nodeMainExecutor.execute(exampleNode, nodeConfiguration);
	}
}

References

ROS wiki - rosjava
ROS wiki - ROS Development Environment
ROS wiki - Android Studio Development Environment
GitHub rosjava_core - Latest official documentation
GitHub rosjava_core - Overview summery

Clone this wiki locally