ImgCompressPlugin
Gradle plugin for automatic image compression on Android, one click to scan the resource directory in the project and perform batch image compression, providing three compression modes, support lossy and lossless compression. At the same time, compressed files will be recorded, suitable for individual and team use.
Making address: github.com/duking666/I…
Resolved scenarios
- The application of apK volume reduction is required
- Solve the complicated process of manual compression one by one,
One click
Can solve - Uncompressed pictures were accidentally introduced into the project but not found. How to break? ImgCompressPlugin helps you out!
- UI children’s shoes give the size of the design drawing
feeling
It looks like it’s compressed, but it probably isn’t, so should I manually compress it or sneak off and put it in the project? ImgCompressPlugin helps you out! - How to avoid distortion caused by repeated compression during team version iteration, and how to ensure that the image has been compressed by other children? ImgCompressPlugin helps you out!
How to use
Step 1: In the build.gradle file of the Project:
buildscript {
repositories {
jcenter()
}
dependencies {
...
classpath 'com. Kingkingdu. Plugin: img - compressor: 1.1.5'}}... . // Configure the image compression plugin.'img-compressor'
imgCompressOpt{
way="pngquant"
test = false
whiteFiles=["text_pic1.png"."test_pic2.jpg"]
minSize=5
tinyKeys=["your key"]}Copy the code
Step 2: After importing, click sync Now. After gradle is configured, the image below shows imgCompressTask. Double-click and wait for the compression result
Configuration information
way
: Set the compression mode. Three common compression modes are supported: “Tinypng “,” PngQuant “, and” Zopflip “. Select 1 of the threetest
: set up the test model is open, false says compressed images directly to cover artwork, saying it was the true original and a compressed output to the test directory (Project/ImageCompressTest)whiteFiles
: Optional, whitelist file array, not compressedminSize
: Unit: KB. Compression is triggered only when the original image is larger than a certain value. 0 indicates that all images are compressedtinyKeys
: optional, only required if way=”tinypng”,TinyKeys application tutorial entry
Effect of compression
The original image | tinypng | pngquant | zopflip |
---|---|---|---|
The compression type | lossy | lossy | nondestructive |
1.3 M | 445KB | 542KB | 903KB |
How to choose the appropriate compression mode
- If the project itself originally used one of the three compression modes, way selects the same mode.
- If you don’t have a fixed compression method,tiny or PngQuant is recommended. Tiny takes a long time to compress, so you need to apply for a key on tinyKey’s website
Meaning of whitelist
- If a small amount of image distortion is found after image compression, it can be added to the whitelist to avoid compression
Test mode
- Suitable scenario :UI designers may need help to compare the compressed image for distortion, but they don’t have the code permission for the project, so put the compressed image in a directory to package the whole and send it to the designer for review. Of course, the most convenient way is Android Studio’s version comparison tool.
- When test=true in the configuration, the original image and the compressed image are printed to the test directory
(Project/ImageCompressTest)
, the compressed image is named asxxxx(test).png
- Note: If only the original image exists but no compressed image exists in the test directory, the image is fully compressed and no further compression is required
Best practices
-
ImgCompressTask test=false, imgCompressTask test=false
- Task Finish, compressed:3 files Skip :3 files before total size: 951.50KB after total size: 309.67KB save size: 641.83KB
- According to the log, 3 images are compressed, 3 images are skipped, and the size comparison of images before and after compression is obtained. The reason for skipping the image is that the image is fully compressed and does not need to be compressed again
-
After the task is executed, an image-compressed-info.json file is generated in the project directory, which records the compressed image information
-
Use Android Studio’s built-in version control to compare before and after images for distortion, whitelist distorted images (which are rare), and revert the original image
-
Add the image-compressed-info.json file to version control, and submit the modified image and JSON file
-
In the subsequent version development, anyone in the team can add any images during the development process, or after the version is tested, execute a task to compress the images introduced by the new version. The images of the old version remain unchanged, and there will not be repeated compression by team members
-
It is recommended that one child on the team deal exclusively with image compression, not that everyone knows how to use it. Json file conflicts are also avoided.
Problem solving
- Q1: If the compressed image is replaced with a new one in the next iteration, will you forget to compress it?
- A: no, the plugin itself will be checked by MD5, not the image name, so the new image will also be compressed
- Q2: What image formats are supported?
- A: PNG and JPG formats are supported, but webP does not support them
- Q3: For componentized projects with multiple Modules, will all modules be scanned?
- A: All applications or Android Library types will be scanned
The last
This plug-in has been used in the author’s team line, the effect is good. Finally, attached is the open source address of the project :github.com/duking666/I… Welcome star and fork to contribute code or issue to the project, thank you