Skip to content

Commit 939b2bd

Browse files
committed
Improved the way the AndroidManifest.xml file is retrieved, so that it may work with Maven and most build systems
1 parent a988582 commit 939b2bd

1 file changed

Lines changed: 76 additions & 42 deletions

File tree

AndroidAnnotations/src/main/java/com/googlecode/androidannotations/rclass/CompiledRClassFinder.java

Lines changed: 76 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -27,67 +27,101 @@
2727
import javax.lang.model.element.TypeElement;
2828
import javax.lang.model.util.Elements;
2929
import javax.tools.Diagnostic.Kind;
30-
import javax.tools.FileObject;
31-
import javax.tools.StandardLocation;
30+
import javax.tools.JavaFileObject;
3231

3332
import com.googlecode.androidannotations.helper.AnnotationHelper;
3433
import com.googlecode.androidannotations.model.AnnotationElements;
3534

3635
public class CompiledRClassFinder extends AnnotationHelper implements RClassFinder {
3736

38-
public CompiledRClassFinder(ProcessingEnvironment processingEnv) {
39-
super(processingEnv);
40-
}
37+
private static final int MAX_PARENTS_FROM_SOURCE_FOLDER = 10;
4138

42-
public IRClass find(AnnotationElements extractedModel) throws IOException {
39+
public CompiledRClassFinder(ProcessingEnvironment processingEnv) {
40+
super(processingEnv);
41+
}
4342

44-
File manifestFile = findManifestFile();
43+
public IRClass find(AnnotationElements extractedModel) throws IOException {
4544

46-
String rClassPackage = extractPackage(manifestFile);
45+
File manifestFile = findManifestFile();
4746

48-
if (rClassPackage == null) {
49-
Messager messager = processingEnv.getMessager();
50-
messager.printMessage(Kind.WARNING, "Could not find the AndroidManifest.xml file in " + manifestFile.getAbsolutePath());
51-
return IRClass.EMPTY_R_CLASS;
52-
}
47+
String rClassPackage = extractPackage(manifestFile);
5348

54-
Elements elementUtils = processingEnv.getElementUtils();
55-
String rClass = rClassPackage + ".R";
56-
TypeElement rType = elementUtils.getTypeElement(rClass);
49+
if (rClassPackage == null) {
50+
Messager messager = processingEnv.getMessager();
51+
messager.printMessage(Kind.WARNING, "Could not find the AndroidManifest.xml file in " + manifestFile.getAbsolutePath());
52+
return IRClass.EMPTY_R_CLASS;
53+
}
5754

58-
if (rType == null) {
59-
Messager messager = processingEnv.getMessager();
60-
messager.printMessage(Kind.WARNING, "The AndroidManifest.xml file was found, but not the compiled R class: " + rClass);
61-
return IRClass.EMPTY_R_CLASS;
62-
}
55+
Elements elementUtils = processingEnv.getElementUtils();
56+
String rClass = rClassPackage + ".R";
57+
TypeElement rType = elementUtils.getTypeElement(rClass);
6358

64-
return new RClass(rType);
65-
}
59+
// processingEnv.getFiler().getResource(StandardLocation.SOURCE_OUTPUT,
60+
// rClassPackage, "R.java");
6661

67-
private String extractPackage(File manifestFile) throws FileNotFoundException, IOException {
68-
FileReader reader = new FileReader(manifestFile);
69-
BufferedReader br = new BufferedReader(reader);
62+
if (rType == null) {
63+
Messager messager = processingEnv.getMessager();
64+
messager.printMessage(Kind.WARNING, "The AndroidManifest.xml file was found, but not the compiled R class: " + rClass);
65+
return IRClass.EMPTY_R_CLASS;
66+
}
7067

71-
String line;
72-
while ((line = br.readLine()) != null) {
68+
return new RClass(rType);
69+
}
7370

74-
ManifestPackageExtractor extractor = new ManifestPackageExtractor(line);
71+
private String extractPackage(File manifestFile) throws FileNotFoundException, IOException {
72+
FileReader reader = new FileReader(manifestFile);
73+
BufferedReader br = new BufferedReader(reader);
7574

76-
if (extractor.matches()) {
77-
return extractor.extract();
78-
}
79-
}
80-
return null;
81-
}
75+
String line;
76+
while ((line = br.readLine()) != null) {
8277

83-
private File findManifestFile() throws IOException {
84-
Filer filer = processingEnv.getFiler();
78+
ManifestPackageExtractor extractor = new ManifestPackageExtractor(line);
8579

86-
FileObject res = filer.getResource(StandardLocation.CLASS_OUTPUT, "", "");
87-
File projectRoot = new File(res.toUri()).getParentFile();
80+
if (extractor.matches()) {
81+
return extractor.extract();
82+
}
83+
}
84+
return null;
85+
}
8886

89-
File androidManifestFile = new File(projectRoot, "AndroidManifest.xml");
90-
return androidManifestFile;
91-
}
87+
private File findManifestFile() throws IOException {
88+
Filer filer = processingEnv.getFiler();
89+
90+
JavaFileObject dummySourceFile = filer.createSourceFile("dummy" + System.currentTimeMillis());
91+
String dummySourceFilePath = dummySourceFile.toUri().toString();
92+
93+
if (dummySourceFilePath.startsWith("file:")) {
94+
dummySourceFilePath = dummySourceFilePath.substring("file:".length());
95+
}
96+
97+
Messager messager = processingEnv.getMessager();
98+
messager.printMessage(Kind.NOTE, "Dummy source file: " + dummySourceFilePath);
99+
100+
File sourcesGenerationFolder = new File(dummySourceFilePath).getParentFile();
101+
102+
File projectRoot = sourcesGenerationFolder.getParentFile();
103+
104+
File androidManifestFile = new File(projectRoot, "AndroidManifest.xml");
105+
for (int i = 0; i < MAX_PARENTS_FROM_SOURCE_FOLDER; i++) {
106+
if (androidManifestFile.exists()) {
107+
break;
108+
} else {
109+
if (projectRoot.getParentFile() != null) {
110+
projectRoot = projectRoot.getParentFile();
111+
androidManifestFile = new File(projectRoot, "AndroidManifest.xml");
112+
} else {
113+
break;
114+
}
115+
}
116+
}
117+
118+
if (!androidManifestFile.exists()) {
119+
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 + ")");
120+
} else {
121+
messager.printMessage(Kind.NOTE, "AndroidManifest.xml file found: " + androidManifestFile.toString());
122+
}
123+
124+
return androidManifestFile;
125+
}
92126

93127
}

0 commit comments

Comments
 (0)