我在一个项目中工作,我使用多个库,如 google play service、retrofit、gson、glide、twitter 和 facebook sdk。所以我想知道每个库在我的应用程序中占用的确切大小。请帮助我是否有任何可能的方法来分析 Android Studio 中的大小。任何符合我要求的工具建议或提示都会对我很有帮助。我将在我的 build.gradle
中使用的依赖项发布如下。
compile('com.twitter.sdk.android:twitter:1.9.0@aar') {
transitive = true;
}
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'org.twitter4j:twitter4j-core:4.0.2'
compile 'com.github.bumptech.glide:glide:3.6.1'
compile 'com.google.android.gms:play-services-gcm:8.4.0'
compile 'com.android.support:support-v4:23.2.0'
注意:我分析这份报告是为了通过删除占用大量内存的库来减少我的应用程序的 apk 大小。
在过去的几天里,我完全坚持使用这个解决方案。我什至搜索了很多,但找不到优化的方法来计算项目中库的确切大小使用情况。
请帮忙。提前致谢。
最佳答案
不确定它是否是您正在寻找的东西,但这可能会有所帮助:
task depsize {
doLast {
final formatStr = "%,10.2f"
final conf = configurations.default
final size = conf.collect { it.length() / (1024 * 1024) }.sum()
final out = new StringBuffer()
out << 'Total dependencies size:'.padRight(45)
out << "${String.format(formatStr, size)} Mb\n\n"
conf.sort { -it.length() }
.each {
out << "${it.name}".padRight(45)
out << "${String.format(formatStr, (it.length() / 1024))} kb\n"
}
println(out)
}
}
该任务打印出所有依赖项的总和,并打印出以 kb 为单位的大小,按大小 desc 排序。
更新版本,兼容Gradle 5+
tasks.register("depsize") {
def formatStr = "%,10.2f"
def size = configurations.default.collect { it.length() / (1024 * 1024) }.sum()
def out = new StringBuffer()
out << 'Total dependencies size:'.padRight(45)
out << "${String.format(formatStr, size)} Mb\n\n"
configurations
.default
.sort { -it.length() }
.each {
out << "${it.name}".padRight(45)
out << "${String.format(formatStr, (it.length() / 1024))} kb\n"
}
println(out)
}
更新 #2:最新版本的任务代码可以在 github gist 上找到
关于android - 如何计算在 Android 项目中作为依赖项添加的库的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36897343/