Search in sources :

Example 1 with AndroidManifestNotFoundException

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);
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) AndroidManifestNotFoundException(org.androidannotations.internal.exception.AndroidManifestNotFoundException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) AndroidManifestNotFoundException(org.androidannotations.internal.exception.AndroidManifestNotFoundException)

Example 2 with AndroidManifestNotFoundException

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);
    }
}
Also used : AndroidManifestNotFoundException(org.androidannotations.internal.exception.AndroidManifestNotFoundException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 AndroidManifestNotFoundException (org.androidannotations.internal.exception.AndroidManifestNotFoundException)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1