Fast Integration with Smart Life App SDK for Android

Last Updated on : 2024-06-26 09:58:05download

This topic describes how to quickly integrate Tuya Smart Life App SDK for Android into your development environment, such as Android Studio. It also sheds light on the initialization method and how to enable the debugging mode with a few simple steps. This allows you to run the demo app and get started with your smart life app development by using the App SDK.

Prerequisites

  • Before you start, make sure that you have performed the steps in Preparation.

    • Starting from the Smart Life App SDK for Android 3.29.5, SHA256 hash values are required. For more information, see How to Get SHA1 and SHA256 Keys.
      Note: If you need to launch apps on Google Play, check whether the app re-signing feature is enabled. If so, configure the SHA256 hash values generated by Google on the Tuya Developer Platform. Otherwise, the error “illegal client” will occur. For more information, see How to check app signing on Google Play?

    • Starting from the Smart Life App SDK for Android 4.0.0, the package name for Android configured on the Tuya Developer Platform must be the same as packageName in your project. Otherwise, the error ILLEGAL_CLIENT_ID will arise.

  • If you have not installed Android Studio, visit the Android Studio official website to download Android Studio.

Integrate App SDK

Step 1: Create an Android project

Create a project in Android Studio.

Step 2: Configure build.gradle

Add dependencies to the build.gradle file of the Android project.

android {
	defaultConfig {
		ndk {
			abiFilters "armeabi-v7a", "arm64-v8a"
		}
	}
	packagingOptions {
		pickFirst 'lib/*/libc++_shared.so' // An Android Archive (AAR) file contains an Android library. If the .so file exists in multiple AAR files, select the first AAR file.
	}
}
dependencies {
	implementation 'com.alibaba:fastjson:1.1.67.android'
	implementation 'com.squareup.okhttp3:okhttp-urlconnection:3.14.9'

	// The latest stable App SDK for Android.
	implementation 'com.tuya.smart:tuyasmart:4.0.7'
}

Add the Tuya IoT Maven repository URL to the build.gradle file in the root directory.

repositories {
	jcenter()
        maven { url 'https://maven-other.tuya.com/repository/maven-releases/' }
        maven { url "https://maven-other.tuya.com/repository/maven-commercial-releases/" }
        maven { url 'https://jitpack.io' }
	google()
        mavenCentral()
	maven { url 'https://maven.aliyun.com/repository/public' }
	maven { url 'https://central.maven.org/maven2/' }
	maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
	maven { url 'https://developer.huawei.com/repo/' }
	}
}
  • App SDK v4.0 and later are classified into Trial and Official editions. The Official edition can be used only after you pay for an annual subscription. For more information, see Pricing.

  • App SDK v3.10.0 and earlier only support armeabi-v7a. App SDK v3.11.0 and later have integrated armeabi-v7a and arm64-v8a. If you have added .so libraries to the project, you must remove them and only use the library included in the SDK.

  • If you want to integrate the .so library of a later version, you must remove the library of an earlier version to avoid conflicts or other possible issues.

Step 3: Configure the security image, AppKey, and AppSecret

  1. Log in to the Tuya Developer Platform, go to the SDK Development page, and then click the SDK to be managed.

  2. On the page that appears, click the Get Key tab and click Download in the App Security Image for Android field.

    Fast Integration with Smart Life App SDK for Android
  3. Rename the security image as t_s.bmp and put the image in the assets folder of your project.

    Fast Integration with Smart Life App SDK for Android
  4. Return to the Android project, configure appkey and appSecret in AndroidManifest.xml, and then set permissions for the app.

    <meta-data
    android:name="TUYA_SMART_APPKEY"
    android:value="AppKey" />
    <meta-data
    android:name="TUYA_SMART_SECRET"
    android:value="AppSecret" />
    

Step 4: Obfuscate the code

Configure obfuscation in proguard-rules.pro.

#fastJson
-keep class com.alibaba.fastjson.**{*;}
-dontwarn com.alibaba.fastjson.**

#mqtt
-keep class com.tuya.smart.mqttclient.mqttv3.** { *; }
-dontwarn com.tuya.smart.mqttclient.mqttv3.**

#OkHttp3
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**

-keep class okio.** { *; }
-dontwarn okio.**

-keep class com.tuya.**{*;}
-dontwarn com.tuya.**

Step 5: Initialize the SDK

Initialize the SDK in Application. Make sure that all processes are initialized. Example:

public class TuyaSmartApp extends Application {
	@Override
	public void onCreate() {
		super.onCreate();
		TuyaHomeSdk.init(this);
	}
}

