R.java 中受保护的 Android ID
对于那些在 Android 源代码中花费了大量时间的用户来说,android.R
类中生成的 ID 并不 1:1 反映 android.R
中找到的实际资源,这并不是新闻。所提供 JAR 的 code>res/ 目录。许多可绘制/样式/布局不是“公共”的,应用程序可以通过引用android.R.xxx
来访问。
我的问题是,有谁知道 Android 能够生成与实际资源图不同的 R.java 类的机制吗?其次,它是否是一种机制(使用构建规则等),我们作为开发人员可以利用它来部分保护在用作库的应用程序中公开的 id?
提前致谢!
For those of use who have spent any amount of time in the Android source code, it's not news that the IDs generated in the android.R
class do not 1:1 reflect the actual resources found in the res/
directories of the supplied JAR. Many of the drawables/styles/layouts are not "public" and accessible to applications by referencing android.R.xxx
.
My question is does anyone know the mechanism by which Android
is able to generate an R.java class that differs from the actual resource graph? Secondarily, is it a mechanism (using build rules, etc.) that we as developers could leverage to partially protect the ids that get made public in applications used as libraries?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Android使用aapt打包资源,并在构建过程中使用该工具生成R文件。在此步骤中,XML 文件被编译为二进制文件,其他资源则按原样打包。您可以将 .apk 文件作为 zip 存档打开,并按原样访问所有资源文件。库项目仅包含在编译时添加到项目中的源文件和资源,因此它们将以某种方式存在于 .apk 文件中。布局可能不是 1:1,因为已经为库项目编译了 XML。可能有一种方法可以使用 aapt 从库项目中删除文件,以免在构建时包含这些文件,但随后它们将无法在您的项目中访问。还可能有一种方法可以使用 aapt 和 ant 在编译时混淆资源名称,但这并不能阻止它们访问它们。
我不确定为什么您想要一个与实际资源图不同的 R.java 类。要么在构建时不包含这些资源,要么它们将包含在 apk 中。您的布局在输出中被压缩为二进制文件,因此我不确定它们在另一个项目中反编译或重用有多容易。
这里的结局是什么?如果是为了保护你的资源,那就很难了。如果有人能够 root 其手机,Android 就极其不安全。如果你想保护你的 IP,我假设这是最后的结局,我会尝试将我的项目编译成 apk,看看是否可以反编译或从 apk 中提取关键资源。也许尝试一些混淆,但同样,我对编译后的 XML 文件了解不够,不知道它们是否被混淆到无法轻易反编译。
如果我错了,请有人随时纠正我,但这只是我的 2 美分。
Android packages resources using aapt, and generates the R file using this tool during the build process. In this step, XML files are compiled to binary and other resources are just packaged as they are. You can open up a .apk file as a zip archive and access all the resource files as is. Library projects simply contain source files and resources that are added into the project at compile time, so they would exist somehow in the .apk file. Probably the layouts are not 1:1 because the XML is already compiled for the library project. There is probably a way you could use aapt to remove the files from the library project from being included at build time, but then they wouldn't be accessible in your project. There also might be a way to obfuscate your resource names at compile time using aapt and ant, but then that doesn't stop them from being able to access them.
I'm not sure why you would want to want an R.java class that differs from the actual resource graph. Either don't include those resources at build time or they are going to be in the apk. Your layouts are compressed as binary files in the output, so I'm not sure how easy they are to decompile or reuse in another project.
What is the endgame here? If it is to protect your resources, it will be hard. Android is extremely insecure if someone has the ability to root their phone. If you want to protect your IP, which I'm assuming is the endgame here, I would try and compile my project into an apk and see if I can decompile or extract the critical resources from the apk. Maybe try some obfuscation, but again, I don't know enough about the compiled XML files to know if they get obfuscated enough to not be easily decompiled.
Someone feel free to correct me if I'm wrong, but this is just my 2 cents.