Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

At the beginning

Uniapp is a cross-end hybrid development framework. It is known that developers write a set of code and run on multiple platforms. Roughly, it can run on the following platforms:

Among the 11 platforms in the figure above, APP platform is included. This chapter shares how to package the Uniapp project into an Android Apk package and shows you how to use the SDK plug-in.

The preparation of the instruments

Uniapp’s local APP packaging requires some complicated preparations:

  1. Android Studio
  2. HBuilderX
  3. App offline SDK
  4. To apply forUniappThe offline packageAppkey.

Latest SDK directory description:

  • Hbuilder-hellouniapp: Package the demo App offline.
  • Hbuilder-integrate-as: A minimalist example of integrating uni-app.
  • SDK: SDK library file directory.
  • Feature-android. XLS: detailed configuration of each extended Feature API of the Android platform.
  • Readme: version description file and precautions.
  • Uniplugin-hello-as: Example of uni native plug-in development.

The configuration of engineering

Once you have installed Android Studio, you need to create a new project. If you want to start with a blank project, you can refer to the official tutorial. Here, import the officially created project and select hBuilder-Integrate-As in the Android offline SDK compression package.

  1. The importUniappofflineSDKbagHBuilder-Integrate-ASFolder,simpleDemoIt’s integratedUniappSimple Android apps.

  1. Install the Android SDK, create the corresponding AVD to facilitate debugging, before starting can firstsimpleDemoRun it.

  1. Open Uniapp project with HBuilder, follow the following steps: release -> native App local package -> generate local package resources, you need to create application in Dcloud platform first and log in HBuilder.

    If the Uniapp project is created using CLI, you can directly run build command in the root directory of the project. In fact, the application name and appID created in Dclud will be manually filled into the project mainfest.json. Under dist/ Build.

    Then copy and overwrite the generated folder into the Android project’s SRC /main/assets/apps/ directory.

It should be noted that the appID is unique in each application of Dcloud Developer Center. When developing mobile APP using HBuilder, the account of the first created application will be set as the owner of the corresponding AppID. In actual projects, developer management should be done well, for example, small program account.

  1. Replace simpleDemo’s AppID with the packaged appID described above.

  1. In the Dcloud developer Center, generate an offline package key. Note that the package name is the same as configured in the project when the key is generated.

  1. Put the certificate used to generate keys in the Dcloud platform in the simpleDemo directory, and configure package names, certificates, and other information in the Android project.

  1. Modify the app startup icon, startup page, apK name, etc. The directory is in simpleDemo/ SRC /main/res/. From here, android offline packaging project configuration is complete.

The test and operation

Build->Build Bundle(s)/APK(s)->Build APK(s) In the process of development, hot update real-time view is needed to rely on Hbuilder to run, tune up the AVD of Android Studio, or through the USB interface real machine debugging.

The use of plug-in

Obviously, this kind of mixed development mode, just separates the business code from the Android project, some call system native ability, if the Android SDK of UniApp does not provide, will write local plug-in and reference. See the documentation for how to develop Android plug-ins.

Once the plug-in is developed, it is relatively easy to use. First, create the NativePlugins folder in the uniApp project root directory, and then copy the plug-in file into it. The call method is const myPlugin = uni.requirenativePlugin (‘ Plug-in registered name ‘).

Then copy the aar file into the simpleDemo/libs directory of your Android project, and create a dcloud_uniplugins.json file in SRC /main/ Assets to augment the configuration of the plug-in.

The plugin is already in use in Android Studio. It should be noted that hot updates for Hbuilder development and debugging become relatively difficult when using the plugin. You need to create a custom base in Hbuilder that contains the plugin and install it in the emulator or real machine first.








At this point, this article is finished, sa Hua Sa hua.

I hope this article has been helpful to you. If you have any questions, I am looking forward to your comments. Same old, likes + comments = you know it, favorites = you know it.