Best Practices for Publishing an SDK to the OpenHarmony Central Repository in HarmonyOS Next
Background Introduction
Previously, during the adaptation of Mars in HarmonyOS Next, we submitted a PR to the official Mars repository for xlog compatibility. Some developers privately requested compiled xlog dynamic libraries, so we decided to open-source the qlog source code (https://gitcode.com/wodekouwei/qlog) for easier access. To facilitate usage, we aimed to upload the compiled binaries to the OpenHarmony central repository. This article documents the publishing process to help other developers.
QLog Introduction
QLog is a persistent logging library that asynchronously encrypts and compresses console logs into files, making it easier for developers to view and analyze logs, especially for troubleshooting online issues.
QLog is based on WeChat's XLog, a high-performance logging system designed for WeChat's mobile clients (Android/iOS). Key features include:
-
Encryption & Security
- Log Encryption: All logs are encrypted (e.g., AES) during storage and transmission to prevent sensitive data leaks and comply with privacy regulations.
- Tamper-Proof: Encrypted logs cannot be modified, ensuring integrity and trustworthiness.
-
High Performance & Low Overhead
- Asynchronous Writing: Logs are written in background threads to avoid blocking the main thread, ensuring app smoothness.
- Memory-Mapped Files (mmap): Direct file writing via memory mapping reduces I/O operations, improving efficiency.
- Compression: Logs are compressed before storage or upload, reducing disk usage and network traffic.
-
Flexible Log Levels
- Supports multiple log levels (VERBOSE, DEBUG, INFO, WARN, ERROR), allowing developers to filter logs as needed.
- Dynamic log level adjustment (e.g., only ERROR logs in production) balances debugging and performance.
-
Log Retrieval Mechanism
- Remote Trigger: Servers can instruct clients to upload specific logs without user intervention, simplifying issue diagnosis.
- Conditional Filtering: Logs can be retrieved by time, user ID, log level, etc., for precise debugging.
-
Fault Tolerance & Stability
- Error Handling: Automatically downgrades or caches logs if writing fails or storage is full, preventing crashes.
- Chunked Storage: Logs are split by size or time to avoid performance issues with large files.
-
Cross-Platform Support
- Supports Android and iOS with a unified API, reducing maintenance costs.
-
Selective Logging Control
- Non-critical logs can be disabled to save resources in production.
- Dynamic module-level logging enables targeted debugging.
-
Typical Use Cases
- Issue Tracking: Encrypt and retrieve logs to diagnose user-reported crashes.
- Performance Monitoring: Record key metrics (e.g., startup time) with minimal overhead.
- Security Auditing: Encrypted logs provide tamper-proof records of sensitive operations.
XLog balances security, performance, and flexibility, making it ideal for large-scale mobile apps. QLog brings these capabilities to the HarmonyOS platform.
Usage
Dependency Installation
ohpm i @qing/qlog
For OpenHarmony ohpm
setup, refer to How to Install OpenHarmony ohpm Packages.
Initialize QLogSDK
class LogDep implements LogDependency {
isDebug(): boolean {
return true; // Debug mode prints more logs
}
getFileLogLevel(): number {
return LogLevel.LEVEL_DEBUG; // Set log level
}
}
LogSdk.init(this.context.getApplicationContext(), new LogDep());
Log Messages
Logg.d('HelloWorldTAG', 'debug log...');
Logg.i('HelloWorldTAG', 'info log...');
Logg.w('HelloWorldTAG', 'warn log...');
Logg.e('HelloWorldTAG', 'error log...');
Retrieve Log Files
Logs are stored in the app's sandbox:
Logs are retained for up to 10 days and auto-cleaned on app startup. Use Log.uploadLogFiles()
to compress and retrieve logs. Decrypt the exported ZIP file with decod_mars.py
.
Publishing Process
-
Prepare Files in the Library Module (same level as
src
):-
README.md
: Describe the package and usage. -
CHANGELOG.md
: Version update history. -
LICENSE
: License file.
-
Recompile the Library to generate a
*.har
file.Generate SSH Keys:
ssh-keygen -m PEM -t RSA -b 4096 -f ~/.ssh_ohpm/mykey
- Private key:
mykey
(specify path). - Public key:
mykey.pub
(auto-generated in the same directory). -
Note:
ohpm
requires encrypted keys with a password.
-
Add Public Key to OpenHarmony Center:
- Visit OpenHarmony ohpm, click Profile, and paste
mykey.pub
under Public Key.
- Visit OpenHarmony ohpm, click Profile, and paste
-
Configure
.ohpmrc
:- Set the private key path:
ohpm config set key_path ~/.ssh_ohpm/mykey
-
Get the Publish Code from the website and configure it:
ohpm config set publish_id your_publish_id
- Publish the HAR File:
ohpm publish <HAR路径>
After approval, the package will appear in the repository:
Common Issues
1. oh-package.json5
Configuration
Example:
{
"name": "@qing/qlog",
"version": "1.0.0",
"description": "Persistent log utility for HarmonyOS Next.",
"main": "Index.ets",
"author": "qingkouwei",
"license": "MIT",
"homepage": "https://gitcode.com/wodekouwei/qlog",
"repository": "https://gitcode.com/wodekouwei/qlog",
"dependencies": {},
"compatibleSdkVersion": "12",
"compatibleSdkType": "HarmonyOS",
"obfuscated": false
}
-
Fields Displayed in Repository:
- Ensure
license
matches theLICENSE
file. - If using a namespace (e.g.,
@group/name
), register it in Organization Management:
2. Private Key Password Requirement
Always set a password when generating keys:
ssh-keygen -m PEM -t RSA -b 4096 -f ~/.ssh_ohpm/mykey
3. Publishing to Both Private and Central Repositories
To avoid overwriting .ohpmrc
, specify central repo settings during publish:
ohpm publish --publish_registry https://ohpm.openharmony.cn/ohpm --publish_id xxx --key_path ~/.ssh_ohpm/mykey qloglib/build/default/outputs/default/qloglib.har
For command details, see ohpm Publish Docs.
Summary
This guide outlines the steps to publish a HAR file to the OpenHarmony central repository using ohpm
, including key generation, configuration, and submission. Key takeaways:
- Security: Always set passwords for SSH keys.
-
Metadata: Ensure
oh-package.json5
andLICENSE
are accurate. - Review: Double-check parameters to avoid submission failures.
By following these practices, developers can efficiently manage and share SDKs within the OpenHarmony ecosystem.
Top comments (0)