- 原文發(fā)布與個(gè)人博客KuTear,轉(zhuǎn)載請(qǐng)注明 *
英文文檔
Android 硬編碼提取工具
提取Android Layout硬編碼的string和dimens出來(lái)
用法
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í)行之后你可以在這個(gè)目錄{layout output path}/out/
看見(jiàn)兩個(gè)文件strings.xml
和 dimens.xml
以及其他所有的layout文件在{layout output path}/
實(shí)例
找到自己App的Layout目錄(或新建Application),
下面展示其中一個(gè)文件
<?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>
運(yùn)行工具
> Fuck-Hard-Code -input="{project path/app/src/main/res/layout}" -output="{out path/layout}"
之后我們可以在{out path/layout}
下看到對(duì)應(yīng)文件的內(nèi)容被修改為
<?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}
中有兩個(gè)文件,strings.xml 和 dimens.xml,內(nèi)容分別為
<!--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>
這樣就替換了布局中所有的硬編碼格式归园〖对猓可以使用文件比較工具查看之后饭冬,沒(méi)有問(wèn)題就替換掉以前的布局代碼