Build unsigned apk from Windows command line - Android App

Android examples for App:APK File

Description

Build unsigned apk from Windows command line

Demo Code


//package com.book2s;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class Main {
    private static final String SDK_LIB_JAR_PATH = "D:\\android_sdk_for_studio\\tools\\lib\\sdklib.jar";
    private static final String batchDir = System.getProperty("user.dir")
            + "\\batch\\";

    private static void buildUnsignedApk(String projectDir, String apkName) {
        StringBuffer command = new StringBuffer();
        command.append("java -cp ").append(SDK_LIB_JAR_PATH)
                .append(" com.android.sdklib.build.ApkBuilderMain ")
                .append(projectDir).append("\\bin\\").append(apkName)
                .append(" -v -u -z ").append(projectDir)
                .append("\\bin\\resources.ap_").append(" -f ")
                .append(projectDir).append("\\bin\\classes.dex")
                .append(" -rf ").append(projectDir).append("\\src");
        buildExeBatchFiles(command.toString(), "5.bat");
    }/*from w  ww .j a va 2s.  c om*/

    private static void buildExeBatchFiles(String command, String fileName) {
        System.out.println(command);
        if (!new File(batchDir).exists()) {
            new File(batchDir).mkdirs();
        }
        String filePath = batchDir + fileName;
        try {
            writeFile(filePath, command);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void writeFile(String filePath, String content)
            throws IOException {
        FileWriter fw = new FileWriter(filePath);
        PrintWriter out = new PrintWriter(fw);
        out.write(content);
        out.println();
        fw.close();
        out.close();
    }
}

Related Tutorials