FileDocCategorySizeDatePackage
ApkBuilderTask.javaAPI DocAndroid 1.5 API10450Wed May 06 22:41:08 BST 2009com.android.ant

ApkBuilderTask

public class ApkBuilderTask extends org.apache.tools.ant.Task

Fields Summary
private String
mOutFolder
private String
mBaseName
private boolean
mVerbose
private boolean
mSigned
private final ArrayList
mZipList
private final ArrayList
mFileList
private final ArrayList
mSourceList
private final ArrayList
mJarList
private final ArrayList
mNativeList
private final ArrayList
mZipArchives
private final ArrayList
mArchiveFiles
private final ArrayList
mJavaResources
private final ArrayList
mResourcesJars
private final ArrayList
mNativeLibraries
Constructors Summary
Methods Summary
private voidcreateApk(com.android.apkbuilder.ApkBuilder apkBuilder, java.lang.String configName, java.lang.String resourceFilter)
Creates an application package.

param
apkBuilder
param
configName the name of the filter config. Can be null in which case a full resource package will be generated.
param
resourceFilter the resource configuration filter to pass to aapt (if configName is non null)
throws
FileNotFoundException

        // All the files to be included in the archive have already been prep'ed up, except
        // the resource package.
        // figure out its name.
        String filename;
        if (configName != null && resourceFilter != null) {
            filename = mBaseName + "-" + configName + ".ap_";
        } else {
            filename = mBaseName + ".ap_";
        }
        
        // now we add it to the list of zip archive (it's just a zip file).
        
        // it's used as a zip archive input
        FileInputStream resoucePackageZipFile = new FileInputStream(new File(mOutFolder, filename));
        mZipArchives.add(resoucePackageZipFile);
        
        // prepare the filename to generate. Same thing as the resource file.
        if (configName != null && resourceFilter != null) {
            filename = mBaseName + "-" + configName;
        } else {
            filename = mBaseName;
        }
        
        if (mSigned) {
            filename = filename + "-debug.apk";
        } else {
            filename = filename + "-unsigned.apk";
        }

        if (configName == null || resourceFilter == null) {
            if (mSigned) {
                System.out.println(String.format(
                        "Creating %s and signing it with a debug key...", filename));
            } else {
                System.out.println(String.format(
                        "Creating %s for release...", filename));
            }
        } else {
            if (mSigned) {
                System.out.println(String.format(
                        "Creating %1$s (with %2$s) and signing it with a debug key...",
                        filename, resourceFilter));
            } else {
                System.out.println(String.format(
                        "Creating %1$s (with %2$s) for release...",
                        filename, resourceFilter));
            }
        }
        
        File f = new File(mOutFolder, filename);
        
        // and generate the apk
        apkBuilder.createPackage(f.getAbsoluteFile(), mZipArchives,
                mArchiveFiles, mJavaResources, mResourcesJars, mNativeLibraries);
        
        // we are done. We need to remove the resource package from the list of zip archives
        // in case we have another apk to generate.
        mZipArchives.remove(resoucePackageZipFile);
    
public java.lang.ObjectcreateFile()
Returns an object representing a nested file element.

        Value file = new Value();
        mFileList.add(file);
        return file;
    
public java.lang.ObjectcreateJarfolder()
Returns an object representing a nested jarfolder element.

        Value file = new Value();
        mJarList.add(file);
        return file;
    
public java.lang.ObjectcreateNativefolder()
Returns an object representing a nested nativefolder element.

        Value file = new Value();
        mNativeList.add(file);
        return file;
    
public java.lang.ObjectcreateSourcefolder()
Returns an object representing a nested sourcefolder element.

        Value file = new Value();
        mSourceList.add(file);
        return file;
    
public java.lang.ObjectcreateZip()
Returns an object representing a nested zip element.

        Value zip = new Value();
        mZipList.add(zip);
        return zip;
    
public voidexecute()

        Project taskProject = getProject();
        
        ApkBuilder apkBuilder = new ApkBuilder();
        apkBuilder.setVerbose(mVerbose);
        apkBuilder.setSignedPackage(mSigned);
        
        try {
            // setup the list of everything that needs to go in the archive.
            
            // go through the list of zip files to add. This will not include
            // the resource package, which is handled separaly for each apk to create.
            for (Value v : mZipList) {
                FileInputStream input = new FileInputStream(v.mPath);
                mZipArchives.add(input);
            }
            
            // now go through the list of file to directly add the to the list.
            for (Value v : mFileList) {
                mArchiveFiles.add(ApkBuilder.getInputFile(v.mPath));
            }
            
            // now go through the list of file to directly add the to the list.
            for (Value v : mSourceList) {
                ApkBuilder.processSourceFolderForResource(v.mPath, mJavaResources);
            }
            
            // now go through the list of jar folders.
            for (Value v : mJarList) {
                ApkBuilder.processJarFolder(v.mPath, mResourcesJars);
            }
            
            // now the native lib folder.
            for (Value v : mNativeList) {
                String parameter = v.mPath;
                File f = new File(parameter);
                
                // compute the offset to get the relative path
                int offset = parameter.length();
                if (parameter.endsWith(File.separator) == false) {
                    offset++;
                }

                ApkBuilder.processNativeFolder(offset, f, mNativeLibraries);
            }

            
            // first do a full resource package
            createApk(apkBuilder, null /*configName*/, null /*resourceFilter*/);
    
            // now see if we need to create file with filtered resources.
            // Get the project base directory.
            File baseDir = taskProject.getBaseDir();
            ProjectProperties properties = ProjectProperties.load(baseDir.getAbsolutePath(),
                    PropertyType.DEFAULT);
            
            Map<String, String> apkConfigs = ApkConfigurationHelper.getConfigs(properties);
            if (apkConfigs.size() > 0) {
                Set<Entry<String, String>> entrySet = apkConfigs.entrySet();
                for (Entry<String, String> entry : entrySet) {
                    createApk(apkBuilder, entry.getKey(), entry.getValue());
                }
            }
        } catch (FileNotFoundException e) {
            throw new BuildException(e);
        } catch (IllegalArgumentException e) {
            throw new BuildException(e);
        }
    
public voidsetBasename(java.lang.String baseName)
Sets the value of the "basename" attribute.

param
baseName the value.

        mBaseName = baseName;
    
public voidsetOutfolder(org.apache.tools.ant.types.Path outFolder)
Sets the value of the "outfolder" attribute.

param
outFolder the value.


                    
        
        mOutFolder = outFolder.toString();
    
public voidsetSigned(boolean signed)
Sets the value of the "signed" attribute.

param
signed the value.

        mSigned = signed;
    
public voidsetVerbose(boolean verbose)
Sets the value of the "verbose" attribute.

param
verbose the value.

        mVerbose = verbose;