Search in sources :

Example 1 with Nullable

use of com.android.annotations.Nullable in project atlas by alibaba.

the class ProcessAwoAndroidResources method doFullTaskAction.

@Override
protected void doFullTaskAction() throws IOException {
    // we have to clean the source folder output in case the package name changed.
    File srcOut = getSourceOutputDir();
    if (srcOut != null) {
        // FileUtils.emptyFolder(srcOut);
        srcOut.delete();
        srcOut.mkdirs();
    }
    @Nullable File resOutBaseNameFile = getPackageOutputFile();
    // If are in instant run mode and we have an instant run enabled manifest
    File instantRunManifest = getInstantRunManifestFile();
    File manifestFileToPackage = instantRunBuildContext.isInInstantRunMode() && instantRunManifest != null && instantRunManifest.exists() ? instantRunManifest : getManifestFile();
    // 增加awb模块编译所需要的额外参数
    addAaptOptions();
    AaptPackageProcessBuilder aaptPackageCommandBuilder = new AaptPackageProcessBuilder(manifestFileToPackage, getAaptOptions()).setAssetsFolder(getAssetsDir()).setResFolder(getResDir()).setLibraries(getLibraries()).setPackageForR(getPackageForR()).setSourceOutputDir(absolutePath(srcOut)).setSymbolOutputDir(absolutePath(getTextSymbolOutputDir())).setResPackageOutput(absolutePath(resOutBaseNameFile)).setProguardOutput(absolutePath(getProguardOutputFile())).setType(getType()).setDebuggable(getDebuggable()).setPseudoLocalesEnabled(getPseudoLocalesEnabled()).setResourceConfigs(getResourceConfigs()).setSplits(getSplits()).setPreferredDensity(getPreferredDensity());
    @NonNull AtlasBuilder builder = (AtlasBuilder) getBuilder();
    // MergingLog mergingLog = new MergingLog(getMergeBlameLogFolder());
    //
    // ProcessOutputHandler processOutputHandler = new ParsingProcessOutputHandler(
    // new ToolOutputParser(new AaptOutputParser(), getILogger()),
    // builder.getErrorReporter());
    ProcessOutputHandler processOutputHandler = new LoggedProcessOutputHandler(getILogger());
    try {
        builder.processAwbResources(aaptPackageCommandBuilder, getEnforceUniquePackageName(), processOutputHandler, getMainSymbolFile());
        if (resOutBaseNameFile != null) {
            if (instantRunBuildContext.isInInstantRunMode()) {
                instantRunBuildContext.addChangedFile(InstantRunBuildContext.FileType.RESOURCES, resOutBaseNameFile);
                // get the new manifest file CRC
                JarFile jarFile = new JarFile(resOutBaseNameFile);
                String currentIterationCRC = null;
                try {
                    ZipEntry entry = jarFile.getEntry(SdkConstants.ANDROID_MANIFEST_XML);
                    if (entry != null) {
                        currentIterationCRC = String.valueOf(entry.getCrc());
                    }
                } finally {
                    jarFile.close();
                }
                // check the manifest file binary format.
                File crcFile = new File(instantRunSupportDir, "manifest.crc");
                if (crcFile.exists() && currentIterationCRC != null) {
                    // compare its content with the new binary file crc.
                    String previousIterationCRC = Files.readFirstLine(crcFile, Charsets.UTF_8);
                    if (!currentIterationCRC.equals(previousIterationCRC)) {
                        instantRunBuildContext.close();
                        instantRunBuildContext.setVerifierResult(InstantRunVerifierStatus.BINARY_MANIFEST_FILE_CHANGE);
                    }
                }
                // write the new manifest file CRC.
                Files.createParentDirs(crcFile);
                Files.write(currentIterationCRC, crcFile, Charsets.UTF_8);
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    } catch (ProcessException e) {
        throw new RuntimeException(e);
    }
}
Also used : LoggedProcessOutputHandler(com.android.ide.common.process.LoggedProcessOutputHandler) ProcessOutputHandler(com.android.ide.common.process.ProcessOutputHandler) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) ProcessException(com.android.ide.common.process.ProcessException) AaptPackageProcessBuilder(com.android.builder.core.AaptPackageProcessBuilder) NonNull(com.android.annotations.NonNull) AtlasBuilder(com.android.builder.core.AtlasBuilder) LoggedProcessOutputHandler(com.android.ide.common.process.LoggedProcessOutputHandler) JarFile(java.util.jar.JarFile) OutputFile(org.gradle.api.tasks.OutputFile) File(java.io.File) InputFile(org.gradle.api.tasks.InputFile) Nullable(com.android.annotations.Nullable)

Example 2 with Nullable

use of com.android.annotations.Nullable in project bazel by bazelbuild.

the class KeystoreHelper method createNewStore.

/**
     * Creates a new store
     * @param osKeyStorePath the location of the store
     * @param storeType an optional keystore type, or <code>null</code> if the default is to
     * be used.
     * @param output an optional {@link IKeyGenOutput} object to get the stdout and stderr
     * of the keytool process call.
     * @throws KeyStoreException
     * @throws NoSuchAlgorithmException
     * @throws CertificateException
     * @throws UnrecoverableEntryException
     * @throws IOException
     * @throws KeytoolException
     */
public static boolean createNewStore(String osKeyStorePath, String storeType, String storePassword, String alias, String keyPassword, String description, int validityYears, final IKeyGenOutput output) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableEntryException, IOException, KeytoolException {
    // get the executable name of keytool depending on the platform.
    String os = System.getProperty("os.name");
    String keytoolCommand;
    if (os.startsWith("Windows")) {
        keytoolCommand = "keytool.exe";
    } else {
        keytoolCommand = "keytool";
    }
    String javaHome = System.getProperty("java.home");
    if (javaHome != null && !javaHome.isEmpty()) {
        keytoolCommand = javaHome + File.separator + "bin" + File.separator + keytoolCommand;
    }
    // create the command line to call key tool to build the key with no user input.
    ArrayList<String> commandList = new ArrayList<String>();
    commandList.add(keytoolCommand);
    commandList.add("-genkey");
    commandList.add("-alias");
    commandList.add(alias);
    commandList.add("-keyalg");
    commandList.add("RSA");
    commandList.add("-dname");
    commandList.add(description);
    commandList.add("-validity");
    commandList.add(Integer.toString(validityYears * 365));
    commandList.add("-keypass");
    commandList.add(keyPassword);
    commandList.add("-keystore");
    commandList.add(osKeyStorePath);
    commandList.add("-storepass");
    commandList.add(storePassword);
    if (storeType != null) {
        commandList.add("-storetype");
        commandList.add(storeType);
    }
    String[] commandArray = commandList.toArray(new String[commandList.size()]);
    // launch the command line process
    int result = 0;
    try {
        Process process = Runtime.getRuntime().exec(commandArray);
        result = GrabProcessOutput.grabProcessOutput(process, Wait.WAIT_FOR_READERS, new IProcessOutput() {

            @Override
            public void out(@Nullable String line) {
                if (line != null) {
                    if (output != null) {
                        output.out(line);
                    } else {
                        System.out.println(line);
                    }
                }
            }

            @Override
            public void err(@Nullable String line) {
                if (line != null) {
                    if (output != null) {
                        output.err(line);
                    } else {
                        System.err.println(line);
                    }
                }
            }
        });
    } catch (Exception e) {
        // create the command line as one string for debugging purposes
        StringBuilder builder = new StringBuilder();
        boolean firstArg = true;
        for (String arg : commandArray) {
            boolean hasSpace = arg.indexOf(' ') != -1;
            if (firstArg == true) {
                firstArg = false;
            } else {
                builder.append(' ');
            }
            if (hasSpace) {
                builder.append('"');
            }
            builder.append(arg);
            if (hasSpace) {
                builder.append('"');
            }
        }
        throw new KeytoolException("Failed to create key: " + e.getMessage(), javaHome, builder.toString());
    }
    if (result != 0) {
        return false;
    }
    return true;
}
Also used : IProcessOutput(com.android.utils.GrabProcessOutput.IProcessOutput) KeytoolException(com.android.sdklib.internal.build.DebugKeyProvider.KeytoolException) ArrayList(java.util.ArrayList) Nullable(com.android.annotations.Nullable) KeytoolException(com.android.sdklib.internal.build.DebugKeyProvider.KeytoolException) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnrecoverableEntryException(java.security.UnrecoverableEntryException)

Example 3 with Nullable

use of com.android.annotations.Nullable in project bazel by bazelbuild.

the class ResourceUsageAnalyzer method getResourceFromCode.

@VisibleForTesting
@Nullable
Resource getResourceFromCode(@NonNull String owner, @NonNull String name) {
    Pair<ResourceType, Map<String, String>> pair = resourceObfuscation.get(owner);
    if (pair != null) {
        ResourceType type = pair.getFirst();
        Map<String, String> nameMap = pair.getSecond();
        String renamedField = nameMap.get(name);
        if (renamedField != null) {
            name = renamedField;
        }
        return model.getResource(type, name);
    }
    return null;
}
Also used : ResourceType(com.android.resources.ResourceType) Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap) VisibleForTesting(com.android.annotations.VisibleForTesting) Nullable(com.android.annotations.Nullable)

