1 /* 2 * Copyright (C) 2015 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.compatibility.common.generator; 18 19 import java.io.FileOutputStream; 20 import java.io.OutputStream; 21 import java.util.ArrayList; 22 import java.util.List; 23 24 import org.kxml2.io.KXmlSerializer; 25 26 public class ManifestGenerator { 27 28 private static final String DEFAULT_MIN_SDK = "8"; 29 30 private static final String USAGE = "Usage: " 31 + "manifest-generator -n NAME -p PACKAGE_NAME -o OUTPUT_FILE -i INSTRUMENT_NAME " 32 + "[-s MIN_SDK_VERSION] [-t TARGET_SDK_VERSION] [-r PERMISSION]+ [-a ACTIVITY]+"; 33 private static final String MANIFEST = "manifest"; 34 private static final String USES_SDK = "uses-sdk"; 35 private static final String USES_PERMISSION = "uses-permission"; 36 private static final String APPLICATION = "application"; 37 private static final String INSTRUMENTATION = "instrumentation"; 38 private static final String ACTIVITY = "activity"; 39 private static final String USES_LIBRARY = "uses-library"; 40 main(String[] args)41 public static void main(String[] args) { 42 String pkgName = null; 43 String instrumentName = null; 44 String minSdk = DEFAULT_MIN_SDK; 45 String targetSdk = null; 46 List<String> permissions = new ArrayList<>(); 47 List<String> activities = new ArrayList<>(); 48 List<String> libraries = new ArrayList<>(); 49 String output = null; 50 51 for (int i = 0; i < args.length - 1; i++) { 52 if (args[i].equals("-p")) { 53 pkgName = args[++i]; 54 } else if (args[i].equals("-a")) { 55 activities.add(args[++i]); 56 } else if (args[i].equals("-l")) { 57 libraries.add(args[++i]); 58 } else if (args[i].equals("-o")) { 59 output = args[++i]; 60 } else if (args[i].equals("-i")) { 61 instrumentName = args[++i]; 62 } else if (args[i].equals("-r")) { 63 permissions.add(args[++i]); 64 } else if (args[i].equals("-s")) { 65 minSdk = args[++i]; 66 } else if (args[i].equals("-t")) { 67 targetSdk = args[++i]; 68 } 69 } 70 71 if (pkgName == null) { 72 error("Missing package name"); 73 } else if (instrumentName == null) { 74 error("Missing instrumentation name"); 75 } else if (activities.isEmpty()) { 76 error("No activities"); 77 } else if (output == null) { 78 error("Missing output file"); 79 } 80 81 FileOutputStream out = null; 82 try { 83 out = new FileOutputStream(output); 84 generate(out, pkgName, instrumentName, minSdk, targetSdk, permissions, activities, 85 libraries); 86 } catch (Exception e) { 87 System.err.println("Couldn't create manifest file"); 88 } finally { 89 if (out != null) { 90 try { 91 out.close(); 92 } catch (Exception e) { 93 // Ignore 94 } 95 } 96 } 97 } 98 generate(OutputStream out, String pkgName, String instrumentName, String minSdk, String targetSdk, List<String> permissions, List<String> activities, List<String> libraries)99 /*package*/ static void generate(OutputStream out, String pkgName, String instrumentName, 100 String minSdk, String targetSdk, List<String> permissions, List<String> activities, 101 List<String> libraries) 102 throws Exception { 103 final String ns = null; 104 KXmlSerializer serializer = new KXmlSerializer(); 105 serializer.setOutput(out, "UTF-8"); 106 serializer.startDocument("UTF-8", true); 107 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); 108 serializer.startTag(ns, MANIFEST); 109 serializer.attribute(ns, "xmlns:android", "http://schemas.android.com/apk/res/android"); 110 serializer.attribute(ns, "package", pkgName); 111 serializer.startTag(ns, USES_SDK); 112 serializer.attribute(ns, "android:minSdkVersion", minSdk); 113 if (targetSdk != null) { 114 serializer.attribute(ns, "android:targetSdkVersion", targetSdk); 115 } 116 serializer.endTag(ns, USES_SDK); 117 for (String permission : permissions) { 118 serializer.startTag(ns, USES_PERMISSION); 119 serializer.attribute(ns, "android:name", permission); 120 serializer.endTag(ns, USES_PERMISSION); 121 } 122 serializer.startTag(ns, APPLICATION); 123 for (String library : libraries) { 124 serializer.startTag(ns, USES_LIBRARY); 125 serializer.attribute(ns, "android:name", library); 126 serializer.endTag(ns, USES_LIBRARY); 127 } 128 for (String activity : activities) { 129 serializer.startTag(ns, ACTIVITY); 130 serializer.attribute(ns, "android:name", activity); 131 serializer.endTag(ns, ACTIVITY); 132 } 133 serializer.endTag(ns, APPLICATION); 134 serializer.startTag(ns, INSTRUMENTATION); 135 serializer.attribute(ns, "android:name", instrumentName); 136 serializer.attribute(ns, "android:targetPackage", pkgName); 137 serializer.endTag(ns, INSTRUMENTATION); 138 serializer.endTag(ns, MANIFEST); 139 serializer.endDocument(); 140 out.flush(); 141 } 142 error(String message)143 private static void error(String message) { 144 System.err.println(message); 145 System.err.println(USAGE); 146 System.exit(1); 147 } 148 } 149