[HOWTO][LINUX] Compile 9.png and xml files on the command-line without Eclipse

Search This thread

kellinwood

Retired Recognized Developer
Aug 17, 2010
226
136
Kailua, HI
Developed and tested on Linux. Its a shell script that requires the Android SDK to be installed with the tools directory in the PATH variable.

INTRO

Android Binary Resource Compiler (abrc). This is a command-line
script to compile resources such as NinePatch PNGs and layout XML
files to Android binary format. The compiled resources named in the
command are extracted and saved out into a separate location. This
script was developed as an alternative to using Eclipse and "unzip" to
compile and extract NinePatch PNGs during the creation of custom
Android UI themes.

USAGE
abrc usage
abrc help
abrc [options] compile <project dir> <output dir> <resource file1> <resource file2> ...
abrc [options] setup <project dir>

OPTIONS:

-v Enable verbose output
-o Overwrite files without prompting


COMPILE

The compile operation compiles the resources in the <project dir>
location. This can be any Android Eclipse project directory which
contains the resources you wish to compile and extract. You can also
use this script's "setup" syntax to create a minimal project without
using Eclipse. See the SETUP section below for more information.

The named resource files <resource file1> <resource file2>, etc., are
then extracted into the <output directory>. Parent path components in
the resource file names are preserved and also determine the extracted
location within the output directory.

Example:

Consider the following path in "MyProject" which contains, among other
things, the statusbar_background.9.png file. This file is in the format
created by draw9patch program.

MyProject/res/drawable-mdpi/statusbar_background.9.png

The following command compiles the resources in MyProject and writes
the output to the MyTheme/framework-res directory using the leading
path components of the specified resource file(s). I.e., In this
example the resulting output file is
MyTheme/framework-res/res/drawable-mdpi/statusbar_background.9.png

abrc compile MyProject MyTheme/framework-res res/drawable-mdpi/statusbar_background.9.png


SETUP


The setup operation is completely optional. It creates a minimal
project skeleton for compiling resources. Eclipse projects can also
be used in the compile step above.

abrc setup MyProject

The above command creates the following directory structure/files:

MyProject/res/drawable
MyProject/res/drawable-mdpi
MyProject/res/drawable-hdpi
MyProject/res/layout
MyProject/AndroidManifest.xml

Copy your resources into the appropriate location within the project
and then use the compile operation to compile and extract the binary
files.
 

Attachments

  • abrc.zip
    2.3 KB · Views: 1,982
Last edited:

kellinwood

Retired Recognized Developer
Aug 17, 2010
226
136
Kailua, HI
Yes, that's what it does. It's just a shell script so you can look at it and see. There are some comments in the script that explains things.

It relies on the Android SDK which is assumed to already be installed.

Sent from my FroyoEris using XDA App
 

bliind

