JBoss.orgCommunity Documentation

Chapter 14. Android Integration

14.1. Integration with Drools Expert
14.1.1. Pre-serialized Rules
14.1.2. KieContainer API with drools-compiler dependency
14.2. Integration with Roboguice
14.2.1. Pre-serialized Rules with Roboguice
14.2.2. KieContainer with drools-compiler dependency and Roboguice

Drools Android integration comes in two flavors, with or without the drools-compiler dependency. Without the drools-compiler dependency the knowledge bases are pre-serialized at buildtime using the kie-maven-plugin. They can be then deserialized using the API, or directly injected using Roboguice. When using the drools-compiler dependency there are two options: (1) standard KieContainer API or (2) CDI-like injection with Roboguice.

It is possible to use Drools without the drools-compiler dependency, which results in a smaller apk, by pre-serializing the compiled knowledge bases during build-time and then de-serializing them at runtime.

With the drools-compiler dependency standard KieContainer API can be used. This comes at a cost of a larger apk. To avoid the 65K limit, multidex (or proguard) can be used.

The kie-maven-plugin must be configured to build the kiebase. Multidex must be used to allow for the increased dependencies. There are also some settings for merging various Drools XML files within the apk.

Example 14.2. pom.xml with drools-compiler and multidex

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.drools</groupId>
      <artifactId>drools-bom</artifactId>
      <version>6.5.0.Beta1</version>
      <type>pom</type>
      <scope>import</scope>
  </dependencies>
</dependencyManagement>
<dependencies>
  <dependency>
    <groupId>org.drools</groupId>
    <artifactId>drools-android</artifactId>
    <exclusions>
      <exclusion>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
      </exclusion>
    </exclusions>
  </dependency>
  <dependency>
    <groupId>org.drools</groupId>
    <artifactId>drools-compiler</artifactId>
    <exclusions>
      <exclusion>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-api</artifactId>
      </exclusion>
      <exclusion>
         <groupId>xmlpull</groupId>
         <artifactId>xmlpull</artifactId>
      </exclusion>
      <exclusion>
         <groupId>xpp3</groupId>
         <artifactId>xpp3_min</artifactId>
      </exclusion>
      <exclusion>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-api</artifactId>
      </exclusion>
      <exclusion>
         <groupId>org.eclipse.jdt.core.compiler</groupId>
         <artifactId>ecj</artifactId>
      </exclusion>
    </exclusions>
  </dependency>
</dependencies>
<build>
  <plugins>
    <plugin>
      <groupId>org.kie</groupId>
      <artifactId>kie-maven-plugin</artifactId>
      <version>6.5.0.Beta1</version>
      <executions>
        <execution>
          <id>compile-kbase</id>
          <goals>
            <goal>build</goal>
          </goals>
          <phase>compile</phase>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>com.simpligility.maven.plugins</groupId>
      <artifactId>android-maven-plugin</artifactId>
      <version>4.2.1</version>
      <extensions>true</extensions>
      <configuration>
        <sdk>
          <platform>21</platform>
        </sdk>
        <dex>
          <coreLibrary>true</coreLibrary>
          <jvmArguments><jvmArgument>-Xmx2048m</jvmArgument></jvmArguments>
          <multiDex>true</multiDex>
          <mainDexList>maindex.txt</mainDexList>
        </dex>
        <extractDuplicates>true</extractDuplicates>
        <apk>
          <metaInf>
            <includes>
              <include>services/**</include>
              <include>kmodule.*</include>
              <include>HelloKB/**</include>
              <include>drools**</include>
              <include>maven/${project.groupId}/${project.artifactId}/**</include>
            </includes>
          </metaInf>
        </apk>
      </configuration>
    </plugin>
  </plugins>
</build>
                  

With Roboguice and drools-compiler almost the full CDI syntax can be used to inject KieContainers, KieBases, and KieSessions.

@KContainer, @KBase and @KSession all support an optional 'name' attribute. CDI typically does "getOrCreate" when it injects, all injections receive the same instance for the same set of annotations. the 'name' annotation forces a unique instance for each name, although all instance for that name will be identity equals.