You are here: Platform Specific Issues > Android > Native Queries

Native Queries

Native Queries also work on Android. However there's a limitation. Android uses a special Java Virtual machine, which prevents optimizing Native Queries at runtime. The solution is to optimize the queries as build-time. See "Enhancement Tools"

Example Build Time Enhancement for Android

This example is based on the build time enhancements. This example only shows the important steps for optimizing the native queries for Android.

First define the enhancing task. It's important to also include the Android-platform libraries, otherwise some classes cannot be found.

<path  id="project.classpath">
    <pathelement  path="${basedir}/bin/classes"/>
	<!-- The sdk.dir points to the Android-platform libraries -->
    <fileset  dir="${sdk.dir}/platforms/${target}">
        <include  name="android.jar"/>
    <fileset  dir="libs">
        <include  name="**/*.jar"/>
<taskdef  name="db4o-enhance"
android-nq-optimisation.xml: Define the task for the enhancement

Then define the target which enhances the classes. This example only activates the Native Query optimization. However you can also activate TA or TP support if you like. See "Transparent Persistence"

<target  name="enhance">
    <db4o-enhance  classtargetdir="${basedir}/bin/classes"
                  nq="true"  ta="false"
        <classpath  refid="project.classpath"/>
        <sources  dir="${basedir}/bin/classes">
                  <include  name="**/*.class"/>
android-nq-optimisation.xml: Define a target which runs the task

The next step is to integrate this Ant-task in Eclipse. Right click on the project and then select 'Properties'. There switch to the 'Builders'-tab. Add a new 'Ant Builder'.

On the 'Main'-tab select the enhancement-script:

Then go to the 'Properties'-tab. There add a new property 'android.platform' which points to the right Android-platform and version-path. You can find the platforms in you Android-SDK. The platforms are in the folder 'platforms' in the Android SDK. For example when your using the API-level 7 the platform is in the folder 'AndroidSDK/platforms/android-7'

Apply all settings. The last step is to place the new created builder between the 'Java Builder' and the 'Android Package Builder':

Integrating in the Regular Build

You simply can include the enhancer step into the regular Android build. For that include the enhancer Ant script in the build.xml and let it run during the compilation phase:

<import  file="${sdk.dir}/tools/ant/build.xml"  />

<!-- Add the db4o step below the predefined android targets -->
<import  file="android-db4o-optimisation.xml"  />

<target  name="-post-compile"  depends="enhance">
build.xml: Run the enhancer step during the build