Senior Member
Jan 28, 2009
487
752
Hamden, CT
This is a great tool. It was a little confusing at first, I don't think I had my path set up correctly. I've found the easiest way to use this, for me anyway, was to use the setup command and make a new folder with it, I deleted the m and ldpi folders because i'm not using them, copied my .9s into the drawable-hdpi folder, and from inside the "project" directory ran,
abrc compile . done res/drawable-hdpi/*
This compiles all the images in there and places them in the done folder.
Hope i'm not stepping on any toes, just what I found to be the easiest after placing aapt in /usr/tools/ and android.jar in /usr/ as I was kinda confused when I first downloaded it.
 
  • Like
Reactions: arbayong

Splder

Senior Member
Jun 26, 2009
1,367
1,509
Istanbul
so im trying to get this to work but no luck if someone could help me

first i set paths
Code:
export PATH=${PATH}:/home/suphi/Documents/android-sdk-linux/tools:/home/suphi/Documents/android-sdk-linux/platform-tools:/home/suphi/Documents/android-sdk-linux/apktool

then i run this code

Code:
abrc compile before after res/drawable-hdpi/*

then i get this error

Code:
Using aapt to compile resources to /tmp/resources-82KEfVduDK.zip
W/asset   ( 4429): Asset path /home/suphi/Documents/android-sdk-linux/android.jar is neither a directory nor file (type=1).
ERROR: Asset package include '/home/suphi/Documents/android-sdk-linux/android.jar' not found.

i also put the aapt from platform-tools into tools folder as i was getting errors before saying aapt was missing from tools folder
 

kellinwood

Retired Recognized Developer
Aug 17, 2010
226
136
Kailua, HI
so im trying to get this to work but no luck if someone could help me

<snip>

i also put the aapt from platform-tools into tools folder as i was getting errors before saying aapt was missing from tools folder

There are several 'tools' folders. It belongs in the platforms/android-N/tools folder where the 'N' in android-N is a number (API level). Making copies might also cause problems, so I would try and figure out why its missing from the tools directory in the first place.

Ken
 

figofuture

Member
Apr 5, 2010
14
7
no need to use abrc to prepare compiled 9patch pngs, just using aapt in android-sdk is ok, the command line is like this: aapt.exe c -v -S /path/to/project -C /path/to/destination
 

Kdio

Senior Member
Nov 12, 2009
2,165
3,094
Campos dos Goytacazes
Hi @kellinwood and ALL

Had being advised by your awesome work by @Modding.MyMind here, I've came to ask for your orientation.

At the moment, AFAIK, we are uncapable to compile xml's without providing all the resources addressed by them.

My objective here is try to compile modded XMLs only. While trying to accomplish this task, I've got to the following Python script that will generate all the /res/values/*.xml's that addresses local resources.

Code:
import os
import shutil

input_folder = '/home/Kdio/apk/Phone'

output_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), "out")
if not os.path.isdir(output_folder):
    os.mkdir(output_folder)

tokens = {}
xmlExist = {}

for root, dirs, files in os.walk(input_folder):
    for f in files:
        inputFile = os.path.join(root, f)
        filename, ext = os.path.splitext(inputFile)
        
        if (ext != '.xml'):
            continue
        
        xmlExist[os.path.basename(inputFile)] = 1
        
        with open(inputFile, 'r') as f:
            for line in f:
                token = ''
                read_token = False
                for c in line:
                    if (c == '@'):
                        read_token = True
                        continue
                    
                    if (read_token is True):
                        if (c == '"'):
                            read_token = False
                            tokens[token] = 1
                            token = ''
                        else:
                            token += c
        f.close()

xmls = {}
for token in sorted(tokens):
    if (token == 'null'):
        continue
    if (':' in token):
        continue
    #token = token.split(':')[-1]
    head, tail = token.split('/')
    if (head == 'anim'):
        continue

    if ((tail + '.xml') in xmlExist.keys()):
        continue

    if (not head in xmls.keys()):
        xmls[head] = {}
    xmls[head][tail] = 1

#for xml in xmls:
#    for res in xmls[xml]:
#        print(xml + ' -> ' + res)

for xml in xmls:

    xmlText = '<?xml version="1.0" encoding="utf-8"?>\n<resources>\n'

    for res in xmls[xml]:
        if (xml == 'bool'):
            xmlText += '    <bool name="' + res + '">false</bool>\n'

        if ((xml == 'id') or (xml == 'drawable') or (xml == 'style') or (xml == 'layout')):
            xmlText += '    <item type="' + xml + '" name="' + res + '">false</item>\n'

        if (xml == 'array'):
            xmlText += '    <string-array name="' + res + '">\n        <item>array-dummy</item>\n    </string-array>\n'

        if (xml == 'attr'):
            xmlText += '    <attr name="' + res + '" format="string" />\n'

        if (xml == 'color'):
            xmlText += '    <color name="'+ res + '">#00000000</color>\n'

        if (xml == 'dimen'):
            xmlText += '    <dimen name="' + res + '">1.0dip</dimen>\n'

        if (xml == 'integer'):
            xmlText += '    <integer name="' + res + '">1</integer>\n'

        if (xml == 'plural'):
            xmlText += '    <plurals name="' + res + '">\n        <item quantity="other">a</item>\n        <item quantity="one">b</item>\n    </plurals>\n'

        if (xml == 'string'):
            xmlText += '    <string name="' + res + '">a</string>\n'

        if (xml == 'fraction'):
            xmlText += '    <fraction name="' + res + '">1%</fraction>\n'

    xmlText += '</resources>\n'

    f = open(os.path.join(output_folder, xml + 's.xml'), 'w')
    f.write(xmlText)
    f.close

Please edit 'input_folder' accordingly.

Then, issuing the below command:

Code:
aapt p -f -M onlyXMLs/AndroidManifest.xml -F onlyXMLs.zip -S onlyXMLs/res -I android.jar

What I get from it now are errors regarding external resources references not being found only.

This is exactly the point where my shallow knowledge ends ... :rolleyes:

Could you (and all) someway point me the light (if there is any) how could we 'dummy' external resources as done with local ones?

Thank you all.

Nice regards.

.
 

Top Liked Posts

  • There are no posts matching your filters.
  • 7
    Developed and tested on Linux. Its a shell script that requires the Android SDK to be installed with the tools directory in the PATH variable.

    INTRO

    Android Binary Resource Compiler (abrc). This is a command-line
    script to compile resources such as NinePatch PNGs and layout XML
    files to Android binary format. The compiled resources named in the
    command are extracted and saved out into a separate location. This
    script was developed as an alternative to using Eclipse and "unzip" to
    compile and extract NinePatch PNGs during the creation of custom
    Android UI themes.

    USAGE
    abrc usage
    abrc help
    abrc [options] compile <project dir> <output dir> <resource file1> <resource file2> ...
    abrc [options] setup <project dir>

    OPTIONS:

    -v Enable verbose output
    -o Overwrite files without prompting


    COMPILE

    The compile operation compiles the resources in the <project dir>
    location. This can be any Android Eclipse project directory which
    contains the resources you wish to compile and extract. You can also
    use this script's "setup" syntax to create a minimal project without
    using Eclipse. See the SETUP section below for more information.

    The named resource files <resource file1> <resource file2>, etc., are
    then extracted into the <output directory>. Parent path components in
    the resource file names are preserved and also determine the extracted
    location within the output directory.

    Example:

    Consider the following path in "MyProject" which contains, among other
    things, the statusbar_background.9.png file. This file is in the format
    created by draw9patch program.

    MyProject/res/drawable-mdpi/statusbar_background.9.png

    The following command compiles the resources in MyProject and writes
    the output to the MyTheme/framework-res directory using the leading
    path components of the specified resource file(s). I.e., In this
    example the resulting output file is
    MyTheme/framework-res/res/drawable-mdpi/statusbar_background.9.png

    abrc compile MyProject MyTheme/framework-res res/drawable-mdpi/statusbar_background.9.png


    SETUP


    The setup operation is completely optional. It creates a minimal
    project skeleton for compiling resources. Eclipse projects can also
    be used in the compile step above.

    abrc setup MyProject

    The above command creates the following directory structure/files:

    MyProject/res/drawable
    MyProject/res/drawable-mdpi
    MyProject/res/drawable-hdpi
    MyProject/res/layout
    MyProject/AndroidManifest.xml

    Copy your resources into the appropriate location within the project
    and then use the compile operation to compile and extract the binary
    files.
    1
    How does it work? It calls aapt and then unzip compiled resources? It's too small to do this directly.
    1
    This is a great tool. It was a little confusing at first, I don't think I had my path set up correctly. I've found the easiest way to use this, for me anyway, was to use the setup command and make a new folder with it, I deleted the m and ldpi folders because i'm not using them, copied my .9s into the drawable-hdpi folder, and from inside the "project" directory ran,
    abrc compile . done res/drawable-hdpi/*
    This compiles all the images in there and places them in the done folder.
    Hope i'm not stepping on any toes, just what I found to be the easiest after placing aapt in /usr/tools/ and android.jar in /usr/ as I was kinda confused when I first downloaded it.