5+APP Android native plug-in development process

1. Offline packaging

Hbuilder official path: https://nativesupport.dcloud.net.cn/AppDocs/usesdk/android

Why use offline packaging? Because it is to develop Android third-party plug-ins, mainly using Android Stuido, so the 5+APP project is imported to Android studio to facilitate development and testing.

In fact, there is another reason. The third-party plug-ins involved use system-level permissions, and they must be signed and packaged with platform.pk8 before they can run. So it is convenient to use offline packaging.

Hbuilder(X) does not need pk8 signatures. Of course, you can convert pk8 into a keystore file so that you can sign.

Here are the big guys introducing conversion, but I haven't tried it yet. ( Https://blog.csdn.net/langlitaojin/article/details/108045709 )

2. Copy the jar package and so file of the third-party plug-in to the corresponding directory (app-libs) of Android studio, and then configure it in the outermost layer of gradle:

repositories {
    flatDir {
        dirs 'libs'
    }
}

Add in the dependencies layer:

implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar', '*.so'], exclude: [])

Note: If there is a so file, you need to configure ndk support in the android layer of gradle.

ndk {
     // 设置支持的SO库架构
     abiFilters 'armeabi' ,'x86', 'armeabi-v7a'
}

3. Create java classes to encapsulate the functions used by third-party plug-ins, and encapsulate them into independent methods. Data is passed to the js side: broadcast, or use interface class callback.

There is one thing to note about this. If the broadcast listener is registered and not logged out, after the application exits and reopens, it will report:

IllegalArgumentException: Receiver not registered。

I used the 5+ method to cancel the broadcast registered in java, but it didn't work and it was puzzled.

Then I encapsulated a method to log off the listener in the java layer, and then called it in the js layer. The problem did not reappear.

So, I think that the native method should be written in the java layer as much as possible, and called by the js layer, avoid writing with 5+.

Here, I think it’s better to use broadcast less. Logging out is a troublesome thing. In Java, you can log out in a special life cycle, but in js, it’s troublesome. It requires various events (the return key on the page or close Key, virtual or physical return on the mobile phone) to monitor for logout processing.

 

4. Create a js class and use 5+API to encapsulate the method of the java class encapsulated above.

Use java to encapsulate the third-party library, and then encapsulate a corresponding api in js, which is convenient for front-end developers to use and maintain.

-------------------------------------------------- -------------------------------------------------- ------------
Note: If it is a system-level application, you need to hook webview (because webview has security loopholes, starting from Android 5.1, Google prohibits system applications from using webview).

提示:Caused by: java.lang.UnsupportedOperationException: For security reasons, WebView is not allowed in privileged processes

 

Solution:

1. First, create an Application inherited from DCloudApplication, and then reference your own Application in AndroidManifest.xml, and add tools:replace="android:name" to avoid conflicts with DCloudApplication in Hbuilder's lib

2. Before the super method of the application's onCreate method, call the method of hook webview to bypass Google's prohibition.

Specific hook method: https://www.52pojie.cn/thread-992014-1-1.html

 

 

Guess you like

Origin blog.csdn.net/XIADANXIN/article/details/112982780