HarmonyOS development: Solve the problem of failure to import high version project from lower version of DevEco Studio

Preface

Based on DevEco Studio 4.0 Beta2, hvigorVersion is 3.0.2 , I developed a project and uploaded it to the remote warehouse. When my colleagues downloaded it, it never ran and errors were reported frequently. Since the APIs are all using 9, the first impression is that the development environment is different. , so I asked him to send his development environment, DevEco Studio 3.1.1 Release, hvigorVersion is 2.4.2 . Sure enough, the environment is different, so the solution is simple, either upgrade the IDE , or change the hvigorVersion to the current device; upgrade IDE can indeed be solved, but it is not the optimal solution. After all, in order to solve the problem, it is necessary to upgrade. Firstly, it is inconvenient. Secondly, most developers prefer the stable version and are really unwilling to upgrade to the Beta version; then The only option was the second way, but the second way could not be solved. I checked the official website and the community, but there was so little information.

In Android Studio, if the development environment is different, the problem can be easily solved by simply changing the gradle version number under build.gradle and the version number in the gradle-wrapper.properties file. However, in DevEco Studio, this method seems difficult to do. It works, but we have to solve it. After all, in many cases, it is difficult to achieve unified development environment. For example, if you download a third-party library and want to run it to see the effect, you may encounter different development environments and cannot Therefore , when you encounter a problem, you must solve it instead of avoiding it. Only in this way can you make things better and benefit the public.

After half a day of exploration, the problem has been fixed. Although looking back now, I feel that the solution to the problem is very simple, but with no information and unclear error messages, it is really impossible to solve it. Fortunately, my work was not in vain in the end. So I summarized it. If you also need to face such problems when developing HarmonyOS, I hope it can bring you some help.

The outline of this chapter is as follows:

1. Error message reported when a lower version IDE imports a higher version project

2. Lack of information, how to face difficulties

3. Modify files and solve problems step by step

4. Problem summary

1. Error message when a lower version IDE imports a higher version project

When your development environment is lower than the target project development environment, you will encounter the following error message after importing the project.

Two solutions are given, one is to change to the local hvigorVersion, and the other is to upgrade the IDE. When we click on the blue text, the solution will be automatically triggered. We click on the first method directly.

You can find that in the hvigor-config.json5 file, it has been changed to the version number of your device.

After compiling the project, you will find that it is useless. Although there are no errors, it cannot run.

This has reached the point where there is no solution, and the target, that is, the module that can be run, cannot be found.

2. How to face difficulties when information is scarce

When encountering the above problems, you must first go to the official website and explore the community. However, you have not found a solution at all. You have no choice but to investigate the solution yourself and imitate similar errors in Android Studio to solve DevEco Studio. Since The files and build tools have changed, and in the end it was difficult to identify the root problem, but inspiration came. Since the IDE can be compared, the project can also be compared.

The project structure of the high version and the low version is basically the same. Since it cannot run, in addition to the inconsistent version numbers, there must be some inconsistencies in the files, which causes the project to fail to compile. Based on this conjecture, all the files of the high and low version projects , using a one-to-one analogy, it has been verified that this method is feasible, but it is very time-consuming.

In order to reduce the number of analog files, we simply adopted the structure of an empty project, which is an analogy to a newly created project. Finally, we found that there were many file inconsistencies, as follows:

1. hvigorfile.ts file under the root project

Version 4.0 or above

import { appTasks } from '@ohos/hvigor-ohos-plugin';

export default {
    system: appTasks,  /* Built-in plugin of Hvigor. It cannot be modified. */
    plugins:[]         /* Custom plugin to extend the functionality of Hvigor. */
}

Versions below 4.0

// Script for compiling build behavior. It is built in the build plug-in and cannot be modified currently.
export { appTasks } from '@ohos/hvigor-ohos-plugin';

2. hvigorfile.ts file under entry (main module)

Version 4.0 or above

import { hapTasks } from '@ohos/hvigor-ohos-plugin';

export default {
    system: hapTasks,  /* Built-in plugin of Hvigor. It cannot be modified. */
    plugins:[]         /* Custom plugin to extend the functionality of Hvigor. */
}

Versions below 4.0

// Script for compiling build behavior. It is built in the build plug-in and cannot be modified currently.
export { hapTasks } from '@ohos/hvigor-ohos-plugin';

3. The build-profile.json5 file under the root project

Version 4.0 or above

