FileDocCategorySizeDatePackage
ProjectProperties.javaAPI DocAndroid 1.5 API10623Wed May 06 22:41:10 BST 2009com.android.sdklib.project

ProjectProperties

public final class ProjectProperties extends Object
Class to load and save project properties for both ADT and Ant-based build.

Fields Summary
public static final String
PROPERTY_TARGET
The property name for the project target
public static final String
PROPERTY_APK_CONFIGS
public static final String
PROPERTY_SDK
private static final String
LOCAL_HEADER
private static final String
DEFAULT_HEADER
private static final String
BUILD_HEADER
private static final Map
COMMENT_MAP
private final String
mProjectFolderOsPath
private final Map
mProperties
private final PropertyType
mType
Constructors Summary
private ProjectProperties(String projectFolderOsPath, Map map, PropertyType type)
Private constructor.

Use {@link #load(String, PropertyType)} or {@link #create(String, PropertyType)} to instantiate.

        mProjectFolderOsPath = projectFolderOsPath;
        mProperties = map;
        mType = type;
    
Methods Summary
public static com.android.sdklib.project.ProjectPropertiescreate(java.lang.String projectFolderOsPath, com.android.sdklib.project.ProjectProperties$PropertyType type)
Creates a new project properties object, with no properties.

The file is not created until {@link #save()} is called.

param
projectFolderOsPath the project folder.
param
type

        // create and return a ProjectProperties with an empty map.
        return new ProjectProperties(projectFolderOsPath, new HashMap<String, String>(), type);
    
public java.lang.StringgetProperty(java.lang.String name)
Returns the value of a property.

param
name the name of the property.
return
the property value or null if the property is not set.

        return mProperties.get(name);
    
public static com.android.sdklib.project.ProjectPropertiesload(java.lang.String projectFolderOsPath, com.android.sdklib.project.ProjectProperties$PropertyType type)
Loads a project properties file and return a {@link ProjectProperties} object containing the properties

param
projectFolderOsPath the project folder.
param
type One the possible {@link PropertyType}s.

        File projectFolder = new File(projectFolderOsPath);
        if (projectFolder.isDirectory()) {
            File defaultFile = new File(projectFolder, type.mFilename);
            if (defaultFile.isFile()) {
                Map<String, String> map = SdkManager.parsePropertyFile(defaultFile, null /* log */);
                if (map != null) {
                    return new ProjectProperties(projectFolderOsPath, map, type);
                }
            }
        }
        return null;
    
public com.android.sdklib.project.ProjectPropertiesmerge(com.android.sdklib.project.ProjectProperties$PropertyType type)
Merges all properties from the given file into the current properties.

This emulates the Ant behavior: existing properties are not overriden. Only new undefined properties become defined.

Typical usage:

  • Create a ProjectProperties with {@link PropertyType#BUILD}
  • Merge in values using {@link PropertyType#DEFAULT}
  • The result is that this contains all the properties from default plus those overridden by the build.properties file.

param
type One the possible {@link PropertyType}s.
return
this object, for chaining.

        File projectFolder = new File(mProjectFolderOsPath);
        if (projectFolder.isDirectory()) {
            File defaultFile = new File(projectFolder, type.mFilename);
            if (defaultFile.isFile()) {
                Map<String, String> map = SdkManager.parsePropertyFile(defaultFile, null /* log */);
                if (map != null) {
                    for(Entry<String, String> entry : map.entrySet()) {
                        String key = entry.getKey();
                        String value = entry.getValue();
                        if (!mProperties.containsKey(key) && value != null) {
                            mProperties.put(key, value);
                        }
                    }
                }
            }
        }
        return this;
    
public java.lang.StringremoveProperty(java.lang.String name)
Removes a property and returns its previous value (or null if the property did not exist).

param
name the name of the property to remove.

        return mProperties.remove(name);
    
public voidsave()
Saves the property file.

throws
IOException

        File toSave = new File(mProjectFolderOsPath, mType.mFilename);
        
        FileWriter writer = new FileWriter(toSave);
        
        // write the header
        writer.write(mType.mHeader);
        
        // write the properties.
        for (Entry<String, String> entry : mProperties.entrySet()) {
            String comment = COMMENT_MAP.get(entry.getKey());
            if (comment != null) {
                writer.write(comment);
            }
            String value = entry.getValue();
            value = value.replaceAll("\\\\", "\\\\\\\\");
            writer.write(String.format("%s=%s\n", entry.getKey(), value));
        }
        
        // close the file to flush
        writer.close();
    
public voidsetAndroidTarget(com.android.sdklib.IAndroidTarget target)
Sets the target property to the given {@link IAndroidTarget} object.

param
target the Android target.

        assert mType == PropertyType.DEFAULT;
        mProperties.put(PROPERTY_TARGET, target.hashString());
    
public voidsetProperty(java.lang.String name, java.lang.String value)
Sets a new properties. If a property with the same name already exists, it is replaced.

param
name the name of the property.
param
value the value of the property.

        mProperties.put(name, value);