Giter Site home page Giter Site logo

yuweiguocn / android-resource-remover Goto Github PK

View Code? Open in Web Editor NEW

This project forked from keepsafe/android-resource-remover

6.0 0.0 1.0 345 KB

A simple utility to remove unused resources in your Android app to lower the size of the APK. It's based on the Android lint tool output.

License: Apache License 2.0

Python 100.00%

android-resource-remover's Introduction

android-resource-remover

一个根据lint结果自动化清理无用资源的python脚本工具。

使用lint检查无用资源

通常我们可以使用lint检查工程内无用资源,但执行lint命令发现只检查了主Module的无用资源,并没有检查子Module的无用资源。

针对这个问题可以使用下面的配置,对应用的依赖也执行检查:

lintOptions {
    checkReleaseBuilds false
    abortOnError false
    checkDependencies true //对依赖的资源也执行检查,注意也会对引用aar检查,如果是子module打开源码依赖即可
    check "UnusedResources" //只检查无用资源,提升执行速度
}

然后执行lint命令进行检查无用资源:

./gradlew lint

然后我们可以在build下的reports目录下找到lint的结果。

配置

首先将本工程clone到你的项目工程的根目录中,然后根据你项目使用反射获取的资源在resouceCleanConfig.json文件中进行配置:

{
	"projects": [
		"/app/",
		"/push/"
	],
	"pathIncludes": [
		".jpg",
		".png",
		".webp",
		".xml"
	],
	"pathExcludes": [
		"------common-config-start--------",
		"/layout/"
	]
}

  • projects:表示要清理无用资源的路径需要包含的子Module工程名称,因为依赖检查也会对引用的所有aar进行检查,所以我们需要指定打开源码的子Module的名称。
  • pathIncludes:表示要清理的无用资源的路径需要匹配的规则。
  • pathExcludes:表示要清理的无用资源的路径不能包含的字符串,也就是白名单。

以上三个条件为并列条件,只有同时满足才会被清理。上述json配置的文件表示清理app和push Module下文件后缀为.jpg或.png或xml的资源,但不删除布局文件。

白名单

目前发现工程使用代码获取资源的方法有以下两种,代码中使用下列方法获取的图片名称需要添加配置文件白名单中。

//第一种
item.resId = mContext.getResources().getIdentifier(name, "drawable", mContext.getPackageName());
//第二种
public static Integer getResourceDrawableId(String resource) {
    if(TextUtils.isEmpty(resource)){
        return -1;
    }
    try {
        String name = resource.trim();
        Field field = R.drawable.class.getField(name);
        return field.getInt(null);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return -1;
}

建议对新增的资源在使用代码获取时对名称添加统一前缀reflect_

自动化

此步骤请谨慎操作,删除的资源无法恢复,使用前请添加版本控制。

配置好后执行如下命令自动清理无用资源:

python android_clean_app.py --xml ../app/build/reports/lint-results.xml

参数--xml指定为你的lint结果文件的路径。

解决打包失败问题

lint的检测结果包含无用代码对资源的引用,如果只是将无用资源删除后可能会引起打包失败。 针对这个问题我们会自动添加一个unused_ids.xml文件保存删除的资源id,这可以保证不会由于删除无用资源引起的打包失败。

运行结果

Licence

Apache version 2.0

android-resource-remover's People

Contributors

ca77y avatar fafhrd91 avatar gitter-badger avatar mrmaffen avatar philippb avatar snejku avatar yuweiguocn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

meijiancheng

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.