HarmonyOS development: walk into the dependency and use of static shared packages

foreword

In the last article, we developed and used the dynamic shared package. Due to the limitations of the dynamic shared package, for example, calling the shared package resource must be called through the tool class, and for example, it is only used for application internal code, For the sharing of resources, if I want to open source and use it for anyone who wants to use it in a remote dependent way, the dynamic sharing package cannot be realized. Is there any way to solve the above problems? Must have, this is today's protagonist, the static shared package .

Static shared package (Harmony Archive) HAR can contain code, C++ library, resources and configuration files, and can be used as a dependency for one or more modules. Unlike the dynamic shared package, it cannot be run independently, but can only be used as a dependency item to use.

Many usage methods of the static shared package are the same as those of the dynamic shared package. Considering that most of them are exposed to HarmonyOS development for the first time, in order to make it easier for everyone to learn and practice more intuitively, here I also describe the process of the static shared package from the beginning to the end. Let me explain at the end.

The content outline of this chapter is as follows:

1. How to create a static shared package (HAR)

2. Static shared package local dependency mode

3. Various calling methods of static shared packages

4. Static shared package obfuscation file

5. Summary

1. How to create a Static Shared Package (HAR)

Like the dynamic shared package, right click on the current project name, select New, and then select Module.

Select Static Library.

Set the shared package name and click Finish.

Unlike the dynamic shared package, the type of the static shared package is har.

2. Static shared package local dependency mode

1. Internal dependencies of the project

The internal dependency method of the project is exactly the same as the dynamic shared package, which can be manually dependent or commanded:

Manual method 1, remember the format: 【"folder": "file:../folder"】

"staticlibrary": "file:../staticlibrary"

Manual method 2: format, 【"@xx/xx": "file:../staticlibrary"】

"@ohos/library": "file:../staticlibrary"

After importing, to synchronize the project, click Sync Now. Of course, you can, and a prompt will appear when you place the mouse on the error report. Click Run 'ohpm install' in the prompt box.

The command mode depends on:

In the command line or in the IDE Terminal window, enter the directory under the module that needs to be referenced, and execute the ohpm install ../folder command. The folder is your shared package, as shown in the following figure:

After the command is executed, it will automatically add dependencies in oh-package.json5, and generate a mapping file, which is mapped to the oh_modules directory of the calling Module:

2. Dependence between different projects

Above, whether it is a dynamic shared package or a static shared package, it is a dependency between the same project. However, in many scenarios, in order to reuse the code, we will have multiple projects to reuse the shared package, such as network sharing package, various tools share packages, etc., so how to achieve this way?

For example, in another project MyLibrary, there is a static shared package net. How do we use it in the Demo project? Of course, this is a simple example. It can be any project, or any static shared package. The problem is that it is not under one project.

Step 1: Compile the library module

Select the static shared package that needs to be shared, and click the Build > Make Module option in the menu bar, as shown in the figure below:

After the build is complete, a .har file will be generated under build->default->outputs->default, as shown below:

Step 2: Depend on the project that needs to call this static shared package

Remember, use the format ["@xx/xx": "file:../xx.har"], otherwise you cannot rely on it.

The above method is not conducive to the collaboration between teams. After all, you have this .har in your device, and others may not have it. Therefore, in order to better team collaboration, there is another simple way to copy the .har file to In the project, the specific directory is defined by yourself, as shown in the figure below, I created a libs directory for storing .har files

In the above-mentioned dependency methods, whether it is the same project or different projects, there are certain limitations in actual development. For example, if it is the same project, then the reuse between projects cannot be realized. If it is different For the project, even if the .har is copied into the project to achieve team collaboration, if the har is updated, it has to be replaced, which is really inconvenient.

Is there a remote Maven form similar to that in Android? I can tell you responsibly that there must be, and then the remote dependency method. Since there are many contents, we will put it in the next document to explain.

3. Multiple calling methods of static shared package

Whether you use the same project or different projects, or remote dependencies, the essential implementation is the same.

1. Resource call

