Template version: v0.4.0

react-native-smartassets

This project is based on react-native-smartassets.

This third-party library has been migrated to Gitcode and is now available for direct download from npm, the new package name is:@react-native-ohos/react-native-smartassets.The version correspondence details are as follows:

Name Version Release Information Supported RN Version Supported Autolink Compile API Version Community Baseline Version npm Address
@react-native-ohos/react-native-smartassets ~ 1.0.6 Gitcode Releases 0.72.* / 0.82.* partially(>=1.0.7) API12+ 1.0.4 Npm Address
@react-native-oh-tpl/react-native-smartassets <=1.0.5@deprecated Github Releases(deprecated) 0.72.* No API12+ 1.0.4 Npm Address

1. Installation and Usage

Go to the project directory and execute the following instruction:

npm

npm install @react-native-ohos/react-native-smartassets

yarn

yarn add @react-native-ohos/react-native-smartassets

The following code shows the basic use scenario of the repository:

The name of the imported repository remains unchanged.

import React from 'react';
import { View, Image } from 'react-native';
import { SmartAssets } from 'react-native-smartassets';

export default function App() {
  // Initialize SmartAssets when the app starts
  SmartAssets.initSmartAssets();

  return (
    <View>
      {/* Use Image component normally, SmartAssets will handle asset loading automatically */}
      <Image 
        source={require('./assets/images/demo.png')} 
        style={{ width: 100, height: 100 }}
      />
    </View>
  );
}
Is supported autolink Supported RN Version
~1.0.6 Partially(>=1.0.7) 0.72/0.82
<= 1.0.5@deprecated No 0.72

Projects using AutoLink need to be configured according to this document, AutoLink framework guide.: https://gitcode.com/openharmony-sig/ohos_react_native/blob/master/docs/zh-cn/Autolinking.md

If the version you are using supports Autolink and the project has integrated Autolink, you can skip the ManualLink configuration.

ManualLink: This step provides guidance for manually configuring native dependencies.

Open the harmony directory of the HarmonyOS project in DevEco Studio.

2.1. Overrides RN SDK

To ensure the project relies on the same version of the RN SDK, you need to add an overrides field in the project's root oh-package.json5 file, specifying the RN SDK version to be used. The replacement version can be a specific version number, a semver range, or a locally available HAR package or source directory.

For more information about the purpose of this field, please refer to the official documentation.

{
  ...
  "overrides": {
    "@rnoh/react-native-openharmony" : "./react_native_openharmony"
  }
}

2.2. Introducing Native Code

Currently, two methods are available:

  • Use the HAR file.
  • Directly link to the source code.

Method 1 (recommended): Use the HAR file.

The HAR file is stored in the `harmony` directory in the installation path of the third-party library.

Open entry/oh-package.json5 file and add the following dependencies:

"dependencies": {
    "@rnoh/react-native-openharmony": "file:../react_native_openharmony",
    "@react-native-ohos/react-native-smartassets": "file:../../node_modules/@react-native-ohos/react-native-smartassets/harmony/smartassets.har"
  }

Click the sync button in the upper right corner.

Alternatively, run the following instruction on the terminal:

cd entry
ohpm install

Method 2: Directly link to the source code.

For details, see [Directly Linking Source Code](./link-source-code.md).

2.3. Configuring CMakeLists and Introducing SmartAssetsPackage

Open entry/src/main/cpp/CMakeLists.txt and add the following code:

+ set(OH_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")

# RNOH_BEGIN: manual_package_linking_1
+ add_subdirectory("${OH_MODULES}/@react-native-ohos/react-native-smartassets/src/main/cpp" ./smartassets)
# RNOH_END: manual_package_linking_1

# RNOH_BEGIN: manual_package_linking_2
+ target_link_libraries(rnoh_app PUBLIC smartassets)
# RNOH_END: manual_package_linking_2

Open entry/src/main/cpp/PackageProvider.cpp and add the following code:

#include "RNOH/PackageProvider.h"
#include "generated/RNOHGeneratedPackage.h"
+ #include "SmartAssetsPackage.h"

using namespace rnoh;

std::vector<std::shared_ptr<Package>> PackageProvider::getPackages(Package::Context ctx) {
    return {
        std::make_shared<RNOHGeneratedPackage>(ctx),
+       std::make_shared<SmartAssetsPackage>(ctx)
    };
}

2.4. Introducing SmartAssetsPackage to ArkTS

Open the entry/src/main/ets/RNPackagesFactory.ets file and add the following code:

  ...
+ import { SmartAssetsPackage } from '@react-native-ohos/react-native-smartassets';

export function createRNOHPackages(ctx: RNPackageContext): RNOHPackage[] {
  return [
+   new SmartAssetsPackage(ctx)
  ];
}

2.5. Running

Click the sync button in the upper right corner.

Alternatively, run the following instruction on the terminal:

cd entry
ohpm install

Then build and run the code.

3. Constraints

3.1. Compatibility

This document is verified based on the following versions:

  1. RNOH: 0.72.32; SDK: HarmonyOS-Next-DB6 5.0.0.61; IDE: DevEco Studio 5.0.3.706; ROM: 3.0.0.61;
  2. RNOH: 0.82.1; SDK: HarmonyOS 6.0.0 Release SDK; IDE: DevEco Studio 6.0.0.858; ROM: 6.0.0.112;

4. Static Methods

The **Platform** column indicates the platform where the properties are supported in the original third-party library.

If the value of **HarmonyOS Support** is **yes**, it means that the HarmonyOS platform supports this property; **no** means the opposite; **partially** means some capabilities of this property are supported. The usage method is the same on different platforms and the effect is the same as that of iOS or Android.

Name Description Type Required Platform HarmonyOS Support
initSmartAssets Initialize SmartAssets and set up image loading hook function yes iOS/Android yes
initSmartAssetsInner Internal initialization logic, set up resource resolution hook function no iOS/Android yes
setBundlePath Search for image resources in a specified path function no iOS/Android yes
setiOSRelateMainBundlePath Set iOS path to search for image resources function no iOS no

5. Known Issues

6. Others

  • The setiOSRelateMainBundlePath method is only supported on iOS platform in the original third-party library, therefore it is not supported on HarmonyOS platform.

7. License

This project is licensed under The MIT License (MIT).