|
20 | 20 |
|
21 | 21 | public class AndroidManifestFinder { |
22 | 22 |
|
23 | | - private static final int MAX_PARENTS_FROM_SOURCE_FOLDER = 10; |
24 | | - |
25 | | - private ProcessingEnvironment processingEnv; |
26 | | - |
27 | | - public AndroidManifestFinder(ProcessingEnvironment processingEnv) { |
28 | | - this.processingEnv = processingEnv; |
29 | | - } |
30 | | - |
31 | | - public AndroidManifest extractAndroidManifest() { |
32 | | - try { |
33 | | - return extractAndroidManifestThrowing(); |
34 | | - } catch (Exception e) { |
35 | | - throw new RuntimeException(e); |
36 | | - } |
37 | | - } |
38 | | - |
39 | | - private AndroidManifest extractAndroidManifestThrowing() throws Exception { |
40 | | - File androidManifestFile = findManifestFileThrowing(); |
41 | | - return parseThrowing(androidManifestFile); |
42 | | - } |
43 | | - |
44 | | - /** |
45 | | - * We use a dirty trick to find the AndroidManifest.xml file, since it's not |
46 | | - * available in the classpath. The idea is quite simple : create a fake |
47 | | - * class file, retrieve its URI, and start going up in parent folders to |
48 | | - * find the AndroidManifest.xml file. Any better solution will be |
49 | | - * appreciated. |
50 | | - */ |
51 | | - private File findManifestFileThrowing() throws IOException { |
52 | | - Filer filer = processingEnv.getFiler(); |
53 | | - |
54 | | - JavaFileObject dummySourceFile = filer.createSourceFile("dummy" + System.currentTimeMillis()); |
55 | | - String dummySourceFilePath = dummySourceFile.toUri().toString(); |
56 | | - |
57 | | - if (dummySourceFilePath.startsWith("file:")) { |
58 | | - dummySourceFilePath = dummySourceFilePath.substring("file:".length()); |
59 | | - } |
60 | | - |
61 | | - Messager messager = processingEnv.getMessager(); |
62 | | - messager.printMessage(Kind.NOTE, "Dummy source file: " + dummySourceFilePath); |
63 | | - |
64 | | - File sourcesGenerationFolder = new File(dummySourceFilePath).getParentFile(); |
65 | | - |
66 | | - File projectRoot = sourcesGenerationFolder.getParentFile(); |
67 | | - |
68 | | - File androidManifestFile = new File(projectRoot, "AndroidManifest.xml"); |
69 | | - for (int i = 0; i < MAX_PARENTS_FROM_SOURCE_FOLDER; i++) { |
70 | | - if (androidManifestFile.exists()) { |
71 | | - break; |
72 | | - } else { |
73 | | - if (projectRoot.getParentFile() != null) { |
74 | | - projectRoot = projectRoot.getParentFile(); |
75 | | - androidManifestFile = new File(projectRoot, "AndroidManifest.xml"); |
76 | | - } else { |
77 | | - break; |
78 | | - } |
79 | | - } |
80 | | - } |
81 | | - |
82 | | - if (!androidManifestFile.exists()) { |
83 | | - throw new IllegalStateException("Could not find the AndroidManifest.xml file, going up from path " + sourcesGenerationFolder.getAbsolutePath() + " found using dummy file [" + dummySourceFilePath + "] (max atempts: " + MAX_PARENTS_FROM_SOURCE_FOLDER + ")"); |
84 | | - } else { |
85 | | - messager.printMessage(Kind.NOTE, "AndroidManifest.xml file found: " + androidManifestFile.toString()); |
86 | | - } |
87 | | - |
88 | | - return androidManifestFile; |
89 | | - } |
90 | | - |
91 | | - private AndroidManifest parseThrowing(File androidManifestFile) throws Exception { |
92 | | - |
93 | | - DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); |
94 | | - DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); |
95 | | - Document doc = docBuilder.parse(androidManifestFile); |
96 | | - |
97 | | - Element documentElement = doc.getDocumentElement(); |
98 | | - documentElement.normalize(); |
99 | | - |
100 | | - String applicationPackage = documentElement.getAttribute("package"); |
101 | | - |
102 | | - NodeList activityNodes = documentElement.getElementsByTagName("activity"); |
103 | | - |
104 | | - List<String> activityQualifiedNames = new ArrayList<String>(); |
105 | | - |
106 | | - for (int i = 0; i < activityNodes.getLength(); i++) { |
107 | | - Node activityNode = activityNodes.item(i); |
108 | | - Node nameAttribute = activityNode.getAttributes().getNamedItem("android:name"); |
109 | | - if (nameAttribute != null) { |
110 | | - |
111 | | - String activityName = nameAttribute.getNodeValue(); |
112 | | - String activityQualifiedName; |
113 | | - if (activityName.startsWith(applicationPackage)) { |
114 | | - activityQualifiedName = activityName; |
115 | | - } else { |
116 | | - if (activityName.startsWith(".")) { |
117 | | - activityQualifiedName = applicationPackage + activityName; |
118 | | - } else { |
119 | | - activityQualifiedName = applicationPackage + "." + activityName; |
120 | | - } |
121 | | - } |
122 | | - activityQualifiedNames.add(activityQualifiedName); |
123 | | - } |
124 | | - } |
125 | | - |
126 | | - return new AndroidManifest(applicationPackage, activityQualifiedNames); |
127 | | - } |
| 23 | + private static final int MAX_PARENTS_FROM_SOURCE_FOLDER = 10; |
| 24 | + |
| 25 | + private ProcessingEnvironment processingEnv; |
| 26 | + |
| 27 | + public AndroidManifestFinder(ProcessingEnvironment processingEnv) { |
| 28 | + this.processingEnv = processingEnv; |
| 29 | + } |
| 30 | + |
| 31 | + public AndroidManifest extractAndroidManifest() { |
| 32 | + try { |
| 33 | + return extractAndroidManifestThrowing(); |
| 34 | + } catch (Exception e) { |
| 35 | + throw new RuntimeException(e); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private AndroidManifest extractAndroidManifestThrowing() throws Exception { |
| 40 | + File androidManifestFile = findManifestFileThrowing(); |
| 41 | + return parseThrowing(androidManifestFile); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * We use a dirty trick to find the AndroidManifest.xml file, since it's not |
| 46 | + * available in the classpath. The idea is quite simple : create a fake |
| 47 | + * class file, retrieve its URI, and start going up in parent folders to |
| 48 | + * find the AndroidManifest.xml file. Any better solution will be |
| 49 | + * appreciated. |
| 50 | + */ |
| 51 | + private File findManifestFileThrowing() throws IOException { |
| 52 | + Filer filer = processingEnv.getFiler(); |
| 53 | + |
| 54 | + JavaFileObject dummySourceFile = filer.createSourceFile("dummy" + System.currentTimeMillis()); |
| 55 | + String dummySourceFilePath = dummySourceFile.toUri().toString(); |
| 56 | + |
| 57 | + if (dummySourceFilePath.startsWith("file:")) { |
| 58 | + dummySourceFilePath = dummySourceFilePath.substring("file:".length()); |
| 59 | + } |
| 60 | + |
| 61 | + Messager messager = processingEnv.getMessager(); |
| 62 | + messager.printMessage(Kind.NOTE, "Dummy source file: " + dummySourceFilePath); |
| 63 | + |
| 64 | + File sourcesGenerationFolder = new File(dummySourceFilePath).getParentFile(); |
| 65 | + |
| 66 | + File projectRoot = sourcesGenerationFolder.getParentFile(); |
| 67 | + |
| 68 | + File androidManifestFile = new File(projectRoot, "AndroidManifest.xml"); |
| 69 | + for (int i = 0; i < MAX_PARENTS_FROM_SOURCE_FOLDER; i++) { |
| 70 | + if (androidManifestFile.exists()) { |
| 71 | + break; |
| 72 | + } else { |
| 73 | + if (projectRoot.getParentFile() != null) { |
| 74 | + projectRoot = projectRoot.getParentFile(); |
| 75 | + androidManifestFile = new File(projectRoot, "AndroidManifest.xml"); |
| 76 | + } else { |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (!androidManifestFile.exists()) { |
| 83 | + throw new IllegalStateException("Could not find the AndroidManifest.xml file, going up from path " + sourcesGenerationFolder.getAbsolutePath() + " found using dummy file [" + dummySourceFilePath + "] (max atempts: " + MAX_PARENTS_FROM_SOURCE_FOLDER + ")"); |
| 84 | + } else { |
| 85 | + messager.printMessage(Kind.NOTE, "AndroidManifest.xml file found: " + androidManifestFile.toString()); |
| 86 | + } |
| 87 | + |
| 88 | + return androidManifestFile; |
| 89 | + } |
| 90 | + |
| 91 | + private AndroidManifest parseThrowing(File androidManifestFile) throws Exception { |
| 92 | + |
| 93 | + DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); |
| 94 | + DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); |
| 95 | + Document doc = docBuilder.parse(androidManifestFile); |
| 96 | + |
| 97 | + Element documentElement = doc.getDocumentElement(); |
| 98 | + documentElement.normalize(); |
| 99 | + |
| 100 | + String applicationPackage = documentElement.getAttribute("package"); |
| 101 | + |
| 102 | + NodeList activityNodes = documentElement.getElementsByTagName("activity"); |
| 103 | + |
| 104 | + List<String> activityQualifiedNames = new ArrayList<String>(); |
| 105 | + |
| 106 | + for (int i = 0; i < activityNodes.getLength(); i++) { |
| 107 | + Node activityNode = activityNodes.item(i); |
| 108 | + Node nameAttribute = activityNode.getAttributes().getNamedItem("android:name"); |
| 109 | + if (nameAttribute != null) { |
| 110 | + |
| 111 | + String activityName = nameAttribute.getNodeValue(); |
| 112 | + String activityQualifiedName; |
| 113 | + if (activityName.startsWith(applicationPackage)) { |
| 114 | + activityQualifiedName = activityName; |
| 115 | + } else { |
| 116 | + if (activityName.startsWith(".")) { |
| 117 | + activityQualifiedName = applicationPackage + activityName; |
| 118 | + } else { |
| 119 | + activityQualifiedName = applicationPackage + "." + activityName; |
| 120 | + } |
| 121 | + } |
| 122 | + activityQualifiedNames.add(activityQualifiedName); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return new AndroidManifest(applicationPackage, activityQualifiedNames); |
| 127 | + } |
128 | 128 |
|
129 | 129 | } |
0 commit comments