Example 4 with Nullable

use of com.android.annotations.Nullable in project buck by facebook.

the class PositionXmlParser method findNodeAtLineAndCol.

@Nullable
private static Node findNodeAtLineAndCol(@NonNull Node node, int line, int column) {
    Position p = getPositionHelper(node, -1, -1);
    if (p != null) {
        if (line < p.getLine() || line == p.getLine() && column != -1 && column < p.getColumn()) {
            return null;
        }
        Position end = p.getEnd();
        if (end != null) {
            if (line > end.getLine() || line == end.getLine() && column != -1 && column >= end.getColumn()) {
                return null;
            }
        }
    } else {
        return null;
    }
    NodeList children = node.getChildNodes();
    for (int i = 0, n = children.getLength(); i < n; i++) {
        Node item = children.item(i);
        Node match = findNodeAtLineAndCol(item, line, column);
        if (match != null) {
            return match;
        }
    }
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        for (int i = 0, n = attributes.getLength(); i < n; i++) {
            Node item = attributes.item(i);
            Node match = findNodeAtLineAndCol(item, line, column);
            if (match != null) {
                return match;
            }
        }
    }
    return node;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) SourcePosition(com.android.common.ide.common.blame.SourcePosition) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Nullable(com.android.annotations.Nullable)

Example 5 with Nullable

