Using Android bindings

Here you can find out how to generate Android bindings using Android Studio. For creating Android bindings, Kanzi Connect is tested to work with Android Studio version 4.0.1 on Windows.

Creating an Android application that uses Kanzi Connect bindings

To create an Android application that uses Kanzi Connect bindings:

  1. In Android Studio create an Android Studio project where you want to use the Kanzi Connect bindings. For example, select File > New > New Project and in the Create New Project window:

    • Select a project template

    • Set Name to ConnectTestApplication

    • Set Company domain to yourcompany.com

    If you use different application name and company domain, you must make changes in the code used later in this topic.

    ../../_images/create-new-project---select-project-template.png
  2. Select File > New > New Module, in the Create New Module window, select Import JAR/AAR Package.

    ../../_images/create-new-module---select-module-type.png
  3. Select the AAR file that you want to import and click Finish.

    For example, select the kanziconnect-release.aar.

    ../../_images/create-new-module---import-module-from-library.png
  4. Select File > Project Structure, select Modules > app and in the Properties tab set the properties.

    ../../_images/project-structure---modules-.png
  5. In the Project Structure window, select app Dependencies, under Declared Dependencies click +, select Module Dependency, and select the modules that you want to add as dependencies.

    ../../_images/project-structure--dependencies---01.png ../../_images/project-structure--dependencies---02.png ../../_images/project-structure--dependencies---03.png
  6. (Optional) This step is required if you want to use the generated <ServiceName>RuntimeDataListener.java class in your project.

    1. To enable the data bindings set, in Android Studio open the build.gradle(:app) file and add

      dataBinding.enabled = true
      

      If you do not enable the data bindings, the project compiles successfully, but cannot run on a target device.

    2. To connect to a Kanzi Connect Server, you need protobuf library that is located in <KanziConnectInstallation>/ext/platforms/android_gradle/lib.

      Edit build.gradle(:app) to get the protobuf library and pack into the apk

      apply plugin: 'com.android.application'
      //begin kanziconnect
      if (!System.env.KANZI_CONNECT_SDK) {
          throw new FileNotFoundException('Missing environment variable: KANZI_CONNECT_SDK')
      }
      def connectSdkDir = System.getenv('KANZI_CONNECT_SDK')
      def thirdPartyLibDir = new File(connectSdkDir, "/ext/platforms/android_gradle/lib")
      //end kanziconnect
      android {
          compileSdkVersion 29
          buildToolsVersion "29.0.3"
          // begin kannziconnect
          dataBinding.enabled = true
          // end kanziconnect
          defaultConfig {
              applicationId "com.rightware.connecttestapplication"
              minSdkVersion 28
              targetSdkVersion 29
              versionCode 1
              versionName "1.0"
      
              testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
              //begin kanziconnect
              sourceSets {
                  main {
                      jniLibs.srcDirs = [thirdPartyLibDir]
                  }
              }
              //end kanziconnect
          }
      
          buildTypes {
              release {
                  minifyEnabled false
                  proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
              }
          }
          compileOptions {
              sourceCompatibility JavaVersion.VERSION_1_8
              targetCompatibility JavaVersion.VERSION_1_8
          }
          dependenciesInfo {
              includeInApk true
              includeInBundle true
          }
          // begin kanziconnect
          packagingOptions {
              pickFirsts += ["**/libprotobuf.so"]
          }
          //end kanziconnect
      }
      
    3. Now you can import the Kanzi Connect classes to your application with a regular Java import statement.

      import com.rightware.connect.*;
      
      ../../_images/mainactivity-code-example.png

When you create an Android application in which you want to use Kanzi Connect bindings you can integrate Kanzi Connect API to Android Activity. See Integrating Kanzi Connect API to Android Activity.

Integrating Kanzi Connect API to Android Activity

To integrate Kanzi Connect, use the KanziConnectContext class included in the AAR package.

To integrate Kanzi Connect API to Android Activity:

  1. Add the Kanzi Connect code to relevant locations in your code of the main activity of your application

    // begin kanziconnect
    import com.rightware.connect.*;
    // end kanziconnect
    
    public class MainActivity extends AppCompatActivity {
    
        // begin kanziconnect
        private static KanziConnectContext m_KCContext;
        // end kanziconnect
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // begin kanziconnect
            if (m_KCContext == null) {
                m_KCContext = new KanziConnectContext(3 /*info level logging*/);
                m_KCContext.initialize(ConfigurationReader.getConnectionParametersStatic(""), null);
                m_KCContext.start();
            }
            // end kanziconnect
    
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });
        }
    
  2. In Android Studio select Build > Rebuild Project and run your application.

    When you start your application, it connects to the Kanzi Connect server located at localhost.

    Kanzi Connect AAR supports native libraries for armeabi-v7a, arm64-v8a, x86, and x86_64. You can run the application on these targets or in the emulator.

  3. ConfigurationReader component used in the above integration example requires READ_EXTERNAL_STORAGE permission to search and read the connection.xml file from SD card. If required, use Android settings application to grant the permissions.

Generating Android bindings for a service

Java bindings for a service are created on Windows using generate_java_bindings.bat helper file. It takes these arguments:

  • The service interface to process

  • The output directory where to store the generated Java files.

For example, to generate on Windows the Java bindings for the Media service and store them to the c:/temp/media/ use

generate_java_bindings.bat definitions/media_interface.xml c:/temp/media/

On Linux you can generate the files using the generate.py.

This command creates these Java files for each service interface:

File name

Description

<ServiceName>ServiceConcept.java

This class implements the service functionality and integrates it towards generic android Java bindings. Do not manually edit this file.

<ServiceName>Service.java

You can use this stub file as a basis for service implementation. It contains empty functions for all the service methods and extends the Concept class. The script does not generate this file if one already exists at the destination directory.

<ServiceName>Client.java

This is a client-side interface for the mentioned service. It has a method for each method defined in service description and defines a callback interface to receive events.

<ServiceName>RuntimeDataListener.java

Creates a listener for the service runtime data. Changes for each value within the runtime data can be observed independently.