{
  "app": {
    "signingConfigs": [],
    "compileSdkVersion": 9,
    "compatibleSdkVersion": 9,
    "products": [
      {
        "name": "default",
        "signingConfig": "default",
      }
    ],
    "buildModeSet": [
      {
        "name": "debug",
      },
      {
        "name": "release"
      }
    ]
  },
  "modules": [
    {
      "name": "entry",
      "srcPath": "./entry",
      "targets": [
        {
          "name": "default",
          "applyToProducts": [
            "default"
          ]
        }
      ]
    }
  ]
}

Versions below 4.0

{
  "app": {
    "signingConfigs": [],
    "compileSdkVersion": 9,
    "compatibleSdkVersion": 9,
    "products": [
      {
        "name": "default",
        "signingConfig": "default",
      }
    ]
  },
  "modules": [
    {
      "name": "entry",
      "srcPath": "./entry",
      "targets": [
        {
          "name": "default",
          "applyToProducts": [
            "default"
          ]
        }
      ]
    }
  ]
}

4. hvigorw file under the root project (can be ignored)

Although the hvigorw file was found to be inconsistent, only one line of content was different, HVIGOR_APP_HOME="`pwd -P`" and HVIGOR_APP_HOME=$(dirname $(readlink -f $0)). After subsequent verification, it was of no use, so this file, everyone It can be ignored and there is no need to modify this file.

Version 4.0 or above

#!/bin/bash

# ----------------------------------------------------------------------------
#  Hvigor startup script, version 1.0.0
#
#  Required ENV vars:
#  ------------------
#    NODE_HOME - location of a Node home dir
#    or
#    Add /usr/local/nodejs/bin to the PATH environment variable
# ----------------------------------------------------------------------------

HVIGOR_APP_HOME="`pwd -P`"
HVIGOR_WRAPPER_SCRIPT=${HVIGOR_APP_HOME}/hvigor/hvigor-wrapper.js
warn() {
	echo ""
	echo -e "\033[1;33m`date '+[%Y-%m-%d %H:%M:%S]'`$@\033[0m"
}

error() {
	echo ""
	echo -e "\033[1;31m`date '+[%Y-%m-%d %H:%M:%S]'`$@\033[0m"
}

fail() {
	error "$@"
	exit 1
}

# Determine node to start hvigor wrapper script
if [ -n "${NODE_HOME}" ];then
   EXECUTABLE_NODE="${NODE_HOME}/bin/node"
   if [ ! -x "$EXECUTABLE_NODE" ];then
       fail "ERROR: NODE_HOME is set to an invalid directory,check $NODE_HOME\n\nPlease set NODE_HOME in your environment to the location where your nodejs installed"
   fi
else
   EXECUTABLE_NODE="node"
   which ${EXECUTABLE_NODE} > /dev/null 2>&1 || fail "ERROR: NODE_HOME is not set and not 'node' command found in your path"
fi

# Check hvigor wrapper script
if [ ! -r "$HVIGOR_WRAPPER_SCRIPT" ];then
	fail "ERROR: Couldn't find hvigor/hvigor-wrapper.js in ${HVIGOR_APP_HOME}"
fi

# start hvigor-wrapper script
exec "${EXECUTABLE_NODE}" \
	"${HVIGOR_WRAPPER_SCRIPT}" "$@"

Versions below 4.0

#!/bin/bash

# ----------------------------------------------------------------------------
#  Hvigor startup script, version 1.0.0
# 
#  Required ENV vars:
#  ------------------
#    NODE_HOME - location of a Node home dir
#    or 
#    Add /usr/local/nodejs/bin to the PATH environment variable
# ----------------------------------------------------------------------------

HVIGOR_APP_HOME=$(dirname $(readlink -f $0))
HVIGOR_WRAPPER_SCRIPT=${HVIGOR_APP_HOME}/hvigor/hvigor-wrapper.js
warn() {
	echo ""
	echo -e "\033[1;33m`date '+[%Y-%m-%d %H:%M:%S]'`$@\033[0m"
}

error() {
	echo ""
	echo -e "\033[1;31m`date '+[%Y-%m-%d %H:%M:%S]'`$@\033[0m"
}

fail() {
	error "$@"
	exit 1
}

# Determine node to start hvigor wrapper script
if [ -n "${NODE_HOME}" ];then
   EXECUTABLE_NODE="${NODE_HOME}/bin/node"
   if [ ! -x "$EXECUTABLE_NODE" ];then
       fail "ERROR: NODE_HOME is set to an invalid directory,check $NODE_HOME\n\nPlease set NODE_HOME in your environment to the location where your nodejs installed"
   fi
else
   EXECUTABLE_NODE="node"
   which ${EXECUTABLE_NODE} > /dev/null 2>&1 || fail "ERROR: NODE_HOME is not set and not 'node' command found in your path"
fi

