1 /* 2 * Copyright (C) 2012 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 org.apache.tools.ant.BuildException; 20 import org.apache.tools.ant.Task; 21 22 /** 23 * Task building an emma filter to remove all build-only classes. 24 * 25 * Currently ignore: 26 * app.package.R 27 * app.package.R$* 28 * app.package.Manifest 29 * app.package.BuildConfig 30 * 31 */ 32 public class GetEmmaFilterTask extends Task { 33 34 private static final String[] FILTER_CLASSES = new String[] { 35 "R", "R$*", "Manifest", "BuildConfig" 36 }; 37 38 private String mAppPackage; 39 private String mLibraryPackagesRefId; 40 private String mFilterOut; 41 42 43 public void setAppPackage(String appPackage) { 44 mAppPackage = appPackage; 45 } 46 47 public void setLibraryPackagesRefId(String libraryPackagesRefId) { 48 mLibraryPackagesRefId = libraryPackagesRefId; 49 } 50 51 public void setFilterOut(String filterOut) { 52 mFilterOut = filterOut; 53 } 54 55 @Override 56 public void execute() throws BuildException { 57 if (mAppPackage == null) { 58 throw new BuildException("Missing attribute appPackage"); 59 } 60 if (mLibraryPackagesRefId == null) { 61 throw new BuildException("Missing attribute libraryPackagesRefId"); 62 } 63 if (mFilterOut == null) { 64 throw new BuildException("Missing attribute filterOut"); 65 } 66 67 StringBuilder sb = new StringBuilder(); 68 69 String libraryPackagesValue = getProject().getProperty(mLibraryPackagesRefId); 70 71 if (libraryPackagesValue != null && libraryPackagesValue.length() > 0) { 72 // split the app packages. 73 String[] libPackages = libraryPackagesValue.split(";"); 74 75 for (String libPackage : libPackages) { 76 if (libPackage.length() > 0) { 77 for (String filterClass : FILTER_CLASSES) { 78 sb.append(libPackage).append('.').append(filterClass).append(','); 79 } 80 } 81 } 82 } 83 84 // add the app package: 85 final int count = FILTER_CLASSES.length; 86 for (int i = 0 ; i < count ; i++) { 87 sb.append(mAppPackage).append('.').append(FILTER_CLASSES[i]); 88 if (i < count - 1) { 89 sb.append(','); 90 } 91 } 92 93 getProject().setProperty(mFilterOut, sb.toString()); 94 } 95 } 96