preface
Recently, due to the need of work, some functions in the application have been isolated and packaged into SDK for partners to use. Due to lack of experience, there is no much online to write this aspect of the content of the article, encountered a lot of pits, decided to record.
SDK
In fact, just said to write SDK also a little panic, THE impression of SDK has always been a complex thing, the mind is Java SDK,Android SDK such a behemoth. SDK stands for Software Development Kit. In a broad sense, SDK is a collection of Development tools used to build applications for specific Software packages, Software frameworks, hardware platforms, operating systems, etc. In the narrow sense, SDK is a collection of new and independent tools that can complete specific functions and return relevant data based on the development package of system SDK. It may sound strange, but in fact, some business logic is isolated, packaged into JAR, SO and AAR, and some APIs are exposed to external calls, which can also be called SDK. Such as push SDK, payment SDK, map SDK… Since it is a so-called set, then naturally there are big and small. Here are some of the things I encountered when encapsulating the SDK and using it. I hope it will help you.
Package the AAR once and twice
Most of the time, our business depends on the third party library, the carrier may be JAR, SO, AAR, if it is jar package, it is easy to handle, directly into the liBS directory; The so library is not easy. Many developers only provide a few so libraries for the ABI, and then you have to choose between all of them or just one directory, otherwise you will get an error. An AAR is a collection of code, resources (images, layouts, etc.), so libraries, and so on. Then the need arises, how to repackage the third-party AAR package, add your own business logic and then package it into an AAR? In fact, I have not found a direct solution to this problem, if you have, please contact me, thank you very much! However, this problem can be solved in a curve. An AAR is just a compressed package, as shown below:
Aar decompress package. PNG
Android Studio imports an AAR as a module, using classes and other resource files. Can we just extract it and import it as a Module? This avoids wrapping an AAR on top of an AAR! Perfect jump over the hole! And it turns out it can. If the AAR you want to encapsulate contains only jar packages and so libraries, then it is even easier to unpack them and import them into the project-related directories separately! But expect Google to come up with a foolproof one-click merger.
Initialization of Application objects in SDK
There was a time when the SDK access party reported that there was a crash somewhere. When you looked at the Log, it was a null pointer to the method in the Application object. I think, confused? It isn’t. Wrong lead-in position? It isn’t. Didn’t read the document carefully? Also not? Demo works well…… Then it occurred to the access party to customize the Application object. If an AAR defines an Application, theme, or icon, then the following exception should be thrown at compile time:
Compilation error AndroidManifest merge exception
Since both the project and the AAR define their own Application, the compiler does not know which one to use. This causes the exception shown above.
tools:replace="android:name"
Copy the code
Merger can refer to the official document: merge multiple listing file | Android Studio – Android Developers still, but you are a big probability drop pit, the solution directly from the SDK Application object to replace for you, In other words, the Application object in the SDK is not initialized at all, which is the culprit of the null pointer. Indeed, I did not consider the situation of custom Application when writing the document before, which is still lack of experience! At one time, I even removed all the initialization methods from the Application. Later, I thought it was OK to let the access party inherit my Application. Test a wave, quickly update the next document, or be mocked again… Understand why some third-party SDKS require Application objects defined in the SDK to be inherited.
Application multiple inheritance
As an SDK provider, if your SDK requires users to inherit your Application, I think we should provide a solution of Application multiple inheritance. After all, users may not only use you for one SDK, and it will be awkward when multiple SDKS have this requirement. Java multiple Inheritance, where the requirement is to inherit two classes, rather than “inherit” method declarations on multiple interfaces by implementing them. Language native is not supported, so it is more troublesome. The solution is as follows: create a proxy Application object through the interface implementation + reflection method, the curve implementation of Application multiple inheritance, because of the code is more, here is not paste source code, solution: ApplicationProxyDemo source code with annotation food, flavor better!
Four, two ways to import AAR
Solution 1: Use Android Studio to create a module method, select JAR/AAR.
Import. AAR
Gradle script:
- Add the following code to the Android {} tag in the build.gradle file to declare the local repository:
repositories {
flatDir {
dirs 'libs'
}
}
}
Copy the code
- Add the AAR package to the libs directory (create one in the app directory if you don’t have one) and add it to Dependencies:
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) ... Compile (name: 'xxxxxx-xxxx', ext: 'aar')Copy the code
5. Confusion in SDK that can’t be ignored
- General avoid confusion, such as some third-party class library requirements
- Avoid confusing classes that provide external methods and data entities, and add them to the access party’s ProGuard-rules.pro.
The last
In fact, IN the recent SDK development and docking encountered a lot of problems, but also learned a lot of things, due to the lack of relevant experience, but also in the continuous exploration. Is the so-called actual combat out of real knowledge, easy to talk on paper, real weapon will expose some problems, or to start more. Of course, technology is business driven.