use of com.android.annotations.Nullable in project buck by facebook.

the class XmlUtils method getRootTagName.

/**
     * Returns the name of the root element tag stored in the given file, or null if it can't be
     * determined.
     */
@Nullable
public static String getRootTagName(@NonNull File xmlFile) {
    try (InputStream stream = new BufferedInputStream(new FileInputStream(xmlFile))) {
        XMLInputFactory factory = XMLInputFactory.newFactory();
        XMLStreamReader xmlStreamReader = factory.createXMLStreamReader(stream);
        while (xmlStreamReader.hasNext()) {
            int event = xmlStreamReader.next();
            if (event == XMLStreamReader.START_ELEMENT) {
                return xmlStreamReader.getLocalName();
            }
        }
    } catch (XMLStreamException | IOException ignored) {
    // Ignored.
    }
    return null;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) XMLInputFactory(javax.xml.stream.XMLInputFactory) Nullable(com.android.annotations.Nullable)

Aggregations

Nullable (com.android.annotations.Nullable)83 File (java.io.File)21 IOException (java.io.IOException)9 ResourceType (com.android.resources.ResourceType)8 PsiClass (com.intellij.psi.PsiClass)7 PsiElement (com.intellij.psi.PsiElement)7 PsiReferenceExpression (com.intellij.psi.PsiReferenceExpression)7 ResourceUrl (com.android.ide.common.resources.ResourceUrl)6 PsiAssignmentExpression (com.intellij.psi.PsiAssignmentExpression)6 PsiDeclarationStatement (com.intellij.psi.PsiDeclarationStatement)6 PsiExpression (com.intellij.psi.PsiExpression)6 PsiExpressionStatement (com.intellij.psi.PsiExpressionStatement)6 PsiMethod (com.intellij.psi.PsiMethod)6 PsiStatement (com.intellij.psi.PsiStatement)6 ArrayList (java.util.ArrayList)6 PsiField (com.intellij.psi.PsiField)5 PsiFile (com.intellij.psi.PsiFile)4 Node (lombok.ast.Node)4 UReferenceExpression (org.jetbrains.uast.UReferenceExpression)4 AbstractResourceRepository (com.android.ide.common.res2.AbstractResourceRepository)3