Updated on 2025-12-04 GMT+08:00

Code Compilation and Project Settings

Compiling the SDK

  1. Use Android Studio to create an Android project and set the package name to com.huaweicloud.sdk.iot.device.

    Figure 1 Creating an Android project using Android Studio

  2. Copy the Java source code of the iot-device-sdk-android project to the com.huaweicloud.sdk.iot.device package.

    Figure 2 Copying the Java source code to the package

  3. Configure the build.gradle file in the app directory.

    1. Modify the code in the build.gradle file.
      Figure 3 Deleting configuration data from the Android SDK
    2. Add the following compilation scripts to the build.gradle file.
      task cleanJar(type: Delete){
          // Delete the existing data.
          delete 'build/libs/com.huaweicloud.sdk.iot.device-1.0.0.jar'
          delete 'build/libs/classes/'
      }
      
      task copyJavaclasses(type: Copy) {
      
          // Set the file to be copied.
          from('build/intermediates/javac/release/classes')
      
          // Store the JAR package.
          into('build/libs/')
      }
      
      task makeJar(type: Exec){
          workingDir 'build/libs/'
          commandLine 'cmd', '/c', 'jar cvf com.huaweicloud.sdk.iot.device-1.0.0.jar -C ./ .'
      }
      
      cleanJar.dependsOn(build)
      copyJavaclasses.dependsOn(cleanJar)
      makeJar.dependsOn(copyJavaclasses)
    3. Add the following three dependencies to the build.gradle file:
      implementation 'com.google.code.gson:gson:2.8.6'
      implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
      implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
    4. After the Android project is configured, run task makeJar in the build.gradle file (ensure that the Java environment variable has been added) to generate a JAR package.
      Figure 4 Generating a JAR package using the Android SDK
    5. Check whether the JAR package is stored in the app/build/libs directory.
      Figure 5 Obtaining the JAR package

Setting the Code Project

  1. Add the generated JAR package to the app/libs directory.
  2. Add the following dependencies to build.gradle:

    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
    implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
    implementation 'com.google.code.gson:gson:2.8.6'
    implementation group: 'org.bouncycastle', name: 'bcpkix-jdk15to18', version: '1.68'
    implementation group: 'org.bouncycastle', name: 'bcprov-jdk15to18', version: '1.68'
    implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.9.1'

  3. Add the following network permissions to the AndroidManifest.xml file:

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