Configure appkey and appSecret in AndroidManifest.xml, or run the initialization code.

TuyaHomeSdk.init(Application application, String appkey, String appSerect)

Step 6: Disable the cloud connection

Before you exit the app, you must call the following operation to disable the cloud connection.

TuyaHomeSdk.onDestroy();

Step 7: Enable or disable logging

  • In debug mode, you can enable SDK logging to facilitate troubleshooting.

  • We recommend that you disable logging in release mode.

    TuyaHomeSdk.setDebugMode(true);
    

If you integrate with the SDK v3.32.5 or earlier, remove the dependency tuyamsmart-log from the build.gradle file of the Android project. This setting is not required for SDK versions later than v3.32.5.

implementation("com.tuya.smart:tuyasmart:3.32.5"){
        exclude group: 'com.tuya.smart', module: 'tuyasmart-log'
    }

Run the demo app

  • After you integrate the SDK, you can get the security image, AppKey, and AppSecret. Make sure that the security image, AppKey, and AppSecret are consistent with those used on the Tuya Developer Platform. Any mismatch will cause the SDK development to be failed. For more information, see Step 3: Configure the security image, AppKey, and AppSecret.
  • The demo app that is created in the sample project of Smart Life App SDK is used for reference only. Do not use the demo app for commercial purposes. For more information, see Tuya Developing Service Agreement.

In the following example, a demo app is used to describe the process of app development with the App SDK and indicate the SDK capabilities that you can integrate to implement smart life scenarios. Before the development of your app, we recommend that you run the demo app as needed.

Feature overview

The demo app supports the following features:

  • User management: Register and log in to the app account by mobile phone number or email address.

  • Home and device management

    • Create a home and switch between homes.
    • Display a list of devices in the home and control data points (DPs) of the devices.
    • Rename and remove devices.
  • Pair devices: Multiple pairing methods are supported, including EZ Mode, AP Mode, Bluetooth Low Energy, Zigbee Gateway, Zigbee Subdevice, QR Code, and Bluetooth LE Mesh Sub Device.

    Fast Integration with Smart Life App SDK for Android

For more information, see the GitHub projects tuya-home-android-sdk-sample-java or tuya-home-android-sdk-sample-kotlin.

Run the sample

  1. Choose app > build.gradle and change the value of applicationId to your app package name.

    Fast Integration with Smart Life App SDK for Android

  2. Make sure that you have finished the operations in Step 3: Configure the security image, AppKey, and AppSecret.

  3. Click Run to run the sample.

FAQs

SING_VALIDATE_FALED

  • Troubleshoot the error message: Permission Verification Failed

    • Problem: When the system runs the demo app, an error message is returned in the following response:
    {
    	"success": false,
    	"errorCode" : "SING_VALIDATE_FALED",
    	"status" : "error",
    	"errorMsg" : "Permission Verification Failed",
    	"t" : 1583208740059
    }
    
    • Solutions:

      • Check whether your security image, AppKey, and AppSecret are correctly configured and consistent with those obtained in Preparation.
      • Check whether the security image is placed in the correct directory and whether the file name is t_s.bmp.

ILLEGAL_CLIENT_ID

  • After the Smart Life App SDK for Android is updated to v3.29.5, an error message is returned to indicate an illegal client. How do I troubleshoot this problem? (ILLEGAL_CLIENT_ID)

    • Starting from the Smart Life App SDK for Android 3.29.5, data security is enhanced and SHA256 hash values are required.
    • You must follow the steps in the documentation to get the SHA256 hash values and add the hash values to the Tuya Developer Platform. For more information, see Preparation.
  • Why has the error ILLEGAL_CLIENT_ID occurred after the upgrade to Smart Life App SDK 4.0.0?

    • Starting from the Smart Life App SDK 4.0.0, data security is enhanced.
    • The package name for Android configured on the Tuya Developer Platform must be the same as packageName in your project. Otherwise, the error ILLEGAL_CLIENT_ID will arise.
  • After SHA256 hash values are configured on the Tuya Developer Platform and the sample is run, the error message that indicates an illegal client is still returned. How do I troubleshoot the problem?

    • Solution: Before you run the sample, add the following signature information to build.gradle of the app module.

      	android {
      			...
      			signingConfigs {
      			debug {
      				storeFile file('xxxx.jks')
      				storePassword 'xxxxxx'
      				keyAlias 'xxx'
      				keyPassword 'xxxxxx'
      			}
      			release {
      				storeFile file('xxxx.jks')
      				storePassword 'xxxxxx'
      				keyAlias 'xxx'
      				keyPassword 'xxxxxx'
      			}
      	}
      }