Skip to content

On-premise users: click in-app to access the full platform documentation for your version of DataRobot.

Android integration

It is possible to use Java Scoring Code on Android with little or no modifications.

Note

Supported Android versions are 8.0 (API 26) or later.

Using a single model

Using a single model in an Android project is almost the same as using it in any Java project:

  1. Copy the Scoring Code JAR file into the Android project in the directory app/libs.
  2. Add the following lines to the dependency section in app/build.gradle:
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    annotationProcessor fileTree(include: ['*.jar'], dir: 'libs')
    
  3. You can now use the model in the same way as the Java API.

More complex use cases

You must process the Scoring Code JARs to enable more complex functionality. DataRobot provides a tool: scoring-code-jar-tool that will process one or more Scoring Code JAR files to be able to accomplish the following goals. scoring-code-jar-tool is distributed as a JAR file and can be obtained here.

Using multiple models

It is not possible to use more than one Scoring Code JAR in the same Android project. Each Scoring Code JAR contains the same dependencies and Android does not allow multiple classes with the same fully qualified name. To fix this, scoring-code-jar-tool can be used to take multiple input JAR files and merge them into a single JAR file with duplicate classes removed.

For example:

`java -jar scoring-code-jar-tool.jar --output combined.jar model1.jar model2.jar`

Dynamic loading of JARs

To dynamically load scoring code jars, they must be compiled into Dalvik Executable (DEX) format. scoring-code-jar-tool can compile to dex using the --dex parameter.

For example:

`java -jar scoring-code-jar-tool.jar --output combined.jar --dex /home/user/Android/Sdk/build-tools/29.0.3/dx model1.jar model2.jar`

The --dex parameter requires the path to the dx tool which is a part of the Android SDK.

Java example

In this example, a model with id 5ebbeb5119916f739492a021 has been processed by scoring-code-jar-tool with the --dex argument to produce an output JAR called model-dex.jar. For the sake of this example, the merged JAR file has been added as asset to the project. It is not possible to get a filesystem path to assets which is why the asset is copied to a location in the filesystem before it is loaded.

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String filename = "model-dex.jar";

        File externalFile = new File(getExternalFilesDir(null), filename);
        try {
            copyAssetToFile(filename, externalFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        DexClassLoader loader = new DexClassLoader(externalFile.getAbsolutePath(), "", null, MainActivity.class.getClassLoader());
        IClassificationPredictor classificationPredictor = Predictors.getPredictor("5ebbeb5119916f739492a021", loader);
    }

    private void copyAssetToFile(String assetName, File dest) throws IOException {
        AssetManager assetManager = getAssets();

        try (InputStream in = assetManager.open(assetName)) {
            try (OutputStream out = new FileOutputStream(dest)) {
                byte[] buffer = new byte[1024];
                int read;
                while ((read = in.read(buffer)) != -1) {
                    out.write(buffer, 0, read);
                }
            }
        }
    }
}

Updated May 11, 2023