├── src └── main │ ├── resources │ └── META-INF │ │ └── gradle-plugins │ │ └── webpConvert.properties │ └── groovy │ └── webp │ └── plugin │ └── WebpConvertBuildPlugin.groovy ├── .gitignore ├── README.zh-cn.md ├── bintray.gradle └── README.md /src/main/resources/META-INF/gradle-plugins/webpConvert.properties: -------------------------------------------------------------------------------- 1 | implementation-class=webp.plugin.WebpConvertBuildPlugin 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # OSX 2 | .DS_Store 3 | 4 | # library 5 | mgim/ 6 | mglibs/ 7 | mgim-android/ 8 | library/ 9 | 10 | # android generated 11 | bin/ 12 | gen/ 13 | 14 | # eclipse 15 | .classpath 16 | .settings 17 | .project 18 | 19 | local.properties 20 | project.properties 21 | 22 | # IDEA 23 | .idea/ 24 | *.iml 25 | out/ 26 | 27 | # gradle 28 | build/ 29 | .gradle/ 30 | gradle/ 31 | gradlew 32 | gradlew.bat 33 | gradle.properties 34 | 35 | # build 36 | lint.xml 37 | lint.html 38 | proguard-rules.txt 39 | 40 | run.sh 41 | com_crashlytics_export_strings.xml 42 | crashlytics-build.properties 43 | Mogujie4Android/libs/eventbus-2.2.1.jar 44 | 45 | # test 46 | test.html 47 | 48 | Mogujie4Android/assets/crashlytics-build.properties -------------------------------------------------------------------------------- /README.zh-cn.md: -------------------------------------------------------------------------------- 1 | ## webp 批量替换图片 gradle plugin 2 | *其他语言版本: [English](README.md).* 3 | 4 | ###更新说明 5 | 6 | #### 1.1.34 7 | 优化print log 的代码 8 | 9 | 10 | ### webp插件介绍 11 | *** 12 | webp转换插件可批量转换 build时 /build/intermediates/res/${flavorName}/${buildType}目录下的图片为webp格式, 13 | 打包出的apk就包含webp的图片了 14 | 15 | 其搜索目标文件的规则如下 16 | 17 | 1. res下以drawable为开头的目录 18 | 2. 后缀为png,jpg的文件 19 | 3. 不包含.9图片 20 | 21 | 22 | webp插件的运行时机是在 processXXXResource Task前 添加一个名为webpConvertPlugin的 task并执行 23 | ### 局限性 24 | *** 25 | Android 从4.0开始对webp格式的文件都有原生支持,所以如果你apk 的minSdkVersion 是 15以上 你不需要修改任何关于图片使用的java代码。 26 | 27 | 但请注意,如果你的项目里的jpg图片包含alpha值的设置,转换后的webp图片是不能再4.2.1以下正常运行的。 28 | 29 | 请参考: 30 | 31 | ![pic](http://7xod3k.com1.z0.glb.clouddn.com/drjlnosgrtrevhtpuyuvlvcmzeenclxf) 32 | 33 | ### 安装webp命令行工具 34 | *** 35 | 36 | 37 | 1. homebrew 安装方法: 38 | 39 | 装了brew 工具的同学可以用brew install webp 40 | 41 | 2. macports 安装方法: 42 | 43 | 在http://distfiles.macports.org/MacPorts/中寻找对应你系统的最新版MacPorts安装包下载并安装 44 | 在终端依次运行以下命令 45 | 46 | export PATH=$PATH:/opt/local/bin 47 | sudo port selfupdate 48 | sudo port install webp 49 | 50 | 通过在终端键入 cwebp判断是否安装成功 51 | 52 | 53 | 54 | 55 | 安装遇到问题请参考: 56 | 57 | 58 | 59 | 60 | 61 | ### webp插件的使用方法如下: 62 | *** 63 | 1. 在外层的build.gradle文件中(即与settings.gradle同级的文件)添加如下代码 64 | 65 | classpath 'com.mogujie.gradle:webpConvertPlugin:1.1.34' 66 | 2. 在内层build.gradle文件中(即与src同级的文件)添加如下代码 67 | 68 | apply plugin: 'webpConvert' 69 | 70 | webpinfo { 71 | //是否在debug时跳过webp转换 72 | skipDebug = true 73 | //是否显示log 74 | isShowLog = false 75 | } 76 | 77 | 3. 在与src同级的目录下添加名为webp_white_list.txt的文件 此文件提供白名单功能 可以设置哪些文件不会被转换为webp文件,配置时,一个文件名为一行,如 78 | 79 | bill_footer_sitepro_arrow.png 80 | cart_checkbox_false.png 81 | 82 | 83 | 好了,万事具备,只要你clean后 assemble一发,png,jpg就替换成功了,打包出的apk就包含webp的图片了.so easy 84 | 85 | gradle clean 86 | gradle assembleDebug 87 | 88 | 89 | ### Licence 90 | *** 91 | webpConvertPlugin is licensed under the MIT license 92 | 93 | 94 | 95 | 96 | 如遇使用问题,请@伯约 boyue@mogujie.com 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /bintray.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.jfrog.bintray' 2 | 3 | def siteUrl = 'https://github.com/mogujie' // 项目的主页 4 | def gitUrl = 'https://github.com/mogujie' // Git仓库的url 5 | group = "com.mogujie.gradle" // Maven Group ID for the artifact,一般填你唯一的包名 6 | version = "1.1.34" 7 | 8 | install { 9 | repositories.mavenInstaller { 10 | // This generates POM.xml with proper parameters 11 | pom { 12 | project { 13 | packaging 'jar' 14 | // Add your description here 15 | name 'webp convert plugin' //项目描述 16 | url siteUrl 17 | // Set your license 18 | licenses { 19 | license { 20 | name 'The MIT License (MIT)' 21 | url 'https://opensource.org/licenses/MIT' 22 | } 23 | } 24 | developers { 25 | developer { 26 | id 'boyue' //填写的一些基本信息 27 | name 'boyue' 28 | email 'daydayupon360@gmail.com' 29 | } 30 | } 31 | scm { 32 | connection gitUrl 33 | developerConnection gitUrl 34 | url siteUrl 35 | } 36 | } 37 | } 38 | } 39 | } 40 | task sourcesJar(type: Jar) { 41 | from sourceSets.main.java.srcDirs 42 | classifier = 'sources' 43 | } 44 | //task javadoc(type: Javadoc) { 45 | // source = android.sourceSets.main.java.srcDirs 46 | // classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 47 | //} 48 | task javadocJar(type: Jar, dependsOn: javadoc) { 49 | classifier = 'javadoc' 50 | from javadoc.destinationDir 51 | } 52 | artifacts { 53 | archives javadocJar 54 | archives sourcesJar 55 | } 56 | 57 | Properties properties = new Properties() 58 | properties.load(project.rootProject.file('local.properties').newDataInputStream()) 59 | bintray { 60 | user = properties.getProperty("bintray.user") 61 | key = properties.getProperty("bintray.apikey") 62 | configurations = ['archives'] 63 | pkg { 64 | repo = "maven" 65 | name = "webp_convert_gradle_plugin" //发布到JCenter上的项目名字 66 | websiteUrl = siteUrl 67 | vcsUrl = gitUrl 68 | licenses = ["MIT"] 69 | publish = true 70 | } 71 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## webp batch convert pics gradle plugin 2 | *Read this in other languages: [简体中文](README.zh-cn.md).* 3 | 4 | 5 | 6 | ###UpdateInfo 7 | 8 | #### 1.1.34 9 | optimize the codes about print log 10 | 11 | ### Introduce 12 | *** 13 | 14 | This plugin can find the jpg,png files under the directory of /build/intermediates/res/${flavorName}/${buildType} while gradle building, 15 | and then auto convert them into webp files. 16 | 17 | So,the generated apk is included with the webp pics. 18 | 19 | 20 | The rule to find the target pic: 21 | 22 | 1. the directories which name starts with 'drawable' 23 | 2. the suffix name of the file is 'jpg' or 'png' 24 | 3. exclude the .9 pic 25 | 26 | The plugin task named webpConvertPlugin,which will execute before processXXXResource Task 27 | 28 | ### Limit 29 | *** 30 | Android support webp file naturly from 4.0 ,so you don't need to change your code if you minSdkVersion is 15+ 31 | 32 | But please pay attention,if your apk is using the jpg with transparency,then the convert webp file will not work below 4.2.1. 33 | 34 | 35 | refer to : 36 | 37 | 38 | ![pic](http://7xod3k.com1.z0.glb.clouddn.com/drjlnosgrtrevhtpuyuvlvcmzeenclxf) 39 | 40 | 41 | ### Install 'webp' command tools 42 | *** 43 | 1. Install by homebrew : 44 | 45 | brew install webp 46 | 47 | 2. install by macports: 48 | 49 | download the necessary pkg on and install 50 | then run the command lines: 51 | 52 | export PATH=$PATH:/opt/local/bin 53 | sudo port selfupdate 54 | sudo port install webp 55 | 56 | 57 | 3. use 'cwebp' command in terminal to check if it installed success. 58 | 59 | 60 | 61 | reference to : 62 | 63 | 64 | 65 | ### Usage: 66 | *** 67 | 1. add below code in the outer build.gradle file(which is in the same directory of settings.gradle) 68 | 69 | classpath 'com.mogujie.gradle:webpConvertPlugin:1.1.34' 70 | 71 | 2. add below code in the inner build.gradle file(which is in the same directory of src) 72 | 73 | apply plugin: 'webpConvert' 74 | 75 | webpinfo { 76 | //if skip the task when debug 77 | skipDebug = true 78 | //if print log 79 | isShowLog = false 80 | } 81 | 82 | 83 | 3. add one file called 'webp_white_list.txt'(it will be created auto if the file don't exist when the plugin executed),it can keep the file you don't want to convert.just as below: 84 | 85 | bill_footer_sitepro_arrow.png 86 | cart_checkbox_false.png 87 | 88 | 89 | now,clean your project then build it .you will find the webp pics is in your generated apk 90 | 91 | gradle clean 92 | gradle assembleDebug 93 | 94 | 95 | ### Licence 96 | *** 97 | webpConvertPlugin is licensed under the MIT license 98 | 99 | 100 | 101 | contact me:boyue@mogujie.com 102 | 103 | 104 | -------------------------------------------------------------------------------- /src/main/groovy/webp/plugin/WebpConvertBuildPlugin.groovy: -------------------------------------------------------------------------------- 1 | package webp.plugin 2 | 3 | import com.android.build.gradle.AppPlugin 4 | import org.gradle.api.Plugin 5 | import org.gradle.api.Project 6 | 7 | class WebpConvertBuildPlugin implements Plugin { 8 | WebpInfo config; 9 | 10 | @Override 11 | void apply(Project project) { 12 | def hasApp = project.plugins.withType(AppPlugin) 13 | 14 | def variants = hasApp ? project.android.applicationVariants : project.android.libraryVariants 15 | 16 | config = project.extensions.create("webpinfo", WebpInfo); 17 | 18 | project.afterEvaluate { 19 | 20 | variants.all { variant -> 21 | 22 | def flavor = variant.getVariantData().getVariantConfiguration().getFlavorName() 23 | def buildType = variant.getVariantData().getVariantConfiguration().getBuildType().name 24 | 25 | if (config.skipDebug == true && "${buildType}".contains("debug")) { 26 | printlog "skipDebug webpConvertPlugin Task!!!!!!" 27 | 28 | return 29 | } 30 | 31 | def dx = project.tasks.findByName("process${variant.name.capitalize()}Resources") 32 | def webpConvertPlugin = "webpConvertPlugin${variant.name.capitalize()}" 33 | project.task(webpConvertPlugin) << { 34 | String resPath = "${project.buildDir}/intermediates/res/${flavor}/${buildType}" 35 | println "resPath:" + resPath 36 | def dir = new File("${resPath}") 37 | dir.eachDirMatch(~/drawable[a-z0-9-]*/) { drawDir -> 38 | printlog "drawableDir:" + drawDir 39 | def file = new File("${drawDir}") 40 | file.eachFile { filename -> 41 | def name = filename.name 42 | def f = new File("${project.projectDir}/webp_white_list.txt") 43 | if (!f.exists()) { 44 | f.createNewFile() 45 | } 46 | def isInWhiteList = false 47 | f.eachLine { whiteName -> 48 | if (name.equals(whiteName)) { 49 | isInWhiteList = true 50 | } 51 | } 52 | if (!isInWhiteList) { 53 | if (name.endsWith(".jpg") || name.endsWith(".png")) { 54 | if (!name.contains(".9")) { 55 | 56 | def picName = name.split('\\.')[0] 57 | def suffix = name.split('\\.')[1] 58 | printlog "find target pic >>>>>>>>>>>>>" + name 59 | printlog "picName:" + picName 60 | 61 | 62 | "cwebp -q 75 -m 6 ${filename} -o ${drawDir}/${picName}.webp".execute() 63 | sleep(10) 64 | "rm ${filename}".execute() 65 | printlog "delete:" + "${filename}" 66 | printlog "generate:" + "${drawDir}/${picName}.webp" 67 | 68 | } 69 | } 70 | } 71 | 72 | } 73 | } 74 | } 75 | 76 | project.tasks.findByName(webpConvertPlugin).dependsOn dx.taskDependencies.getDependencies(dx) 77 | dx.dependsOn project.tasks.findByName(webpConvertPlugin) 78 | } 79 | 80 | } 81 | 82 | } 83 | 84 | void printlog(String msg) { 85 | if (config.isShowLog == true) { 86 | println msg 87 | } 88 | } 89 | } 90 | 91 | 92 | class WebpInfo { 93 | boolean skipDebug 94 | boolean isShowLog 95 | } 96 | --------------------------------------------------------------------------------