如何设置 aapt add 命令添加文件的路径

发布于 2024-11-11 18:29:12 字数 263 浏览 7 评论 0原文

我正在使用 aapt 工具从我的 apk 的不同文件夹中删除一些文件。这很好用。

但是当我想向 apk 添加文件时,aapt 工具 add 命令不允许我指定要添加文件的路径,因此我只能将文件添加到 apk 的根文件夹中。 这很奇怪,因为我认为开发人员永远不会想将文件添加到 apk 的子文件夹(例如 res 文件夹)。这可以通过 aapt 或任何其他方法实现吗?因为从任何文件夹中删除文件都可以正常工作,而添加文件仅适用于 apk 的根文件夹。无法将其用于任何其他文件夹。

谢谢

I'm using aapt tool to remove some files from different folders of my apk. This works fine.

But when I want to add files to the apk, the aapt tool add command doesn't let me specify the path to where I want the file to be added, therefore I can add files only to the root folder of the apk.
This is strange because I don't think that developers would never want to add files to a subfolder of the apk (res folder for example). Is this possible with aapt or any other method? Cause removing files from any folder works fine, and adding file works only for the root folder of the apk. Can't use it for any other folder.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱娆 2024-11-18 18:29:12

aapt 工具保留在 add 命令中指定的目录结构,如果您想将某些内容添加到 apk 中的现有文件夹中,您只需在系统上有一个类似的文件夹,并且必须指定要添加的每个文件,完整列出该目录。示例

$ aapt list test.apk
res/drawable-hdpi/pic1.png
res/drawable-hdpi/pic2.png
AndroidManifest.xml

$ aapt remove test.apk res/drawable-hdpi/pic1.png
$ aapt add test.apk res/drawable-hdpi/pic1.png

将添加的 pic1.png 位于终端 res/drawable-hdpi/ 的当前工作目录中的文件夹中,希望这能回答您的问题

The aapt tool retains the directory structure specified in the add command, if you want to add something to an existing folder in an apk you simply must have a similar folder on your system and must specify each file to add fully listing the directory. Example

$ aapt list test.apk
res/drawable-hdpi/pic1.png
res/drawable-hdpi/pic2.png
AndroidManifest.xml

$ aapt remove test.apk res/drawable-hdpi/pic1.png
$ aapt add test.apk res/drawable-hdpi/pic1.png

The pic1.png that will is added resides in a folder in the current working directory of the terminal res/drawable-hdpi/ , hope this answered your question

为人所爱 2024-11-18 18:29:12

实际上,aapt 中存在一个错误,该错误会导致这种情况随机发生。它应该工作的方式正如其他答案所声明的那样:保留路径,除非您传递 -k 。让我们看看它是如何实现的:

控制路径是否被忽略的标志是 mJunkPath

bool        mJunkPath;

变量位于一个名为 Bundle 的类中,并由两个访问器控制:

bool getJunkPath(void) const { return mJunkPath; }
void setJunkPath(bool val) { mJunkPath = val; }

这个 用户在命令行指定 -k,它被设置为 true

case 'k':
    bundle.setJunkPath(true);
    break;

并且,当将数据添加到文件时,会进行检查:

if (bundle->getJunkPath()) {
    String8 storageName = String8(fileName).getPathLeaf();
    printf(" '%s' as '%s'...\n", fileName, storageName.string());
    result = zip->add(fileName, storageName.string(),
                      bundle->getCompressionMethod(), NULL);
} else {
    printf(" '%s'...\n", fileName);
    result = zip->add(fileName, bundle->getCompressionMethod(), NULL);
}

不幸的是,应用程序使用的Bundle实例在堆栈上的main中分配,构造函数中没有对mJunkPath进行初始化,因此该值变量是随机的;如果没有办法将其显式设置为 false,在我的系统上,我(似乎是确定性的)无法在指定路径添加文件。

但是,您也可以只使用 zip,因为 APK 只是一个 Zip 文件,并且 zip 工具可以正常工作。

(郑重声明,我还没有将这个简单的修复程序作为 Android 的补丁提交,如果其他人想要这个世界可能会是一个更好的地方。我在 Android 代码提交过程中的经验是必须忍受极其复杂的提交机制,最终花了六个月的时间才有人回复我,在某些情况下,只要他们的提交过程不是那么复杂,就可以进行一些细微的修改,因为有一个非常简单的过程。这个问题的解决方法,我认为没有必要再次烦恼所有这些问题。)

There is actually a bug in aapt that will make this randomly impossible. The way it is supposed to work is as the other answer claims: paths are kept, unless you pass -k. Let's see how this is implemented:

The flag that controls whether the path is ignored is mJunkPath:

bool        mJunkPath;

This variable is in a class called Bundle, and is controlled by two accessors:

bool getJunkPath(void) const { return mJunkPath; }
void setJunkPath(bool val) { mJunkPath = val; }

If the user specified -k at the command line, it is set to true:

case 'k':
    bundle.setJunkPath(true);
    break;

And, when the data is being added to the file, it is checked:

if (bundle->getJunkPath()) {
    String8 storageName = String8(fileName).getPathLeaf();
    printf(" '%s' as '%s'...\n", fileName, storageName.string());
    result = zip->add(fileName, storageName.string(),
                      bundle->getCompressionMethod(), NULL);
} else {
    printf(" '%s'...\n", fileName);
    result = zip->add(fileName, bundle->getCompressionMethod(), NULL);
}

Unfortunately, the one instance of Bundle used by the application is allocated in main on the stack, and there is no initialization of mJunkPath in the constructor, so the value of the variable is random; without a way to explicitly set it to false, on my system I (seemingly deterministically) am unable to add files at specified paths.

However, you can also just use zip, as an APK is simply a Zip file, and the zip tool works fine.

(For the record, I have not submitted the trivial fix for this as a patch to Android yet, if someone else wants to the world would likely be a better place. My experience with the Android code submission process was having to put up with an incredibly complex submission mechanism that in the end took six months for someone to get back to me, in some cases with minor modifications that could have just been made on their end were their submission process not so horribly complex. Given that there is a really easy workaround to this problem, I do not consider it important enough to bother with all of that again.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文