The resource call is relatively simple, and can be called directly, without the transfer processing like the dynamic sharing package.

For example, in the static shared package, there is a string resource, of course, it can also be other resources, such as color, size, audio and video, etc., all are fine.

Other modules that refer to this static shared package can be called directly, according to the $r('app.type.name') format, type is the resource type you define yourself, and name is the resource name you define, such as the resource in the above figure , we can call it as follows. After all, it is a static shared package, which can be called in exactly the same way as the current module.

$r('app.string.static_name')

Regarding resource types, HarmonyOS provides a variety of resource types, which were introduced in detail in the previous chapter on understanding the project structure. Here is a brief list:

document

overview

boolean.json

Boolean

color.json

color

float.json

floating point

intarray.json

integer array

integer.json

integer

pattern.json

style

plural.json

plural form

strarray.json

array of strings

string.json

string value

media

Multimedia files, such as graphics, video, audio and other files, supported file formats include: .png, .gif, .mp3, .mp4, etc.

rawfile

Used to store raw resource files in any format: $rawfile('filename')"

2. Method call

For example, in the static shared package, there is an add summation method, the code is as follows, remember that if you want to expose it, you must use the keyword export.

Other modules that refer to this static shared package can be called as follows, just call the add method directly, and there will be a prompt to import the package. If not, you can import the package according to the package import method in the figure.

3. Class call

Like the method call, we create a class that needs to be exposed in the static shared package. Here I use a log printing class as a test. Remember that the keyword export must be added to the class that needs to be exposed to the outside world.

The calling method is exactly the same as the method:

4. Component call

There is a custom text component in the static shared package, the displayed content is: Hello static library, as follows:

The calling method is as follows, which can be directly displayed as a view:

5. Jump to the page in the static shared package

After verification, HAR does not support declaring the pages page in the configuration file. If the verification is wrong, I hope you can point it out.

6. Precautions

Externally exposed interfaces need to be declared in the shared package entry file index.ets, otherwise, other modules cannot be called, and they are all fixed modes, such as the above-mentioned add method, and custom components, etc., remember! ! !

In the same class, if there are multiple methods, they can be reused, such as:

export { Log, add, minus } from './utils/test'

4. Static shared package obfuscation file

Confusion, in development, as the name suggests, is to turn easy-to-understand codes into behaviors that are difficult to read and understand. The purpose is to ensure the safety of the code and avoid leakage. If it is only used by your own company, there is really no need to confuse it. After all, they are all the same As a company, there is no need to be so harsh. If it is an open source code, for its own rights and interests, only open functions, and keep the security of the code, then it can be confused. Of course, if it is not absolute, it still needs to be specific. Specific analysis.

Let's take a look at the effect before obfuscation. All source codes can be clearly viewed in oh_modules:

Let’s look at the effect of obfuscation together, and it has become two files, from the original Net.ts to Net.d.ts and Net.js. Looking at the source code, the original style can no longer be viewed .

How to confuse is very simple, and then you need to add the following properties to the build-profile.json5 file of the static shared package:

The artifactType field has the following two values, and the default is original.

original: Do not confuse.

obfuscation: Obfuscation, currently only supports uglify obfuscation.

It should be noted that, according to the official interpretation, when the artifactType field is set to obfuscation, the apiType field must be set to stageMode , because the Stage model only supports obfuscation.

V. Summary

The static shared package solves the problem of code reuse in multi-project development, and also realizes the convenience of code function encapsulation for direct remote use, and provides a convenient use of open source. This is the dynamic shared package that only applies internal code and resource sharing. It cannot be compared. Of course, the two shared packages have their own advantages and disadvantages. How to choose is your own decision.

In fact, regarding the static shared package .har, have you noticed that after the dynamic shared package is built and compiled, a .har file will also be created. Yes, you can also develop your own static shared package completely through the dynamic shared package .

In the next article, we will introduce how to realize remote dependency and upload of the static shared package and how to build a private server of the static shared package , so stay tuned!

Guess you like

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