# Check hvigor wrapper script
if [ ! -r "$HVIGOR_WRAPPER_SCRIPT" ];then
	fail "ERROR: Couldn't find hvigor/hvigor-wrapper.js in ${HVIGOR_APP_HOME}"
fi

# start hvigor-wrapper script
exec "${EXECUTABLE_NODE}" \
	"${HVIGOR_WRAPPER_SCRIPT}" "$@"

3. Modify files and gradually solve problems

After comparing the high and low project files one by one, a new project only has three different files (the other one can be ignored), namely the hvigorfile.ts file of the root project, the build-profile.json5 file of the root project and the hvigorfile in the module. .ts file, of course, the new project has only one module. If there are multiple modules, there will be more than three. It should be noted that no matter how many modules there are, the hvigorfile.ts file must be modified, and the lower version be consistent.

1. Modify the hvigorfile.ts file of the root project

before fixing

import { hapTasks } from '@ohos/hvigor-ohos-plugin';

export default {
    system: hapTasks,  /* Built-in plugin of Hvigor. It cannot be modified. */
    plugins:[]         /* Custom plugin to extend the functionality of Hvigor. */
}

After modification

export { hapTasks } from '@ohos/hvigor-ohos-plugin';

Note: It is not necessary to simply delete the following code, but also to change the import to export, otherwise the following error will be reported. At the beginning, I was careless and forgot to change it. This error also tortured me for a long time.

2. Modify the build-profile.json5 file of the root project

After you modify the hvigorfile.ts file of the root project and compile it impatiently, the following error will be reported. So, try to modify the files uniformly before compiling, otherwise various problems will come.

before fixing

{
  "app": {
    "signingConfigs": [],
    "compileSdkVersion": 9,
    "compatibleSdkVersion": 9,
    "products": [
      {
        "name": "default",
        "signingConfig": "default",
      }
    ],
    "buildModeSet": [
      {
        "name": "debug",
      },
      {
        "name": "release"
      }
    ]
  },
  "modules": [
    {
      "name": "entry",
      "srcPath": "./entry",
      "targets": [
        {
          "name": "default",
          "applyToProducts": [
            "default"
          ]
        }
      ]
    }
  ]
}

After modification

That is, delete the buildModeSet field.

{
  "app": {
    "signingConfigs": [],
    "compileSdkVersion": 9,
    "compatibleSdkVersion": 9,
    "products": [
      {
        "name": "default",
        "signingConfig": "default",
      }
    ]
  },
  "modules": [
    {
      "name": "entry",
      "srcPath": "./entry",
      "targets": [
        {
          "name": "default",
          "applyToProducts": [
            "default"
          ]
        }
      ]
    }
  ]
}

3. Modify the hvigorfile.ts file in the main module

If after you change the above two files, you still can't control yourself and still can't wait to compile, I'm sorry, you will still encounter the following errors, so, Tiezi, listen to others' advice and be sure to compile all After all the files have been changed, you will not encounter errors when you compile again.

before fixing

import { hapTasks } from '@ohos/hvigor-ohos-plugin';

export default {
    system: hapTasks,  /* Built-in plugin of Hvigor. It cannot be modified. */
    plugins:[]         /* Custom plugin to extend the functionality of Hvigor. */
}

After modification

export { hapTasks } from '@ohos/hvigor-ohos-plugin';

Note: It is not necessary to simply delete the following code, but also to change the import to export, otherwise an error will be reported.

4. Compile and run successfully

After the above three files are modified, they can be compiled and run successfully.

4. Problem summary

1. The most important thing is to modify the hvigorfile.ts file under each module. Modify as many as there are and don’t miss them.

2. When modifying the file under the hvigorfile.ts file, in addition to deleting the code, you must also change the import to export, otherwise an error will be reported.

3. The main module, dynamic shared package and static shared package modules are modified in different ways. You need to understand that one is hapTasks

, one is harTasks.

Main module/dynamic shared package

export { hapTasks } from '@ohos/hvigor-ohos-plugin';

static shared package

export { harTasks } from '@ohos/hvigor-ohos-plugin';

4. If you have multiple modules, you need to add modules in the modules in the build-profile.json5 file of the root project, as shown below:

"modules": [
    {
      "name": "entry",
      "srcPath": "./entry",
      "targets": [
        {
          "name": "default",
          "applyToProducts": [
            "default"
          ]
        }
      ]
    },
    {
      "name": "xx",
      "srcPath": "./xx"
    }
  ]

Okay, that’s it for this article. If you encounter similar problems, I hope it can help you.

Guess you like

Origin blog.csdn.net/ming_147/article/details/133092778