|
17 | 17 |
|
18 | 18 | import java.io.File; |
19 | 19 | import java.io.FileInputStream; |
| 20 | +import java.io.IOException; |
20 | 21 | import java.net.URI; |
| 22 | +import java.net.URISyntaxException; |
21 | 23 | import java.util.ArrayList; |
22 | 24 | import java.util.List; |
23 | 25 | import java.util.Properties; |
|
38 | 40 |
|
39 | 41 | public class AndroidManifestFinder { |
40 | 42 |
|
| 43 | + private static final String ANDROID_MANIFEST_FILE = "androidManifestFile"; |
| 44 | + |
41 | 45 | private static final int MAX_PARENTS_FROM_SOURCE_FOLDER = 10; |
42 | 46 |
|
43 | 47 | private ProcessingEnvironment processingEnv; |
@@ -82,14 +86,34 @@ private AndroidManifest extractAndroidManifestThrowing() throws Exception { |
82 | 86 | return parseThrowing(androidManifestFile, libraryProject); |
83 | 87 | } |
84 | 88 |
|
| 89 | + private File findManifestFileThrowing() throws Exception { |
| 90 | + if (processingEnv.getOptions().containsKey(ANDROID_MANIFEST_FILE)) { |
| 91 | + return findManifestInSpecifiedPath(); |
| 92 | + } else { |
| 93 | + return findManifestInParentsDirectories(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private File findManifestInSpecifiedPath() { |
| 98 | + String path = processingEnv.getOptions().get(ANDROID_MANIFEST_FILE); |
| 99 | + File androidManifestFile = new File(path, "AndroidManifest.xml"); |
| 100 | + Messager messager = processingEnv.getMessager(); |
| 101 | + if (!androidManifestFile.exists()) { |
| 102 | + throw new IllegalStateException("Could not find the AndroidManifest.xml file in specified path : " + path); |
| 103 | + } else { |
| 104 | + messager.printMessage(Kind.NOTE, "AndroidManifest.xml file found: " + androidManifestFile.toString()); |
| 105 | + } |
| 106 | + return androidManifestFile; |
| 107 | + } |
| 108 | + |
85 | 109 | /** |
86 | 110 | * We use a dirty trick to find the AndroidManifest.xml file, since it's not |
87 | 111 | * available in the classpath. The idea is quite simple : create a fake |
88 | 112 | * class file, retrieve its URI, and start going up in parent folders to |
89 | 113 | * find the AndroidManifest.xml file. Any better solution will be |
90 | 114 | * appreciated. |
91 | 115 | */ |
92 | | - private File findManifestFileThrowing() throws Exception { |
| 116 | + private File findManifestInParentsDirectories() throws IOException, URISyntaxException { |
93 | 117 | Filer filer = processingEnv.getFiler(); |
94 | 118 |
|
95 | 119 | JavaFileObject dummySourceFile = filer.createSourceFile("dummy" + System.currentTimeMillis()); |
|
0 commit comments