1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ant; 18 19 import com.android.sdklib.internal.build.BuildConfigGenerator; 20 21 import org.apache.tools.ant.BuildException; 22 import org.apache.tools.ant.types.Path; 23 24 import java.io.File; 25 import java.io.IOException; 26 27 public class BuildConfigTask extends BuildTypedTask { 28 29 private String mGenFolder; 30 private String mAppPackage; 31 setGenFolder(Path path)32 public void setGenFolder(Path path) { 33 mGenFolder = TaskHelper.checkSinglePath("genFolder", path); 34 } 35 setPackage(String appPackage)36 public void setPackage(String appPackage) { 37 mAppPackage = appPackage; 38 } 39 40 41 @Override execute()42 public void execute() throws BuildException { 43 if (mGenFolder == null) { 44 throw new BuildException("Missing attribute genFolder"); 45 } 46 if (mAppPackage == null) { 47 throw new BuildException("Missing attribute package"); 48 } 49 50 BuildConfigGenerator generator = new BuildConfigGenerator( 51 mGenFolder, mAppPackage, 52 Boolean.parseBoolean(getBuildType())); 53 54 // first check if the file is missing. 55 File buildConfigFile = generator.getBuildConfigFile(); 56 boolean missingFile = buildConfigFile.exists() == false; 57 58 if (missingFile || hasBuildTypeChanged()) { 59 if (isNewBuild()) { 60 System.out.println("Generating BuildConfig class."); 61 } else if (missingFile) { 62 System.out.println("BuildConfig class missing: Generating new BuildConfig class."); 63 } else { 64 System.out.println("Build type changed: Generating new BuildConfig class."); 65 } 66 67 try { 68 generator.generate(); 69 } catch (IOException e) { 70 throw new BuildException("Failed to create BuildConfig class", e); 71 } 72 } else { 73 System.out.println("No need to generate new BuildConfig."); 74 } 75 } 76 } 77