重新打包 APK 文件以包含自定义资产 - 使用什么构建工具?

发布于 2024-10-14 18:27:43 字数 1848 浏览 6 评论 0原文

更新:这是一篇旧帖子,下面对损坏的 aapt 版本的引用将会过时。


根据之前的反馈,我将自定义文本字段存储在我的应用程序的资产目录中。

我将使用资产文件中的默认用户详细信息编写应用程序,并且客户端希望为每个用户重建应用程序,包括资产文件中该用户的详细信息。

(我知道这种方法有一些严重的缺点,但客户仍然热衷于这样做 - 请参阅 在APK文件中嵌入登录详细信息,每个用户都不同(或其他选项?)

这个问题与我遇到的麻烦有关解压缩 APK 文件并更新自定义资源文件后,会重建该文件。我确信我错过了一些小东西,但是,我在这些方法上找到的文档和帖子对于新手来说没有足够的帮助。

  1. aapt - Android sdk 工具:不幸的是,android 文档 [下面的链接 2] 上有关“使用 aapt”的 android 文档非常有限。控制台命令 -help 显示更多信息。当尝试使用它向 test.apk 添加文件时,它最终会删除原始文件,并创建一个仅包含我尝试添加的文件的新文件 test.apk.zip。我一直无法找到正确的命令行组合来获取解压的 apk 并重新打包它 - 这将是我的头等奖。

  2. apkbuilder - Android sdk 工具:首先,这个工具已被弃用,这对它来说是一个缺点。我也无法让它与解压文件夹中的内容一起使用。我想我错过了 apkbuilder 之前的步骤,因为 apkbuilder 要求提供资源 zip 存档,而我有一个资源文件夹。

  3. ant - 构建工具:其他类似的帖子说使用 ant 构建,而不是使用 android 工具。我在让 ant 工作时遇到问题。 Getting Ant to Work with Android [下面的链接 3] 的一个特定链接看起来很有希望,但看起来它是针对不同的 android sdk(我的由 android 生成的 build.xml 看起来与他的不同)。不幸的是,我对 ant 知之甚少,而且很难成为足够的专家来解决我当前的问题。
    进一步到下面 NickT 的解决方案 - 运行 ant 脚本给我错误
    使用类加载器 AntClassLoader[] 无法找到 taskdef 类 com.android.ant.SetupTask
    我在网上找到了一些对此错误的参考,并确认 local.properties 有一个 sdk.dir 设置指向我的 android sdk 安装文件夹 (sdk.dir=/Applications/android-sdk-mac_86)。

  4. ??????:可能还有一些我没有列出/发现的其他选项,我有兴趣了解一下。

我意识到深入研究通常被我的IDE掩盖的齿轮可能会导致困难。但我知道很多 SO 用户可以做很多这样的事情,我希望我能引起其中一些人的兴趣。感谢您的任何帮助。

(Mac Snow Leopard 10.6 64 位上的 Eclipse 3.6)

PS,我还无法发布超过 1 个超链接,因此我包含了这些地址以显示我的问题的更多信息。

(1):stackoverflow.com/questions/4783160/embed-login-details-in-apk-file- Different-for-each-user-or-other-

options (2):developer.android.com/guide/developmenting /tools/aapt.html

(3): www.disgruntledrats.com/?p=27

Update: This is an old post, and references below to broken aapt versions will be out of date.


Based on previous feedback, I am storing custom text fields in the assets directory of my app.

I will write the app, using default user details in an asset file, and the client would like to rebuild the app for each user, including that user's details in the asset file.

(I am aware this is method has some serious shortcomings, but the client is still keen to do it in this way - see Embed login details in APK file, different for each user (or other options?))

This question relates to troubles I am having with rebuilding the APK file, once I have unzipped it, and updated the custom asset file. I am quite convinced I am missing something small, however, the documentation and posts I have found on these methods are not helpful enough for a newcomer.

  1. aapt - Android sdk tool: Unfortunately the android docs on "Using aapt" on the android docs [link 2 below] are very limited. The console command -help shows a bit more info. When trying to use it to just add a file to the test.apk, it ends up deleting the original, and creating a new file test.apk.zip containing only the file I tried to add. I have not been able to find the correct command line combination to take an unzipped apk and repackage it - that would be my first prize.

  2. apkbuilder - Android sdk tool: Firstly this tool is deprecated, which is a negative point for it. I also can't get it to work with what I have in that unzipped folder. I think I'm missing a pre-apkbuilder step because apkbuilder asks for a resource zip archive, and I have a resource folder.

  3. ant - build tool: Other similar posts say to build with ant, rather than using the android tools. I am having trouble getting ant to work. One particular link to Getting Ant to Work with Android [link 3 below] looks promising but looks like it is for a different android sdk (my build.xml that is generated by android looks different to his). Unfortunately I know little about ant, and am having trouble becoming expert enough to solve my current issue.
    Further to NickT's solution below - running the ant script gives me the error
    taskdef class com.android.ant.SetupTask cannot be found using the classloader AntClassLoader[].
    I have found some references online to this error, have confirmed that local.properties has an sdk.dir setting that is pointing to my android sdk install folder (sdk.dir=/Applications/android-sdk-mac_86).

  4. ?????: There might be some other option that I have not listed / discovered, which I would be interested in hearing about.

I realize that delving into the gears that are normally covered up by my ide can lead to diffuculties. But I know that a lot of the SO users can do many of these things, and I hope I get the interest of some of them. Thanks for any help.

(Eclipse 3.6 on Mac Snow Leopard 10.6 64 bit)

PS, I am not able to post more than 1 hyperlink yet, so I have included these addresses to show more info to my question.

(1): stackoverflow.com/questions/4783160/embed-login-details-in-apk-file-different-for-each-user-or-other-options

(2): developer.android.com/guide/developing/tools/aapt.html

(3): www.disgruntledrats.com/?p=27

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

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

发布评论

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

评论(4

廻憶裏菂餘溫 2024-10-21 18:27:43

用我们最终选择的方法回答我自己的问题,并“记录在案”:

我无法让 Ant 为我正常工作(我缺乏理解)。

我最终使用了 aapt 工具。它按照 Android sdk 文档工作,并且使用起来非常简单:

aapt add -v Test.apk "assets/readme.txt"

这将添加一个 readme.txt 文件(我们将其存储在当前运行文件夹的子文件夹中,称为 asset)。如果您想将新文件存储在某个地方否则,aapt 确实提供了一个命令行开关来指定)

但是,有一些警告:

  1. 我无法让它在签名的 APK 文件上工作。
  2. 某些版本的 aapt 无法正常工作!他们无法识别子文件夹...

要解决 1:

  1. 从 eclipse 导出 APK 作为未签名的 APK。
  2. 使用 keytool 生成密钥(请参阅 Android 开发文档)。
  3. 使用aapt。
  4. 使用 jarsigner 对 APK 进行签名。

解决2:

我真的帮不上忙。我尝试下载的每个版本的 SDK(在 Mac 上)都包含一个无法识别子文件夹的 aapt 工具。一位同事下载了一个有效的版本,但我们仍然无法弄清楚他下载的是哪个版本,因此我们保留了这个“神奇”副本并将其重命名,并将一直使用它,直到找到有效的新版本。

To answer my own question with the method we finally chose, and "for the record":

I was unable to get Ant working correctly for me (my lack of understanding).

I ended up using the aapt tool. It works as per the Android sdk documentation and really is quite simple to use:

aapt add -v Test.apk "assets/readme.txt"

This will add a readme.txt file (we were storing it in a subfolder of the current running folder, called assets. If you want to store the new file somewhere else, aapt does offer a command line switch to specify that)

However, there are some caveats:

  1. I couldn't get it to work on signed APK files.
  2. some versions of aapt don't work properly! They don't recognise the subfolder...

To solve 1:

  1. Export the APK from eclipse as an unsigned APK.
  2. Use the keytool to generate a key (see Android dev docs).
  3. Use aapt.
  4. Sign the APK using jarsigner.

To solve 2:

I can't really help with this. Every version of the SDK (on Mac) I tried to download included an aapt tool that did not recognize the subfolders. A colleague downloaded a version that worked, but we could still not figure out which version it was that he downloaded, so we kept this "magic" copy and renamed it and will be using it until we find a new version that works.

彡翼 2024-10-21 18:27:43

最简单的解决方案是使用从头开始重建应用程序的脚本将应用程序的模板源提供给您的客户。

这样,您的客户只需更改文件夹中的资源,双击批处理文件,然后在一分钟左右即可获取 apk。

您无需了解任何有关 ANT 的知识,即可在没有 IDE 的情况下进行编译和打包,只需按照此处的步骤操作即可:

http://developer.android.com/guide/developing/other-ide.html

ANT 脚本由“android create”“android update”命令生成。然后你只需要运行“ant release”就可以了。

The simplest solution would be to give the template source of the app to your client with a script that rebuild the app from scratch.

This way your client simply have to change the assets in the folder, double click a batch file and get the apk in a minute or so.

You don't have to know anything about ANT to compile and package without the IDE, just follow the steps here :

http://developer.android.com/guide/developing/other-ide.html

The ANT script is generated by the "android create" "android update" commands. You then just need to run "ant release" and off you go.

新一帅帅 2024-10-21 18:27:43

可能有点晚了,但这篇文章出现在很多搜索中,所以我认为这可能很有趣。

我只是使用标准 Zip 工具通过删除 META-INF 文件夹来“取消签名”APK,更新所有资产文件,然后使用 jarsigner 重新签署 APK。 (如果我能找到一种不使用 jarsigner 来签署 JAR 的方法,那么我们就不需要到处安装 Java SDK,这将非常有用 - 有人有想法吗?)

A bit late, probably, but this post appears in lots of searches, so I thought this may be of interest.

I just used a standard Zip tool to 'unsign' the APK by removing the META-INF folder, updated any asset files, and then resigned the APK using jarsigner. (If I could find a way to sign JARs without using jarsigner, so we wouldn't need the Java SDK installed everywhere, this would be really useful - ideas anyone?)

执笔绘流年 2024-10-21 18:27:43

为了扩展 Yahel 的答案,是的,我认为 Ant 是正确的选择。它可能有点吓人,但令人惊讶的是它“开箱即用”的工作效果,没有太多的混乱。定制它需要一些阅读,但我成功了,所以我将让您受益于我的经验。

首先按照建议创建一个示例应用程序。假设您希望在c:\junk 中创建它。
然后从 Android SDK\tools 目录中的命令提示符处输入:(

android create project -t 5 -n AndSampleApp -p c:\junk\AndSampleApp -a AndSampleApp -k com.yourpackage.andsample.

-t 5 为您提供 API 级别 7,输入“android list Targets”以查看其他选项)

让我们假设您已经在 Eclipse 工作区中构建项目(它是我的机器上的 /dev/projects/EclipseHelios/AndroidWorkspace )。将示例应用程序的 build.xml 和 build.properties 复制到 Eclipse 中的项目文件夹

,然后编辑新创建的 build.xml 并删除该行

<project name="AndSampleApp" default="help">

并将其替换为:

<project>

现在编辑新创建的 build.properties,该属性将为空,除了注释并添加(必要时进行自定义):

# The password will be asked during the build when you use the 'release' target.
# OH NO IT WON'T! IF YOU PUT THE KEYS & PWDS IN AS BELOW - It'S AUTOMATIC

# Customised (hard coded) section ---------------------

projectname=YourActualProjectName

# Line below is the parent folder of where the sources are
workspace.dir=/dev/projects/EclipseHelios/AndroidWorkspace

# Line below is where you want the binaries to go
outbasebase.dir=/dev/projects/AntBuilds

key.store=c:/users/you/your-release-key.keystore
key.alias=your_release_alias
key.store.password=YourSecretPassword
key.alias.password=YourSecretPassword
#------------------------------------------------------

ant.project.name=${projectname}
base.dir=${workspace.dir}/${projectname}
source.dir=${base.dir}/src
outbase.dir=${outbasebase.dir}/${projectname}
out.dir=${outbase.dir}/bin
layout.dir=${base.dir}/res/layout

如果您随后在 /dev/projects/EclipseHelios/AndroidWorkspace/YourActualProjectName 中收到命令行提示

并输入“ant release”,那么您最终应该在 /dev/projects 中得到一个构建并签名的 apk /AntBuilds/YourActualProjectName

您应该能够通过在 build.properties 中添加 assets.dir 条目来自定义资产位置

To expand on Yahel's answer, yes I think Ant is the way to go. It can be a bit intimidating, but it's urprising how well it works 'out of the box' without too much messing about. Customising it takes a bit of reading but I managed it, so I'll give you the benefit of my experience.

Firstly create a sample application as suggested. Let's suppose you want it to be created in c:\junk.
Then from your a command prompt in Android SDK\tools directory type:

android create project -t 5 -n AndSampleApp -p c:\junk\AndSampleApp -a AndSampleApp -k com.yourpackage.andsample.

( -t 5 gives you API level 7, type 'android list targets' to see other options)

Let's assume you've been building your projects in an Eclipse workspace (it's /dev/projects/EclipseHelios/AndroidWorkspace on my machine). Copy the sample app's build.xml and build.properties to the project folder in Eclipse

and edit the newly created build.xml and remove the line

<project name="AndSampleApp" default="help">

and replace it with just:

<project>

Now edit the the newly created build.properties which will be empty except for comments and add (customisng where necessary):

# The password will be asked during the build when you use the 'release' target.
# OH NO IT WON'T! IF YOU PUT THE KEYS & PWDS IN AS BELOW - It'S AUTOMATIC

# Customised (hard coded) section ---------------------

projectname=YourActualProjectName

# Line below is the parent folder of where the sources are
workspace.dir=/dev/projects/EclipseHelios/AndroidWorkspace

# Line below is where you want the binaries to go
outbasebase.dir=/dev/projects/AntBuilds

key.store=c:/users/you/your-release-key.keystore
key.alias=your_release_alias
key.store.password=YourSecretPassword
key.alias.password=YourSecretPassword
#------------------------------------------------------

ant.project.name=${projectname}
base.dir=${workspace.dir}/${projectname}
source.dir=${base.dir}/src
outbase.dir=${outbasebase.dir}/${projectname}
out.dir=${outbase.dir}/bin
layout.dir=${base.dir}/res/layout

If you then get a command line prompt in /dev/projects/EclipseHelios/AndroidWorkspace/YourActualProjectName

and type 'ant release', you should end up with a built and signed apk in /dev/projects/AntBuilds/YourActualProjectName

You should be able to customise the assets location by the adding an assets.dir entry in the build.properties

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