In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to develop Android SDK". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. What is SDK?
I believe that friends who develop Android must have used third-party SDK, such as pushing SDK, sharing SDK and so on. The full name of SDK is Software Development Kit, which is translated as "Software Development Kit". SDK is usually a specific software package, framework collection, etc., written to assist in the development of some kind of software.
SDK can be divided into system SDK and application SDK. The so-called system SDK is a collection of tools developed for the use of specific software frameworks, hardware platforms, etc. Application SDK is a set of tools developed based on system SDK, which is independent of specific business and has specific functions.
The users of SDK are mainly B-end customers, and the final delivery products are codes, examples and documents. Customers' access to SDK is also a process of communicating with SDK providers. The cost of external communication is higher than that of internal communication, and more problems will be encountered. Therefore, SDK development requires more for developers than for application development. If you can develop SDK well, you can develop applications well, but if you can develop applications well, you may not be able to develop SDK.
2. SDK to achieve the goal
In a nutshell, the goal of SDK is simplicity, stability and efficiency.
Brevity
For users, a good product should be simple and easy to use and should not take them too long to learn. SDK should be the same, it should not appear complex and tedious docking work, users can do SDK docking in very little time by reading code and documentation.
For example, when developers need to use SDK services, they only need to add a new line to the code. Initializing SDK in a project requires only one line of code, so developers don't have to care about GLContext, and they don't have to worry about synchronization or asynchronism.
Public class FURenderer {/ / define public static void setup (Context context) {/ /...}} / / one line of code calls FURenderer.setup (context)
Stable.
From the point of view of SDK users, we expect third-party SDK services to be stable and efficient, which is reflected in providing stable and reliable services and efficient runtime performance. This requires us to try our best to do the following when designing and implementing SDK:
Provide stable API to the outside. Once the API of the SDK is determined, the cost of changing the API by the provider is very high unless special circumstances cannot be changed.
Provide stable business to the outside world. After providing a stable API, there must be a stable business as a support.
Stability at run time. Ensure the stable operation of the SDK itself, so that the host application is not unstable due to the access to the SDK.
The version is stable and updated. The iteration of the SDK version is very slow, and the iterative process should be shielded from users as much as possible to avoid unnecessary adaptation costs.
High efficiency
Performance issues should be taken into account in both ordinary application development and SDK development, and SDK designers should focus on the following issues:
Less memory footprint. Generally speaking, SDK and App run in the same process, so SDK should manage the memory occupied by itself, allocate it reasonably and pay attention to release it.
Less memory jitter. SDK designers must reduce the memory jitter caused by frequent GC while consuming less memory.
Less power consumption. It is difficult to make a tradeoff between low power consumption and high performance, which can be considered from the point of view of CPU computation, screen refresh frame rate and so on.
3. SDK architecture design
The architecture implementation of SDK determines the difficulty of subsequent maintenance, so it is best to combine the actual business to determine the appropriate solution. Take the modular development in the project as an example to talk about the principles of architecture design.
Following several principles of object-oriented development, the aim is to achieve three goals: maintainability, reusability and extensibility. Specifically:
According to the principle of single responsibility, the system is divided into several small modules, each module remains relatively independent, reducing the complexity of the implementation class.
According to the principle of interface isolation, define the contract interface for each module, the granularity of the interface should be small, the function should be fine, and the smaller the interface, the easier to maintain.
Modules communicate through protocols or interfaces to avoid direct interdependence, so as to reduce coupling and know each other least, which embodies the Demeter rule.
According to the opening and closing principle, the common behavior of each module is defined, and the skeleton realization is provided by the template design pattern, which is easy to expand the function.
According to the principle that combination is better than inheritance, when multiple modules are superimposed, the combination of classes is used to ensure the flexibility of the design.
For example, the functional module of the third party demo of the project draws lessons from the architecture of the Java collection framework, which is divided into three parts: contract interface, abstract class and concrete implementation.
First of all, IEffectModule is defined as the contract interface for special effects, including the common operations of various functional modules, such as creation, setting parameters, destruction, and so on.
As the skeleton implementation of IEffectModule, AbstractEffectModule implements common methods and defines common member variables.
Define the beauty IFaceBeautyModule interface, which inherits the IEffectModule interface, including additional parameter operations, FaceBeautyModule as its implementation class, and inherits AbstractEffectModule to reuse the code of the base class.
The makeup and beauty module is similar, first define the contract interface, and then define the specific implementation, the interfaces are isolated from each other, and the interfaces are highly cohesive.
FURenderer implements IFURenderer rendering interface and IModuleManager module management interface, combining various functional modules.
4. SDK design specification
API design is very important in any development. In many cases, the quality of software is reflected in the design of API. In ordinary application development, API will only be circulated among developers and will not be exposed to others who are not developers of this application. However, as a service, SDK needs to expose part of the API to developers in order to use SDK services.
Here are some principles that should be paid attention to.
The method name indicates its purpose
The name of a good method is the most intuitive indication of its function, which is self-explanatory and does not require additional documentation, which reduces unnecessary communication costs. What could be more intuitive for developers than reading code directly? refactoring says that it's not too much to name each variable as if you were your child.
Validity test of parameters
If an exception occurs while the program is running, it will destroy the user's experience and have a very bad impact. We adopt the idea of "defensive programming" to avoid the destruction of illegal input to the system.
When the validity check fails, it corresponds to different processing strategies according to different method permissions:
Explicitly check for the public method to throw an exception and use @ throw to explain why the exception was thrown
For private methods, the validity of parameters is checked by assertion.
Check the validity of the parameters of the constructor so that the object is in a unified state.
It should be noted that if the cost of inspection is too high, it needs to be taken into account.
Method only implements a single function
A method should have a single function and do less and more specialized things as much as possible, which is also the embodiment of the principle of single responsibility. Alibaba Code Specification stipulates that a method had better not exceed 80 lines, and large methods should be split into smaller ones.
In addition, note that it is better to provide a small and beautiful method than a large and comprehensive one, which tends to change frequently and is more likely to produce risks. Therefore, it is better to provide smaller methods for combination, and small and beautiful methods are easier to achieve code reuse.
Access control
Including the permissions of class methods and variables, private ones can be declared not to be made public, and the less the outside knows, the better. Can declare static methods with static, static methods naturally thread-safe, reflect the inheritance relationship with protected modification, to ensure that the exposed methods and variables are safe and reliable.
Avoid overly long parameters
Too long parameters can cause difficulties in memory, and calling parameters are prone to errors, which should be avoided as much as possible. When it is impossible to avoid too long parameters, consider other methods to solve the problem:
By using the Builder pattern
By encapsulating multiple parameters into class objects
For example, there is a method in the project with a lot of parameters.
Int onDrawFrameSingleInput (byte [] img, int w, int h, int format, byte [] readBackImg, int readBackW, int readBackH)
After refactoring, the parameters are encapsulated into objects, and the calling method is passed in by constructing only one object, so as to avoid a bad sense of experience caused by a large number of parameters.
Public class VideoFrame {private int width; private int height; private byte [] data; private byte [] readback; private int readbackWidth; private int readbackHeight; private int pixelFormat; /...} int onDrawFrameSingleInput (VideoFrame videoFrame)
Be careful with method overloading
Abusing overloading can easily confuse developers, and when overloading methods are needed, you can use different method names instead. For constructors, overloading can be replaced by static factories.
The ObjectOutputStream class provided in Java is a good example: its write has a variant for each basic type, such as writing out characters, writing out boolean, and so on. Instead of using overloading to design it as write (Long l) and write (Boolean b), the designer designed it as writeLong (l) and writeBoolean (b).
For example, the external processing methods of the project are all overloaded and can only be distinguished according to the parameters, which is very confusing. After changing it to a different method name, you will know the method to be called when you see the name, which is a lot clearer.
/ / int onDrawFrame before reconstruction (byte [] img, int tex, int w, int h); int onDrawFrame (byte [] img, int w, int h); / / int onDrawFrameDualInput after reconstruction (byte [] img, int tex, int w, int h); int onDrawFrameSingleInput (byte [] img, int w, int h, int format)
Avoid methods returning null directly
Do not return null for methods that need to return an array or collection. For example, if we go to a pastry shop to buy bread, if it is a normal state when the bread is gone, we should not return null, but an array or collection of length 0. Java provides Collections.emptyXXX () to represent an empty collection.
Avoid introducing third-party libraries
GitHub has many open source third-party libraries, such as network request OkHttp, picture loading Glide, etc., but in SDK development, the basic principles followed are:
The principle of minimum availability is to use the least amount of code and do not add entities if it is not necessary.
The principle of minimum dependence is to use minimum external dependence and do not increase dependence if it is not necessary.
The introduction of third-party libraries may cause the following problems:
The third-party library of the host application is inconsistent with the version that SDK depends on, which is easy to cause conflicts and increase the cost of docking.
The open source database is constantly updated, and the SDK should be updated in a timely manner to increase the additional maintenance workload.
Due to the introduction of open source library, the problem is difficult to troubleshoot.
Ensure compatibility
SDK is iterative, with new features and bug fixes with each release. For users, the upgrade version should not change too much, generally directly replace the library file or change the version number of the remote dependent library is enough. Avoid renaming the public interface directly. If the old interface is obsolete, it should be marked with the @ Deprecated keyword, and the alternative and the time of abandonment should be given.
Reduce invasiveness
To ensure that fewer code intrusions are mainly used to provide services, fully consider the developer's usage scenarios to design a good API. A set of good API should be defined in the way expected by most developers-easy to understand semantically and simple and reliable to use. The specific performance is that it can run stably and reliably under normal circumstances, and feedback error information in a timely manner under abnormal circumstances.
For example, using Gradle to download dependent libraries, there are unnecessary bundle resources in the AAR package. We provide the construction configuration when packaging apk, and we are free to choose the bundle to be packaged, thus reducing the intrusiveness to the host application.
ApplicationVariants.all {variant-> variant.mergeAssetsProvider.configure {doLast {delete (fileTree (dir: outputDir, / / delete unnecessary bundle files includes: ['model/ai_face_processor_lite.bundle',' model/ai_gesture.bundle' 'graphics/controller.bundle',' graphics/tongue.bundle'])} 5. SDK delivery
Encapsulated package
Android platforms usually use jar and aar to publish SDK, the difference is that jar contains only code, aar can contain code, resources, and dynamic libraries. Generally speaking, aar is the most appropriate delivery method, and users can integrate it with one line of code by uploading it to the maven server. For customers who need flexible customization, we will also provide the source code of SDK. The disadvantage is that it is difficult to upgrade and a lot of code needs to be changed.
For code confusion, don't confuse the public interface with the interface used by native, and the internal implementation details can be confused to reduce the size of the SDK package.
Access document
Access documents are used to tell SDK users how to use SDK, step-by-step use, and possible problems. The contents of the document include: update record, basic information, API description, integration steps, FAQ and so on. The standard of a good document is to be clear and easy to understand. A developer who doesn't know anything about SDK can dock by looking at the documentation, and the frequently encountered problems should be listed one by one, and professional terms should be explained accordingly.
Demo example
An integrated Demo is usually a simple App that shows how to quickly connect to SDK. The source code of Demo is hosted to GitHub for easy reference, and its version change strategy is consistent with the change of SDK version. Although it is a Demo, its development principles should be consistent with SDK to ensure high-quality delivery.
This is the end of "how to develop Android SDK". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.