aboutsummaryrefslogtreecommitdiffstats
path: root/sdkmanager/libs/sdklib
diff options
context:
space:
mode:
authorXavier Ducrohet <xav@android.com>2011-10-14 11:13:58 -0700
committerXavier Ducrohet <xav@android.com>2011-10-14 15:09:08 -0700
commite162064a7b5db1eecec34271bc7e2a4296181ea6 (patch)
tree24e0ac58aca71ad112e018a9c2b435e3a0fbb395 /sdkmanager/libs/sdklib
parent081eec9d6c307290535413033626c05cfc67635d (diff)
downloadsdk-e162064a7b5db1eecec34271bc7e2a4296181ea6.zip
sdk-e162064a7b5db1eecec34271bc7e2a4296181ea6.tar.gz
sdk-e162064a7b5db1eecec34271bc7e2a4296181ea6.tar.bz2
Add BuildConfig to ant SDK project.
The BuildConfig class contains a single boolean constant called DEBUG which is true only for debug/instrumented builds. This allows developers to create debug only code that's automatically stripped from release builds. Also fixed some issues with dependency check for other tasks, notably the aapt task that would always find new files due to not filtering out files that are ignored by aapt itself (hidden files, version control files, etc...) Change-Id: I4391a87c064a185d6b337ca46e3a9f0e43c5174d
Diffstat (limited to 'sdkmanager/libs/sdklib')
-rw-r--r--sdkmanager/libs/sdklib/src/com/android/sdklib/build/ApkBuilder.java2
-rw-r--r--sdkmanager/libs/sdklib/src/com/android/sdklib/internal/build/BuildConfig.template6
-rw-r--r--sdkmanager/libs/sdklib/src/com/android/sdklib/internal/build/BuildConfigGenerator.java138
3 files changed, 146 insertions, 0 deletions
diff --git a/sdkmanager/libs/sdklib/src/com/android/sdklib/build/ApkBuilder.java b/sdkmanager/libs/sdklib/src/com/android/sdklib/build/ApkBuilder.java
index a475e1b..774d9f4 100644
--- a/sdkmanager/libs/sdklib/src/com/android/sdklib/build/ApkBuilder.java
+++ b/sdkmanager/libs/sdklib/src/com/android/sdklib/build/ApkBuilder.java
@@ -949,6 +949,8 @@ public final class ApkBuilder implements IArchiveBuilder {
"class".equalsIgnoreCase(extension) == false && // Java class files
"scc".equalsIgnoreCase(extension) == false && // VisualSourceSafe
"swp".equalsIgnoreCase(extension) == false && // vi swap file
+ "thumbs.db".equalsIgnoreCase(fileName) == false && // image index file
+ "picasa.ini".equalsIgnoreCase(fileName) == false && // image index file
"package.html".equalsIgnoreCase(fileName) == false && // Javadoc
"overview.html".equalsIgnoreCase(fileName) == false; // Javadoc
}
diff --git a/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/build/BuildConfig.template b/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/build/BuildConfig.template
new file mode 100644
index 0000000..0344b55
--- /dev/null
+++ b/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/build/BuildConfig.template
@@ -0,0 +1,6 @@
+/** Automatically generated file. DO NOT MODIFY */
+package #PACKAGE#;
+
+public final class BuildConfig {
+ public final static boolean DEBUG = #DEBUG#;
+} \ No newline at end of file
diff --git a/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/build/BuildConfigGenerator.java b/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/build/BuildConfigGenerator.java
new file mode 100644
index 0000000..fb84bfd
--- /dev/null
+++ b/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/build/BuildConfigGenerator.java
@@ -0,0 +1,138 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.sdklib.internal.build;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
+/**
+ * Class able to generate a BuildConfig class in Android project.
+ * The BuildConfig class contains constants related to the build target.
+ */
+public class BuildConfigGenerator {
+
+ private final static String PH_PACKAGE = "#PACKAGE#";
+ private final static String PH_DEBUG = "#DEBUG#";
+
+ private final String mGenFolder;
+ private final String mAppPackage;
+ private final boolean mDebug;
+
+ /**
+ * Creates a generator
+ * @param genFolder the gen folder of the project
+ * @param appPackage the application package
+ * @param debug whether it's a debug build
+ */
+ public BuildConfigGenerator(String genFolder, String appPackage, boolean debug) {
+ mGenFolder = genFolder;
+ mAppPackage = appPackage;
+ mDebug = debug;
+ }
+
+ /**
+ * Generates the BuildConfig class.
+ */
+ public void generate() throws IOException {
+ String template = readEmbeddedTextFile("BuildConfig.template");
+
+ Map<String, String> map = new HashMap<String, String>();
+ map.put(PH_PACKAGE, mAppPackage);
+ map.put(PH_DEBUG, Boolean.toString(mDebug));
+
+ String content = replaceParameters(template, map);
+
+ File genFolder = new File(mGenFolder);
+ File pkgFolder = new File(genFolder, mAppPackage.replaceAll("\\.", File.separator));
+ if (pkgFolder.isDirectory() == false) {
+ pkgFolder.mkdirs();
+ }
+
+ File buildConfigJava = new File(pkgFolder, "BuildConfig.java");
+ writeFile(buildConfigJava, content);
+ }
+
+ /**
+ * Reads and returns the content of a text file embedded in the jar file.
+ * @param filepath the file path to the text file
+ * @return null if the file could not be read
+ * @throws IOException
+ */
+ private String readEmbeddedTextFile(String filepath) throws IOException {
+ InputStream is = BuildConfigGenerator.class.getResourceAsStream(filepath);
+ if (is != null) {
+ BufferedReader reader = new BufferedReader(new InputStreamReader(is));
+
+ String line;
+ StringBuilder total = new StringBuilder(reader.readLine());
+ while ((line = reader.readLine()) != null) {
+ total.append('\n');
+ total.append(line);
+ }
+
+ return total.toString();
+ }
+
+ // this really shouldn't happen unless the sdklib packaging is broken.
+ throw new IOException("BuildConfig template is missing!");
+ }
+
+ private void writeFile(File file, String content) throws IOException {
+ FileOutputStream fos = null;
+ try {
+ fos = new FileOutputStream(file);
+ InputStream source = new ByteArrayInputStream(content.getBytes("UTF-8"));
+
+ byte[] buffer = new byte[1024];
+ int count = 0;
+ while ((count = source.read(buffer)) != -1) {
+ fos.write(buffer, 0, count);
+ }
+ } finally {
+ if (fos != null) {
+ fos.close();
+ }
+ }
+ }
+
+ /**
+ * Replaces placeholders found in a string with values.
+ *
+ * @param str the string to search for placeholders.
+ * @param parameters a map of <placeholder, Value> to search for in the string
+ * @return A new String object with the placeholder replaced by the values.
+ */
+ private String replaceParameters(String str, Map<String, String> parameters) {
+
+ for (Entry<String, String> entry : parameters.entrySet()) {
+ String value = entry.getValue();
+ if (value != null) {
+ str = str.replaceAll(entry.getKey(), value);
+ }
+ }
+
+ return str;
+ }
+}