{"id":30496325,"url":"https://github.com/code-wgl/dimensauto","last_synced_at":"2025-08-25T01:33:08.076Z","repository":{"id":179702236,"uuid":"267041741","full_name":"code-wgl/DimensAuto","owner":"code-wgl","description":"一款开发无感知、即时编译的 dimens 自动化转换工具、 gradle 插件脚本","archived":false,"fork":false,"pushed_at":"2020-08-16T14:48:36.000Z","size":355,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-27T00:48:05.128Z","etag":null,"topics":["android","dimens","gradle","groovy"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/code-wgl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-05-26T12:51:22.000Z","updated_at":"2024-08-28T02:46:14.000Z","dependencies_parsed_at":null,"dependency_job_id":"118ce589-8f4d-4305-b1d4-169d19448169","html_url":"https://github.com/code-wgl/DimensAuto","commit_stats":null,"previous_names":["developer-wgl/dimensauto","code-wgl/dimensauto"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/code-wgl/DimensAuto","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code-wgl%2FDimensAuto","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code-wgl%2FDimensAuto/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code-wgl%2FDimensAuto/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code-wgl%2FDimensAuto/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/code-wgl","download_url":"https://codeload.github.com/code-wgl/DimensAuto/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code-wgl%2FDimensAuto/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271991151,"owners_count":24854718,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-24T02:00:11.135Z","response_time":111,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","dimens","gradle","groovy"],"created_at":"2025-08-25T01:33:05.894Z","updated_at":"2025-08-25T01:33:08.060Z","avatar_url":"https://github.com/code-wgl.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# DimensAuto\n[![](https://img.shields.io/badge/version-1.2.0-orange?branch=master)](https://img.shields.io/badge/version-1.2.0-orange)\n[![](https://img.shields.io/github/issues/developer-wgl/DimensAuto?branch=master)](https://img.shields.io/github/issues/developer-wgl/DimensAuto)\n[![](https://img.shields.io/github/stars/developer-wgl/DimensAuto?branch=master)](https://img.shields.io/github/stars/developer-wgl/DimensAuto)\n[![](https://img.shields.io/github/forks/developer-wgl/DimensAuto?branch=master)](https://img.shields.io/github/forks/developer-wgl/DimensAuto)\n[![](https://img.shields.io/github/license/developer-wgl/DimensAuto?branch=master)](https://img.shields.io/github/license/developer-wgl/DimensAuto)\n\n一款开发无感知、即时编译的 `dimens` 自动化转换工具、 `gradle` 插件脚本\n\n## 背景\n去年刚加入小爱时，对于小米这种适配`values-nxhdpi`情况很不适应，有几点写业务的时候很不爽：\n- 每次人工映射效率低；\n- 更改一处值，另一处忘记更改时，UI/测试对于部分机型都不会看出问题，容易出错；\n- 新同事可能并不了解此适配，项目中有很多并未适配的 `dimens` 键值，另外沟通还需要额外工作；   \n\n\u003cbr\u003e\n其实网上对于这种自动化脚本已经很多了，而且小爱这边也是有同事写过这种类似脚本的，那我为什么还要决定在搞一个呢？\n\n- 冲浪了很久。发现都需要单独运行，然后拷贝到项目中、每次改值重新生成，对于有强迫症、比较关心开发效率的我总是感觉很不爽（就是懒、嫌麻烦~）\n\u003cbr\u003e\n\n所以针对以上几点，在工作之余花了几个小时写了 Android Studio 插件 [DimensPlugin](https://github.com/developer-wgl/DimensPlugin)，可在 IDE 中通过 `‘划词’` 这种形式，划取想要的 `dimens` 再使用 `快捷键` 就可以将选中的键值进行自动转换。\n\n但是在组内推广过程中，其实还是有额外成本的和常见的插件并未有很大差别。经过和同事讨论，得到了一些建议，于是就有了此完全自动化的脚本插件，也经过小爱这边线上项目实践未出现过问题，收到同事一致好评~\n\n说了这么多废话，这东西到底哪点方便呢？\n\n## 与其他相比，优势在哪？\n\n1. Gradle 插件脚本，在编译时会自动转换；\n    - 编译和 `dimens` 数量有关，大数量时，也能在 10ms 以内搞定。编译时会有耗时输出；\n    - 编译时会自动生成对应文件夹及文件，对项目旧版 `dimens` 兼容；\n    - 编译时会有提示、警告、错误输出；\n    - 可针对 `module` 单独配置，也可以全局配置；\n    - 开发只需填写一份 `dimens` 无感知其他`dpi`生成；\n    - 配置简单，只需要一行代码；\n2. 支持自定义配置扩展，可全局配置； \n3. 脚本大小 8K，不会被编入`APP`包内；\n4. 支持跨平台开发，已适配 linux、mac、window 平台；\n5. 支持 `git` 等代码管理工具动态管理；\n\n\n## 如何使用\n\n使用常见设计稿中的 `xxhdpi@3x` 中的值（如果设计稿中没有@3x选项，可按照此方式处理：`xxhdpi@3x` = px值/3）放到默认 `values/dimens_auto.xml` 里面按照正常开发去使用就可以了，没了..  \n\n其实就是把正常开发放到 `dimens.xml` 里面的键值改放到 `dimens_auto.xml` 里面（注意两个文件的 `dimens` 键不要重复哦，会有编译报错的）开发了，其他的与正常开发没有任何区别。插件会自动处理转换 `dimens_auto.xml` ，开发者只是换了个位置存放而已，所以才有了开发无感知的说法~\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://img-blog.csdnimg.cn/20200810212929667.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2d1YW5nX2xpYW5nXw==,size_16,color_FFFFFF,t_7\" height=\"400px\"/\u003e\n\u003c/p\u003e\n\n\n## 如何配置\n\n1. 下载 [dimens_auto.gradle](./dimens_auto.gradle)\n2. 在需要使用此插件的 `module` 中的 `build.gradle` 文件头部按照以下操作加入代码：\n\n``` gradle\n// module内使用此方式使用\n// 若将 `dimens_auto.gradle` 以拷贝至项目根目录时（其他位置注意路径引入即可），引入方式如下：\napply from: \"../dimens_auto.gradle\" \n```\n3. 打开项目内的 `dimens_auto.gradle` 自定义配置参数，可配置参数如下： \n`dimens_auto.gradle` 针对自己项目更改配置参数，可配置参数如下：\n\n```gradle\n    /* 参数配置 */\n    def config = [\n            \"dimens_path\"  : \"\",       // 要转换的 `dimens_auto.xml` 相对、`module`的路径. \n            \"convert_rules\": [],       // 转换规则，可自定义扩展。 如 ：[\"hdpi\": 3/1.5,\"xhdpi\": 3/2,\"sw600dp-xhdpi\": 3/2,\"nxhdpi\": 3/2.75,\"xxxhdpi\": 3/4] \n            \"convert_unit\" : [],       // 要转换的单位。 如 [\"dp\", \"dip\", \"sp\"]\n            \"decimal\"      : 2,        // 转换后小数点保留位数\n            \"init_hint\"    : \"\",       // 文件初次创建时的提示语句\n            \"help_url\"     : \"\",       // 帮助文档\n            \"debug\"        : false\n    ]\n```\n\n4. 点击 IDE “运行“ 按钮运行项目，build过程中可以看到 `dimens auto:`  日志输出。如果没有异常输出并且 `src/res` 中已经存在了你配置的 `dimens_auto` 文件则说明配置成功。\n\n    \u003cimg src=\"https://img-blog.csdnimg.cn/20200810212929815.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2d1YW5nX2xpYW5nXw==,size_16,color_FFFFFF,t_70\" width=\"500px\"\u003e\n\n\n## 遇到问题？\n\n1.  何时被执行（转换）\n* 自动执行：Android Studio 编译时会被默认执行   \n    ![自动运行](https://img-blog.csdnimg.cn/20200810212929552.jpg)\n\n* 手动执行：打开 `dimens_auto.gradle` 点击task 左侧按钮运行 \n\n    \u003cimg src=\"https://img-blog.csdnimg.cn/20200810212929616.png\" width=\"400px\"/\u003e\n\n* 手动执行：打开Android Studio 打开菜单 View → Tool Windows → Gradle → Project Name → Tasks → other → dimens_auto 双击执行\n    \u003cimg src=\"https://img-blog.csdnimg.cn/20200810212929829.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2d1YW5nX2xpYW5nXw==,size_16,color_FFFFFF,t_70\" width=\"400px\"/\u003e\n\n2. 不想被转换怎么办\n-  `dimens_auto.gradle`中会有一些参数配置，并不是所有放在 `dimens_auto.xml` 的值都会被转换。默认配置为只处理`【dp、dip、sp】`，未被转换时会抛出警告。如果你不想被转换的值就是dp、dip、sp，那请看下一条\n\n- 当前转换逻辑为处理当前`module`中的默认名为 `dimens_auto.xml` 的文件进行转换，所以把自己不想自动转换的值放到其他dimen文件中即可，如默认的dimens.xml\n\n\n4. 原理是什么\n把默认的dimens值设为1，在代码编译时会根据配置比例进行转换。\n至于匹配规则通过正则编写，至于生成使用的是gradle库进行的生成。 \n\n5. 转换需要多长时间\n自动转换了5个对应的dimens文件，耗时 118 ms .   只转换 default → nxhdpi 耗时 25 ms.，根据电脑性能有关。\n \n6. 只想局部转换怎么办\n我写的一个Idea/Android Studio 插件，可以划词选中后按快捷键自动转换：[DimensPlugin](https://github.com/developer-wgl/DimensPlugin)\n\n\n7. 目前支持哪种格式的dimens转换\n目前已做适配的有以下三种。其他格式的可以自己编译看下转换后的结果，可能也会支持。\n    ![支持dimens转换](https://img-blog.csdnimg.cn/20200810212929547.png)\n\n8. 关于跨平台问题（linux/MAC/windows）\n- windows 运行后不能运行，有乱码产生，已修复;\n- windows 平台运行后有系统换行符（LF/CRLF）change问题:\n    - 需关掉 `git` 编译自动 `crlf` 设置\n    ```git\n    git config --global core.autocrlf false\n    ```\n\n9. 为什么是转换 `dimens_auto.xml` 不是直接转换 `dimens.xml`  呢？\n如果你是全新项目，完全可以尝试更改配置文件直接转换 `dimens.xml` ，这样更省去了同事间推广、同步的成本。\n对于这个问题， 其实读者应该很有疑惑。主要是出于以下几点考虑，才使用了新的 `dimens_auto.xml` 文件用于开发者填写：\n-  主要是出于兼容旧版 `dimen` 文件考虑的。项目中已经现有很多 `dimen`，如果冒然直接更改还是风险的，如果你的项目确认没风险，不妨可以一试\n- 出于同事接收新鲜事物的接受缓冲考虑。毕竟程序员同事如果某一天发现代码消失不见，而且看不懂新的插件运行规则还是很崩溃的。留有旧的 `dimen` 他们可以有缓冲的余地，当某一天尝到了 ”螃蟹的香味“ 也就会开心的转移阵地了\n-  出于上述 `问题6` 考虑。还是需要一个 `dimen.xml` 文件用于存放不想转换的键值的\n\n## 沟通交流\n写一个开源工具和一套完整的文档实属不易。如果觉得还可以，跪求留下你的 `Star` 鼓励下~\n\n欢迎 [issues](https://github.com/developer-wgl/DimensAuto/issues) ，留下你遇到的问题和宝贵的建议，我很愿意与你一起成长技术~\n\n\u003cbr\u003e\n\n对应博客地址：https://blog.csdn.net/guang_liang_/article/details/107922973\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcode-wgl%2Fdimensauto","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcode-wgl%2Fdimensauto","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcode-wgl%2Fdimensauto/lists"}