Method for sharing resources between App and Flutter module
By packaging the IconFont resources into aar package and reading them directly in the native module, the problem of IconFont resource sharing between native App and Flutter module is solved, and resource sharing, performance optimization and development simplification is achieved.
Patent Information
- Application Number
- CN202411428401.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-14
- Publication Date
- 2025-03-07
AI Technical Summary
It is difficult to directly share and manage IconFont resources between native Apps and Flutter modules, resulting in duplication and inconsistency of resources, which increases development costs and complexity.
IconFont resources are introduced through the Flutter module, packaged into aar package, and the native module introduces aar package and directly reads the IconFont resources, avoiding complex format conversion and asynchronous reading.
It realizes resource sharing between the App and Flutter modules, reduces the package size, improves performance, and simplifies the development process.
Smart Images

Figure CN119576313A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of information technology, and in particular to a method for sharing resources between an App and a Flutter module. Background Art
[0002] In today's era of rapid development of digitalization and mobile applications, the combination of native apps and Flutter modules is becoming more and more widespread. However, in this process, the sharing of IconFont resources faces many challenges. With the continuous enrichment of application functions and the improvement of user experience requirements, IconFont (icon font) plays an important role in mobile application development as an efficient, flexible and visually appealing icon display method. In traditional native app development, IconFont resources are usually managed and used independently, which leads to resource duplication and inconsistency. After the introduction of the Flutter module, due to the differences in its own architecture and development mode from the native app, it is difficult to directly share and uniformly manage the IconFont resources between the two. At present, when dealing with the IconFont resources of native apps and Flutter modules, separate development and maintenance are often adopted. This method not only increases the development cost and workload, but also easily leads to inconsistent icon styles, affecting the overall consistency and aesthetics of the user interface. In addition, due to the different processing methods and format requirements of different platforms and technical frameworks for IconFont resources, complex format conversion and adaptation are required when sharing resources between native apps and Flutter modules, which further increases the difficulty and complexity of development.
[0003] At the same time, as a cross-platform mobile application framework, Flutter has been widely used in the field of mobile application development after a long period of development. Flutter technology can independently develop apps and package them for different platforms separately. There is no problem of multiple references to the same resources. This development mode is more suitable for some new projects, but for some old projects, if all are migrated to the Flutter framework, the cost is high and the risk is high. However, if we want to use Flutter's cross-platform features, we can use Flutter to develop modules that need to be developed using Flutter technology, and then package them into aar packages, and then introduce them into native projects. In this solution, native and Flutter are developed independently, so that common resources in the project will be introduced into native and Flutter respectively, resulting in resource duplication, increased package size and other problems. The existing technical solutions are: one is to put the IconFont resource into the native module, and then Flutter reads the IconFont resource stream in the native module through the channel technology. The channel technology solution is to read data asynchronously, and the performance is not very ideal. The other is the memory pointer sharing method, which passes the native data pointer address and length to Flutter. Flutter reads and processes data based on the memory pointer address and length, which requires the use of JNI technology, which increases the difficulty of development. Summary of the invention
[0004] The object of the present invention is to provide a method for sharing resources between App and Flutter modules to solve the problems of resource duplication, increased package size, unsatisfactory performance and increased development difficulty proposed in the above background technology.
[0005] To achieve the above object, the present invention provides the following technical solution: a method for sharing resources between an App and a Flutter module, the method for sharing resources comprising the following steps:
[0006] S1.Flutter module introduces IconFont resources;
[0007] S2. Flutter module packaging aar;
[0008] S3.App native module introduces aar package;
[0009] S4.App native module reads IconFont resources in aar package;
[0010] S5.App uses Icon in IconFont;
[0011] The IconFont resources in S1 are stored in the assets / fonts directory of the Flutter project.
[0012] Preferably, the IconFont resource in S1 is introduced and added to the pubspec.yaml configuration file of the Flutter module.
[0013] Preferably, the AAR package in S2 is automatically uploaded to the Maven repository, and the App native module in S3 automatically references the AAR package through Gradle.
[0014] Preferably, the IconFont resources introduced by Flutter in S3 are packaged into the assets directory of the App package.
[0015] Preferably, the App native module in S4 reads the IconFont resource through AssetManager.
[0016] Preferably, the native module of the App in S4 has built-in code to prohibit repeated reading of the same IconFont resource;
[0017] The built-in code in the native module of the App is as follows:
[0018] / / ttf
[0019] Typefacetf=Typeface.createFromAsset(getAssets(),"flutter_assets / assets / fonts / SFUDinMitAlt.ttf");
[0020] / / pngAssetManagerassetManager=getAssets();
[0021] try{
[0022] InputStreamis=assetManager.open("flutter_assets / assets / images / icon_no_data.png");
[0023] Bitmapbitmap=BitmapFactory.decodeStream(is);
[0024] }catch(IOExceptione){
[0025] thrownewRuntimeException(e);
[0026] }
[0027] Compared with the prior art, the beneficial effects of the present invention are: the method for sharing resources between the App and the Flutter module:
[0028] The App native module directly reads the path of the IconFont resource compiled in the Flutter module in the apk to achieve the purpose of resource sharing. There is no need to asynchronously obtain the resource stream through the Flutter channel in real time. It is more convenient and has better performance. Combined with the Flutter module packaging and introduction method, the overall size of the package is reduced and the poor performance caused by repeated reading is avoided. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] Figure 1 It is a schematic diagram of the resource sharing process of the present invention;
[0030] Figure 2 It is a schematic diagram of the location of the IconFont resource file of the present invention. DETAILED DESCRIPTION
[0031] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0032] See also Figure 1-Figure 2 The present invention provides a technical solution: a method for sharing resources between an App and a Flutter module, the method for sharing resources comprising the following steps:
[0033] Step 1. The Flutter module introduces IconFont resources:
[0034] Put the IconFont resource in the assets / fonts directory of the Flutter project, and then add the resource to the pubspec.yaml configuration file of the Flutter module. The code is as follows:
[0035] fonts:
[0036] -family:IconFont
[0037] fonts:
[0038] -asset:assets / fonts / iconfont.ttf;
[0039] Step 2. Flutter module packaging aar:
[0040] The IconFont resources will also be packaged into the aar package, and the packaged aar will be automatically uploaded to the warehouse;
[0041] Flutter packaging aar means packaging the Flutter project into an AndroidArchive (AAR) file. The AAR file is an Android library file that contains compiled code, resource files, and manifest files. It is used to integrate native Android code and libraries in Android applications. By packaging the Flutter project into an AAR file, the Flutter application can be easily integrated into the existing Android or iOS project. This packaging method allows developers to embed Flutter views into native applications while retaining the hot reload and debugging functions of the Flutter application, thereby achieving seamless integration of native applications and Flutter views. Specifically, starting from Flutter version 1.9.1, Flutter supports direct packaging into AAR files. The command is flutter build aar, by executing this command, you can package the Flutter project into an AAR file, which contains all the contents of the Flutter application, including necessary components such as libflutter.so and flutter_embedding.jar. In addition, you can add third-party plug-ins and libraries to the AAR file to expand the functions of the Flutter application. After packaging the Flutter project into an AAR file, you can directly reference this AAR file in the Android project to integrate the Flutter view with the native Android application;
[0042] Step 3. Import the aar package into the App native module:
[0043] App native modules automatically reference AAR packages;
[0044] Step 4. The App native module reads the IconFont resource in the aar package:
[0045] like Figure 2 As shown in the figure, after the Flutter module is integrated into the native project, the IconFont resources it introduces will be packaged into the assets directory of the apk. The IconFont resources can be read through the AssetManager. The code is as follows:
[0046] Typeface tf=Typeface.createFromAsset(mParent.getAssets(),"flutter_assets / assets / fonts / iconfont.ttf");
[0047] tv_if.setTypeface(tf);
[0048] tv_if.setText("\ue639");
[0049] Step 5. App uses Icon in IconFont.
[0050] During use, when the IconFont resource changes, you need to repackage and execute steps 1 to 3.
[0051] Although embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions and variations may be made to the embodiments without departing from the principles and spirit of the present invention, and that the scope of the present invention is defined by the appended claims and their equivalents.
Claims
1. A method for sharing resources between App and Flutter modules, characterized in that: The resource sharing method comprises the following steps: S1.Flutter module introduces IconFont resources; S2. Flutter module packaging aar; S3.App native module introduces aar package; S4.App native module reads IconFont resources in aar package; S5.App uses Icon in IconFont; The IconFont resources in S1 are stored in the assets / fonts directory of the Flutter project.
2. According to claim 1, a method for sharing resources between an App and a Flutter module is characterized in that: After the IconFont resource in S1 is introduced, it is added to the pubspec.yaml configuration file of the Flutter module.
3. According to claim 1, a method for sharing resources between an App and a Flutter module is characterized in that: The AAR package in S2 is automatically uploaded to the Maven repository, and the App native module in S3 automatically references the AAR package through Gradle.
4. According to claim 1, a method for sharing resources between an App and a Flutter module is characterized in that: The IconFont resources introduced by Flutter in S3 are packaged into the assets directory of the App package.
5. According to claim 1, a method for sharing resources between an App and a Flutter module is characterized in that: The native module of App in S4 reads IconFont resources through AssetManager.
6. According to claim 1, a method for sharing resources between an App and a Flutter module is characterized in that: The S4 App native module has built-in code to prohibit repeated reading of the same IconFont resource.