Relying on the code, I finally stepped onto the peak of life, looking at the seemingly endless balance of Alipay, I can not help but start confused. Where is the road ahead?
In a trance, I woke up at my desk. It was a hot summer day, and the office fan wouldn’t blow the heat out of the air.
It was a dream
A background.
Recently the team worked on an SDK project to provide OCR recognition capability. As an ordinary code farmer, of course, is the leader said what to do what, so the algorithm docking puchi puchi finished id identification, phone number identification card identification function. Happy, delivered to the customer!!
Soon the leader said to build a fingertip recognition SDK for users to do fingertip search, and put it together with the original project to build a unified ability SDK. However, the output SDK requires that irrelevant code other than fingertip recognition be excluded. In order to catch up, I finished writing again, while packaging, directly pulled a new output branch to eliminate the old code. And that’s it.
Then there is a new SDK capability, the Image Quality SDK.
Here comes the troublesome question:
-
The output SDK needs to pull a new branch to cull each time, and merge each time update
-
Each SDK capability needs to be independent and can be combined
How to do ~~
2. Think about
This is not a problem, but it is a bit of a chore to maintain several separate branches of different capabilities.
But I’m lazy
I’m smart enough to think of two ways.
- Each capability is divided into a separate module, and the corresponding capability module is used when compiling
- Use gradle configuration to compile different source files, according to the configuration to print the SDK containing different source code
Finally, of course, we chose to compile the different SDK sources using gradle sourceSets ** configuration. ** Why not select modules? This is because the AAR package is provided, not the maven repository address. Therefore, in the case of multiple capabilities and multiple AAR packages, provide as few AAR packages as possible to facilitate users and themselves.
Three. Dry
-
Start by stripping the source code for the different SDKS and placing it in different folders. Common code is placed in the default Java folder.
This is a delicate task, avoiding interdependence, relying on inversion as much as possible, uncoupling, and having each SDK capability compile independently and compile together.
-
Add the sdkType configuration to gradle.properties
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##Copy the code
-
Configure the source file path in build.gradle
android { //... SourceSets {main {// Default folder def javaPaths = [" SRC /main/ Java "] def assetsPaths = [" SRC /main/assets"] def javaGraphCheck = "src/main/javaGraphcheck" def javaFingerDetect = "src/main/javaFingerdetect" def javaOcr = "src/main/javaOcr" def assetsGraphCheck = "src/main/assetsLens" def assetsOcr = "src/main/assetsOcr" def assetsFinger = "src/main/assetsFinger" if (sdkType == "graphcheck") { javaPaths.add(javaGraphCheck) assetsPaths.add(assetsGraphCheck) } else if (sdkType == "fingerdetect") { javaPaths.add(javaFingerDetect) assetsPaths.add(assetsFinger) } else if (sdkType == "ocr") { javaPaths.add(javaOcr) assetsPaths.add(assetsOcr) } else { javaPaths.add(javaGraphCheck) javaPaths.add(javaFingerDetect) javaPaths.add(javaOcr) assetsPaths.add(assetsGraphCheck) assetsPaths.add(assetsOcr) assetsPaths.add(assetsFinger) } java.srcDirs = javaPaths assets.srcDirs = assetsPaths } } }Copy the code
-
Change the name of the compiled artifact (to visually distinguish what is typed)
android {
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
android.libraryVariants.all { variant ->
variant.outputs.all {
outputFileName = "sail_ocr-${version}-${sdkType}.aar"
}
}
}
}
}
Copy the code
-
Other configuration
You can also use sdkType to determine the dependencies in gradle. The following is the demo module tree in my project.
if (sdkType == "graphcheck") { include ':lensdemo' include ':module_graphcheck' include ':module_ocr' } else if (sdkType == "fingerdetect") { include ':demo_finger_detect' include ':module_scan_finger' include ':module_ocr' include ':module_base' include ':module_ui' include ':lib_base' include ':lib_utils' } else if (sdkType == "ocr") { include ':demo_ocr' include ':module_ocr' include ':module_base' include ':module_ui' include ':lib_base' include ':lib_utils' } else { include ':app' include ':demo_ocr' include ':lensdemo' include ':module_graphcheck' include ':module_scan_finger' include ':module_profile' include ':module_ocr' include ':module_base' include ':module_ui' include ':profile_photo_frame' include ':lib_base' include ':lib_utils' }Copy the code
Now you just need to change the sdkType value in gradle. properties to compile the SDK package directly
End four.
This kind of work is not difficult, but there is a small sense of achievement after finishing.
Devote half a day of your day to stripping code, de-coupling, and debugging errors. Little jobs make time go by so fast.
Many things are not difficult, but no one has been doing them for so long.