使用AndResGuard后遇到AssetManager#list(String path)返回为空的问题

本文介绍了AndResGuard工具在Android资源文件混淆打包时遇到的一个问题:AssetManager#list(String path)返回空的情况,该问题发生在使用7zip压缩时,并提供了解决方案。

我的新书《Android App开发入门与实战》已于2020年8月由人民邮电出版社出版,欢迎购买。点击进入详情

AndResGuard是一款很好的Android资源文件混淆打包的工具。

传送门:https://github.com/shwenzhang/AndResGuard

以往我们使用proguard只对源代码进行了混淆,而忽略了对资源文件的混淆,AndResGuard就是为了解决这一问题的。

使用过程中Proguard和AndResGuard可以混合使用。

但是在使用过程中有一个比较隐秘的问题,现在拿出来说下,毕竟这个会有比较大的影响。

问题描述:

AssetManager#list(String path)返回空。

产生的场景:

AndResGuard打包时,use7zip设置为true,也就是开启7zip导致的;

解决方案:

将use7zip设置为false,也就是不用7zip进行压缩优化,AndResGuard仍然能够对资源文件进行混淆,不影响使用;

相同问题见:https://github.com/shwenzhang/AndResGuard/issues/162

package com.oplus.engineermode.security.sdk.ese.sn220.util; import android.content.Context; import android.content.res.AssetManager; import com.nxp.sems.SemsAgent; import com.nxp.sems.SemsException; import com.oplus.engineermode.core.sdk.utils.Log; import com.oplus.engineermode.security.sdk.ese.sn220.AppletManagInterface; import com.oplus.engineermode.security.sdk.ese.sn220.SemsScript; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SemsScriptUtil { private static final String TAG = "SemsScriptUtil"; private static final String SEMS_SCRIPT_PATH = "sems"; public static String load(AssetManager assetManager, String scriptPrefix) { String script = ""; try { String fileName = getScriptFileName(assetManager, scriptPrefix); if (fileName.isEmpty()) { Log.w(TAG, "SEMS Script not found"); return script; } InputStream scriptStream = assetManager.open(SEMS_SCRIPT_PATH + "/" + fileName); StringBuilder scriptContent = new StringBuilder(); byte[] buffer = new byte[1024]; int nBytes; while ((nBytes = scriptStream.read(buffer)) > 0) { scriptContent.append(new String(buffer, 0, nBytes, StandardCharsets.UTF_8)); } scriptStream.close(); if (scriptContent.length() > 0) { Pattern p = Pattern.compile("\\s*|\t|\r|\n"); Matcher m = p.matcher(scriptContent.toString()); script = m.replaceAll(""); } } catch (IOException e) { Log.e(TAG, "Load SEMS Script Failed", e); } return script; } private static String getScriptFileName(AssetManager assetManager, String scriptPrefix) { String scriptFileName = ""; try { String suffix = ".txt"; String[] assets = assetManager.list(SEMS_SCRIPT_PATH); if (assets != null) { Log.d(TAG, "Total assets found: " + assets.length); for (String asset : assets) { Log.d(TAG, "Checking asset: " + asset); if (asset.startsWith(scriptPrefix) && asset.endsWith(suffix)) { Log.d(TAG, "SEMS Script " + asset + " found"); scriptFileName = asset; break; } } } else { Log.e(TAG, "Asset list is null for path: " + SEMS_SCRIPT_PATH); } } catch (IOException e) { Log.e(TAG, "List SEMS Script Path Failed", e); } return scriptFileName; } } SEMS Script not found打印 找什么文件失败的
07-10
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值