Repackage APK file to contain custom assets - what build tool to use?

时光怂恿深爱的人放手 提交于 2019-11-27 08:28:49

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.

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.

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?)

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!