Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2010-2011 eBusiness Information, Excilys Group Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed To in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package com.googlecode.androidannotations.helper;
 
 import java.io.File;
 import java.net.URI;
 import java.util.List;
 
 
 
 public class AndroidManifestFinder {
 
 	private static final int MAX_PARENTS_FROM_SOURCE_FOLDER = 10;
 
 
 	public AndroidManifestFinder(ProcessingEnvironment processingEnv) {
 		this. = processingEnv;
 	}
 
 		try {
 		} catch (Exception e) {
 			if (e instanceof RuntimeException) {
 				throw (RuntimeExceptione;
 			}
 			throw new RuntimeException(e);
 		}
 	}
 
 		File androidManifestFile = findManifestFileThrowing();
 		return parseThrowing(androidManifestFile);
 	}

We use a dirty trick to find the AndroidManifest.xml file, since it's not available in the classpath. The idea is quite simple : create a fake class file, retrieve its URI, and start going up in parent folders to find the AndroidManifest.xml file. Any better solution will be appreciated.
 
 	private File findManifestFileThrowing() throws Exception {
 		Filer filer = .getFiler();
 
 		JavaFileObject dummySourceFile = filer.createSourceFile("dummy" + System.currentTimeMillis());
 		String dummySourceFilePath = dummySourceFile.toUri().toString();
 
 		if (dummySourceFilePath.startsWith("file:")) {
 			if (!dummySourceFilePath.startsWith("file://")) {
 				dummySourceFilePath = "file://" + dummySourceFilePath.substring("file:".length());
 			}
 		} else {
 			dummySourceFilePath = "file://" + dummySourceFilePath;
 		}
 
 		messager.printMessage(."Dummy source file: " + dummySourceFilePath);
 
 		URI cleanURI = new URI(dummySourceFilePath);
 
 		File dummyFile = new File(cleanURI);
 
 		File sourcesGenerationFolder = dummyFile.getParentFile();
 
 		File projectRoot = sourcesGenerationFolder.getParentFile();
 
 		File androidManifestFile = new File(projectRoot"AndroidManifest.xml");
 		for (int i = 0; i < i++) {
 			if (androidManifestFile.exists()) {
 				break;
 			} else {
				if (projectRoot.getParentFile() != null) {
					projectRoot = projectRoot.getParentFile();
					androidManifestFile = new File(projectRoot"AndroidManifest.xml");
else {
					break;
				}
			}
		}
		if (!androidManifestFile.exists()) {
			throw new IllegalStateException("Could not find the AndroidManifest.xml file, going up from path [" + sourcesGenerationFolder.getAbsolutePath() + "] found using dummy file [" + dummySourceFilePath + "] (max atempts: " +  + ")");
else {
			messager.printMessage(."AndroidManifest.xml file found: " + androidManifestFile.toString());
		}
		return androidManifestFile;
	}
	private AndroidManifest parseThrowing(File androidManifestFilethrows Exception {
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
		Document doc = docBuilder.parse(androidManifestFile);
		Element documentElement = doc.getDocumentElement();
		documentElement.normalize();
		String applicationPackage = documentElement.getAttribute("package");
		NodeList applicationNodes = documentElement.getElementsByTagName("application");
		String applicationQualifiedName = null;
		if (applicationNodes.getLength() > 0) {
			Node applicationNode = applicationNodes.item(0);
			Node nameAttribute = applicationNode.getAttributes().getNamedItem("android:name");
			String qualifiedName = manifestNameToValidQualifiedName(applicationPackagenameAttribute);
			if (qualifiedName != null) {
				applicationQualifiedName = qualifiedName;
else {
				if (nameAttribute != null) {
					messager.printMessage(., String.format("The class application declared in the AndroidManifest.xml cannot be found in the compile path: [%s]"nameAttribute.getNodeValue()));
				}
			}
		}
		NodeList activityNodes = documentElement.getElementsByTagName("activity");
		List<StringactivityQualifiedNames = new ArrayList<String>();
		for (int i = 0; i < activityNodes.getLength(); i++) {
			Node activityNode = activityNodes.item(i);
			Node nameAttribute = activityNode.getAttributes().getNamedItem("android:name");
			String qualifiedName = manifestNameToValidQualifiedName(applicationPackagenameAttribute);
			if (qualifiedName != null) {
				activityQualifiedNames.add(qualifiedName);
else {
				if (nameAttribute != null) {
					messager.printMessage(., String.format("A class activity declared in the AndroidManifest.xml cannot be found in the compile path: [%s]"nameAttribute.getNodeValue()));
else {
					messager.printMessage(., String.format("The %d activity node in the AndroidManifest.xml has no android:name attribute"i));
				}
			}
		}
		return new AndroidManifest(applicationPackageapplicationQualifiedNameactivityQualifiedNames);
	}
	private String manifestNameToValidQualifiedName(String applicationPackageNode nameAttribute) {
		if (nameAttribute != null) {
			String activityName = nameAttribute.getNodeValue();
			if (activityName.startsWith(applicationPackage)) {
				return returnClassIfExistsOrNull(activityName);
else {
				if (activityName.startsWith(".")) {
					return returnClassIfExistsOrNull(applicationPackage + activityName);
else {
					if (classOrModelClassExists(activityName)) {
						return activityName;
else {
						return returnClassIfExistsOrNull(applicationPackage + "." + activityName);
					}
				}
			}
else {
			return null;
		}
	}
	private boolean classOrModelClassExists(String className) {
		if (className.endsWith("_")) {
			className = className.substring(0, className.length() - 1);
		}
		return elementUtils.getTypeElement(className) != null;
	}
	private String returnClassIfExistsOrNull(String className) {
		if (classOrModelClassExists(className)) {
			return className;
else {
			return null;
		}
	}
New to GrepCode? Check out our FAQ X