Android 硬编码提取工具

  • 原文发布与个人博客KuTear,转载请注明 *

英文文档

英文

GITHUB

Android 硬编码提取工具

提取Android Layout硬编码的string和dimens出来

点击下载|Download

用法

Example
#linux/mac
./Fuck-Hard-Code -input="{layout path}" -output="{layout output path}"

#windows
Fuck-Hard-Code.exe -input="{layout path}" -output="{layout output path}"

执行之后你可以在这个目录{layout output path}/out/看见两个文件strings.xmldimens.xml以及其他所有的layout文件在{layout output path}/

实例

找到自己App的Layout目录(或新建Application),
下面展示其中一个文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:text="Hello Word"
        android:textSize="20sp" />
</LinearLayout>

运行工具

> Fuck-Hard-Code -input="{project path/app/src/main/res/layout}" -output="{out path/layout}"

之后我们可以在{out path/layout}下看到对应文件的内容被修改为

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="top">
    <TextView
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/dp_0040_0"
        android:text="@string/strings_0"
        android:textSize="@dimen/sp_0020_0" />
</LinearLayout>

{out path/layout/out} 中有两个文件,strings.xml 和 dimens.xml,内容分别为

<!--strings.xml-->
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="strings_0">Hello Word</string>
</resources>
<!--dimens.xml-->
<?xml version="1.0" encoding="utf-8"?>

<resources>
    <dimen  name="dp_0040_0">40.0dp</dimen>
    <dimen  name="sp_0020_0">20.0sp</dimen>
</resources>

这样就替换了布局中所有的硬编码格式。可以使用文件比较工具查看之后,没有问题就替换掉以前的布局代码

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,099评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,956评论 25 709
  • 1.创建文件夹 !/bin/sh mkdir -m 777 "%%1" 2.创建文件 !/bin/sh touch...
    BigJeffWang阅读 10,228评论 3 53
  • 每个人都有喜欢做的事情,这不同于工作,而更像是一种纯粹的消遣。 前段时间有几位好朋友找我去给他们拍艺术照,他们知...
    做旧时光阅读 477评论 1 2
  • 转眼间,我在家工作已经有3个月的时间了。 在这样的三个月的时间里面,我的内心变化非常的大。 回想起,我刚开始画三维...
    普通女孩阅读 199评论 0 0