Android
Environment to prepare
L JAVA environment JDK1.8
L Android Studio download address: Android Studio official website OR Android Studio Chinese community
L App offline SDK Download: Download the android SDK version 2.9.8+
Import the uni plug-in native project
L Uniplugin-hello-AS project please find it in App offline SDK
L Click the Android Studio menu option File– >New– >Import ProjectApp offline under the SDK
Plug-in development
Uni-app plug-ins currently support only Module extensions and Component extensions. Adapter extensions are not currently supported. Module extensions (non-UI extensions) are described below.
- Create a New Module, File->New->New Module, select Android Library, fill in the information, and click Finish
- Edit the build.gradle file of the new Module and add dependencies to make the Module capable of developing uniApp plug-ins
CompileOnly fileTree(dir: 'libs', include: ['*.jar'])\ compileOnly fileTree(dir: '.. /app/libs', include: [' uniapp - v8 - the aar ']) \ compileOnly "com. Android. Support: recyclerview - v7:28.0.0" \ compileOnly "Com. Android. Support: support - v4:28.0.0" \ compileOnly "com. Android. Support: appcompat - v7:28.0.0" \ compileOnly 'com. Alibaba: fastjson: 1.1.46. Android' \ implementation 'com. Facebook. Fresco ": fresco" : 1.13.0'Copy the code
- Write logic, create a new Java class in your Module to write your own code logic
- Code implementation
-
- We are implementing a Module that needs to inherit from UNI
- The extension method must be annotated with @unijsMethod (uiThread = false or True) to determine whether it needs to be run on the UI thread. Default is true. The extension method must be public and easy to call from within UNI
-
- The first argument is passed in as a JSON parameter, which gets the desired argument by key, and the second is a callback function that returns the desired result to the foreground page
- If you need other permissions, you need to apply for them in the Androidmanifest.xml file, such as reading and writing calendar
- Edit assets/dcloud_uniplugins.json file in the root directory of “app” Module under uniplugin-hello-as project and register it under nativePlugins node. Type: Module, Name specifies the component name to be exposed, class specifies the path to the method, and package name + class name
Dcloud_uniplugins. Json description:
HooksClass: lifecycle proxy (implement AppHookProxy interface class) format (complete package name plus class name) Type: Module or Component type name: Register name Class: Full name of the module or Component entity classCopy the code
- Build. Gradle for your app. The name of your component is the name of your new module
This far, and the code development is over, the next is to use the android play offline package verification of studio, portal nativesupport.dcloud.net.cn/AppDocs/use…
Packaging validation
- Hbuildx creates a new project and makes the plug-in call through uni.requirenativePlugin
- At present, uniApp packaging materials need to be prepared, which will not be described here. AppKey, APPID and Android package name need to be configured in Android Studio, as well as Android certificate alias and secret key
- Go to Publish -> Locally packaged -> Generate locally packaged App resources
- Copy and paste the generated file into uniplugin-hello-as \app\ SRC \main\assets\apps and change the appID of the dcloud_control. XML file
- Android package parameters configuration, package name, certificate alias, etc. (Figure 1), the certificate file needs to be placed in the app (Figure 2) androidmanifest.xml configuration package name and AppKey, is the AppKey of the second step above
- Connect the phone, open USB debugging, and click Run
Uniapp packaging
Plug-in generated
Compile aar file to generate Module: Select Gradle > plug-in Module >Tasks > Other >assembleRelease
HBuildX introduces the plug-in
- Create nativePlugins -> plugin name -> Android -> plugin AAR file. Store package.json under Android.
Tips: The plug-in ID must be the same as the folder name
- Open manifest.json and configure the native plug-in
- Cloud packaging for plug-in function verification