- 原文發(fā)布與個人博客KuTear,轉載請注明 *
英文文檔
Android 硬編碼提取工具
提取Android Layout硬編碼的string和dimens出來
用法

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}"
執(zhí)行之后你可以在這個目錄{layout output path}/out/看見兩個文件strings.xml 和 dimens.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>
這樣就替換了布局中所有的硬編碼格式。可以使用文件比較工具查看之后,沒有問題就替換掉以前的布局代碼