Search in sources :

Example 1 with SignedJarBuilder

use of com.android.jarutils.SignedJarBuilder in project android by JetBrains.

the class AndroidCommonUtils method signApk.

public static void signApk(@NotNull File srcApk, @NotNull File destFile, @NotNull PrivateKey privateKey, @NotNull X509Certificate certificate) throws IOException, GeneralSecurityException {
    FileOutputStream fos = new FileOutputStream(destFile);
    SignedJarBuilder builder = new SafeSignedJarBuilder(fos, privateKey, certificate, destFile.getPath());
    FileInputStream fis = new FileInputStream(srcApk);
    try {
        builder.writeZip(fis, null);
        builder.close();
    } finally {
        try {
            fis.close();
        } catch (IOException ignored) {
        } finally {
            try {
                fos.close();
            } catch (IOException ignored) {
            }
        }
    }
}
Also used : SignedJarBuilder(com.android.jarutils.SignedJarBuilder)

Example 2 with SignedJarBuilder

use of com.android.jarutils.SignedJarBuilder in project android by JetBrains.

the class AndroidApkBuilder method finalPackage.

private static Map<AndroidCompilerMessageKind, List<String>> finalPackage(@NotNull String dexPath, @NotNull String[] javaResourceRoots, @NotNull String[] externalJars, @NotNull String[] nativeLibsFolders, @NotNull String outputApk, @NotNull String apkPath, @Nullable String customKeystorePath, boolean signed, @NotNull Condition<File> resourceFilter) {
    final Map<AndroidCompilerMessageKind, List<String>> result = new HashMap<AndroidCompilerMessageKind, List<String>>();
    result.put(ERROR, new ArrayList<String>());
    result.put(INFORMATION, new ArrayList<String>());
    result.put(WARNING, new ArrayList<String>());
    FileOutputStream fos = null;
    SignedJarBuilder builder = null;
    try {
        String keyStoreOsPath = customKeystorePath != null && customKeystorePath.length() > 0 ? customKeystorePath : DebugKeyProvider.getDefaultKeyStoreOsPath();
        DebugKeyProvider provider = createDebugKeyProvider(result, keyStoreOsPath);
        X509Certificate certificate = signed ? (X509Certificate) provider.getCertificate() : null;
        if (certificate != null && certificate.getNotAfter().compareTo(new Date()) < 0) {
            // generate a new one
            File keyStoreFile = new File(keyStoreOsPath);
            if (keyStoreFile.exists()) {
                keyStoreFile.delete();
            }
            provider = createDebugKeyProvider(result, keyStoreOsPath);
            certificate = (X509Certificate) provider.getCertificate();
        }
        if (certificate != null && certificate.getNotAfter().compareTo(new Date()) < 0) {
            String date = DateFormatUtil.formatPrettyDateTime(certificate.getNotAfter());
            result.get(ERROR).add(("Debug certificate expired on " + date + ". Cannot regenerate it, please delete file \"" + keyStoreOsPath + "\" manually."));
            return result;
        }
        PrivateKey key = provider.getDebugKey();
        if (key == null) {
            result.get(ERROR).add("Cannot create new key or keystore");
            return result;
        }
        if (!new File(apkPath).exists()) {
            result.get(ERROR).add("File " + apkPath + " not found. Try to rebuild project");
            return result;
        }
        File dexEntryFile = new File(dexPath);
        if (!dexEntryFile.exists()) {
            result.get(ERROR).add("File " + dexEntryFile.getPath() + " not found. Try to rebuild project");
            return result;
        }
        for (String externalJar : externalJars) {
            if (new File(externalJar).isDirectory()) {
                result.get(ERROR).add(externalJar + " is directory. Directory libraries are not supported");
            }
        }
        if (result.get(ERROR).size() > 0) {
            return result;
        }
        fos = new FileOutputStream(outputApk);
        builder = new SafeSignedJarBuilder(fos, key, certificate, outputApk);
        FileInputStream fis = new FileInputStream(apkPath);
        try {
            builder.writeZip(fis, null);
        } finally {
            fis.close();
        }
        builder.writeFile(dexEntryFile, AndroidCommonUtils.CLASSES_FILE_NAME);
        final HashSet<String> added = new HashSet<String>();
        for (String resourceRootPath : javaResourceRoots) {
            final HashSet<File> javaResources = new HashSet<File>();
            final File resourceRoot = new File(resourceRootPath);
            collectStandardJavaResources(resourceRoot, javaResources, resourceFilter);
            writeStandardJavaResources(javaResources, resourceRoot, builder, added);
        }
        Set<String> duplicates = new HashSet<String>();
        Set<String> entries = new HashSet<String>();
        for (String externalJar : externalJars) {
            collectDuplicateEntries(externalJar, entries, duplicates);
        }
        for (String duplicate : duplicates) {
            result.get(WARNING).add("Duplicate entry " + duplicate + ". The file won't be added");
        }
        MyResourceFilter filter = new MyResourceFilter(duplicates);
        for (String externalJar : externalJars) {
            fis = new FileInputStream(externalJar);
            try {
                builder.writeZip(fis, filter);
            } finally {
                fis.close();
            }
        }
        final HashSet<String> nativeLibs = new HashSet<String>();
        for (String nativeLibsFolderPath : nativeLibsFolders) {
            final File nativeLibsFolder = new File(nativeLibsFolderPath);
            final File[] children = nativeLibsFolder.listFiles();
            if (children != null) {
                for (File child : children) {
                    writeNativeLibraries(builder, nativeLibsFolder, child, signed, nativeLibs);
                }
            }
        }
    } catch (IOException e) {
        return addExceptionMessage(e, result);
    } catch (CertificateException e) {
        return addExceptionMessage(e, result);
    } catch (DebugKeyProvider.KeytoolException e) {
        return addExceptionMessage(e, result);
    } catch (AndroidLocation.AndroidLocationException e) {
        return addExceptionMessage(e, result);
    } catch (NoSuchAlgorithmException e) {
        return addExceptionMessage(e, result);
    } catch (UnrecoverableEntryException e) {
        return addExceptionMessage(e, result);
    } catch (KeyStoreException e) {
        return addExceptionMessage(e, result);
    } catch (GeneralSecurityException e) {
        return addExceptionMessage(e, result);
    } finally {
        if (builder != null) {
            try {
                builder.close();
            } catch (IOException e) {
                addExceptionMessage(e, result);
            } catch (GeneralSecurityException e) {
                addExceptionMessage(e, result);
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException ignored) {
            }
        }
    }
    return result;
}
Also used : HashMap(com.intellij.util.containers.HashMap) CertificateException(java.security.cert.CertificateException) AndroidCompilerMessageKind(org.jetbrains.android.util.AndroidCompilerMessageKind) DebugKeyProvider(com.android.jarutils.DebugKeyProvider) HashSet(com.intellij.util.containers.HashSet) AndroidLocation(com.android.prefs.AndroidLocation) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate) FileInputStream(java.io.FileInputStream) SignedJarBuilder(com.android.jarutils.SignedJarBuilder) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Aggregations

SignedJarBuilder (com.android.jarutils.SignedJarBuilder)2 DebugKeyProvider (com.android.jarutils.DebugKeyProvider)1 AndroidLocation (com.android.prefs.AndroidLocation)1 HashMap (com.intellij.util.containers.HashMap)1 HashSet (com.intellij.util.containers.HashSet)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 CertificateException (java.security.cert.CertificateException)1 X509Certificate (java.security.cert.X509Certificate)1 AndroidCompilerMessageKind (org.jetbrains.android.util.AndroidCompilerMessageKind)1