use of org.androidannotations.internal.exception.AndroidManifestNotFoundException in project androidannotations by androidannotations.
the class AndroidManifestFinder method parse.
private AndroidManifest parse(File androidManifestFile, boolean libraryProject) throws AndroidManifestNotFoundException {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
Document doc;
try {
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
doc = docBuilder.parse(androidManifestFile);
} catch (Exception e) {
LOGGER.error("Could not parse the AndroidManifest.xml file at path {}", androidManifestFile, e);
throw new AndroidManifestNotFoundException("Could not parse the AndroidManifest.xml file at path {}" + androidManifestFile, e);
}
Element documentElement = doc.getDocumentElement();
documentElement.normalize();
String applicationPackage = documentElement.getAttribute("package");
int minSdkVersion = -1;
int maxSdkVersion = -1;
int targetSdkVersion = -1;
NodeList sdkNodes = documentElement.getElementsByTagName("uses-sdk");
if (sdkNodes.getLength() > 0) {
Node sdkNode = sdkNodes.item(0);
minSdkVersion = extractAttributeIntValue(sdkNode, "android:minSdkVersion", -1);
maxSdkVersion = extractAttributeIntValue(sdkNode, "android:maxSdkVersion", -1);
targetSdkVersion = extractAttributeIntValue(sdkNode, "android:targetSdkVersion", -1);
}
if (libraryProject) {
return AndroidManifest.createLibraryManifest(applicationPackage, minSdkVersion, maxSdkVersion, targetSdkVersion);
}
NodeList applicationNodes = documentElement.getElementsByTagName("application");
String applicationClassQualifiedName = null;
boolean applicationDebuggableMode = false;
if (applicationNodes.getLength() > 0) {
Node applicationNode = applicationNodes.item(0);
Node nameAttribute = applicationNode.getAttributes().getNamedItem("android:name");
applicationClassQualifiedName = manifestNameToValidQualifiedName(applicationPackage, nameAttribute);
if (applicationClassQualifiedName == null) {
if (nameAttribute != null) {
LOGGER.warn("The class application declared in the AndroidManifest.xml cannot be found in the compile path: [{}]", nameAttribute.getNodeValue());
}
}
Node debuggableAttribute = applicationNode.getAttributes().getNamedItem("android:debuggable");
if (debuggableAttribute != null) {
applicationDebuggableMode = debuggableAttribute.getNodeValue().equalsIgnoreCase("true");
}
}
NodeList activityNodes = documentElement.getElementsByTagName("activity");
List<String> activityQualifiedNames = extractComponentNames(applicationPackage, activityNodes);
NodeList serviceNodes = documentElement.getElementsByTagName("service");
List<String> serviceQualifiedNames = extractComponentNames(applicationPackage, serviceNodes);
NodeList receiverNodes = documentElement.getElementsByTagName("receiver");
List<String> receiverQualifiedNames = extractComponentNames(applicationPackage, receiverNodes);
NodeList providerNodes = documentElement.getElementsByTagName("provider");
List<String> providerQualifiedNames = extractComponentNames(applicationPackage, providerNodes);
List<String> componentQualifiedNames = new ArrayList<>();
componentQualifiedNames.addAll(activityQualifiedNames);
componentQualifiedNames.addAll(serviceQualifiedNames);
componentQualifiedNames.addAll(receiverQualifiedNames);
componentQualifiedNames.addAll(providerQualifiedNames);
NodeList metaDataNodes = documentElement.getElementsByTagName("meta-data");
Map<String, AndroidManifest.MetaDataInfo> metaDataQualifiedNames = extractMetaDataQualifiedNames(metaDataNodes);
NodeList usesPermissionNodes = documentElement.getElementsByTagName("uses-permission");
List<String> usesPermissionQualifiedNames = extractUsesPermissionNames(usesPermissionNodes);
List<String> permissionQualifiedNames = new ArrayList<>();
permissionQualifiedNames.addAll(usesPermissionQualifiedNames);
return AndroidManifest.createManifest(applicationPackage, applicationClassQualifiedName, componentQualifiedNames, metaDataQualifiedNames, permissionQualifiedNames, minSdkVersion, maxSdkVersion, targetSdkVersion, applicationDebuggableMode);
}
use of org.androidannotations.internal.exception.AndroidManifestNotFoundException in project androidannotations by androidannotations.
the class AndroidManifestFinder method extractAndroidManifest.
public AndroidManifest extractAndroidManifest() throws AndroidManifestNotFoundException {
try {
File androidManifestFile = findManifestFile();
String projectDirectory = androidManifestFile.getParent();
boolean libraryOption = environment.getOptionBooleanValue(OPTION_LIBRARY);
if (libraryOption) {
return parse(androidManifestFile, true);
}
File projectProperties = new File(projectDirectory, "project.properties");
boolean libraryProject = false;
if (projectProperties.exists()) {
Properties properties = new Properties();
try {
properties.load(new FileInputStream(projectProperties));
if (properties.containsKey("android.library")) {
String androidLibraryProperty = properties.getProperty("android.library");
libraryProject = androidLibraryProperty.equals("true");
LOGGER.debug("Found android.library={} property in project.properties", libraryProject);
}
} catch (IOException ignored) {
// we assume the project is not a library
}
}
return parse(androidManifestFile, libraryProject);
} catch (FileNotFoundException exception) {
throw new AndroidManifestNotFoundException("Unable to find AndroidManifest.xml", exception);
}
}
Aggregations