目录

@compatibility/api-compatibility-check

Since DevEco Studio 6.0.1 Beta1, Code Linter supports version compatibility rules.

These rules detect if your project code calls APIs from a version higher than the value of compatibleSdkVersion set in your project configuration, which can lead to compatibility issues. To prevent these issues, you are advised to implement appropriate checks in your code.

Rule Configuration

// code-linter.json5
{
  "rules": {
    "@compatibility/api-compatibility-check": "warn"
  }
}

Options

This rule is not configurable.

Example of Correct Code

Example 1: Implement an SDK version check before calling the API.

import { dataUriUtils } from '@kit.AbilityKit';
import { deviceInfo } from '@kit.BasicServicesKit';

@Component
struct Test {
  build() {
    Text('hello').onClick(() => {
      
      if (deviceInfo.distributionOSApiVersion >= 60000) {
        dataUriUtils.getId('');
      }

      // Perform an SDK version check before using the API.
      if (deviceInfo.sdkApiVersion >= 20) {
        dataUriUtils.getId('');
      }
    })
  }
}

Example 2: Implement a null check before calling the API.

import { dataUriUtils } from '@kit.AbilityKit';

@Component
struct Test {
  build() {
    Text("hello").onClick(() => {
      // Perform a null check.
      if (dataUriUtils.getId !== undefined) {
        dataUriUtils.getId('');
      }
    })
  }
}

Example 3: Use try-catch for exception handling before calling the API.

import { dataUriUtils } from '@kit.AbilityKit'

@Component
struct Test {
  build() {
    Text('hello').onClick(() => {
      // Use the try-catch syntax.
      try {
        dataUriUtils.getId('');
      } catch (error) {
        // Handle exceptions.
      }
    })
  }
}

Example of Incorrect Code

// Set compatibleSdkVersion to 5.0.5(17) in the project.
import { ScrollEffectType } from '@kit.UIDesignKit';

@Component
struct Test {
  build() {
    Text('hello').onClick(() => {
      // ScrollEffectType.COMMON_BLUR is supported since version 5.1.0(18) and should not be called directly.
      const value = ScrollEffectType.COMMON_BLUR
      console.info(value.toString())
    })
  }
}

Rule Set

plugin:@compatibility/all

For details about how to configure Code Linter check rules, see Code Linter.