Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate apk file programmatically through java code

Tags:

android

apk

I need to generate or build an APK file through some Java program where I select the Android source project. Suppose I have a button on a web page. When clicked, it generates an .apk file.

I have seen we can build the APK file through Ant and Gradle. But this runs through the command shell. I don't want to do it in a command shell. I want to write a Java program. Or maybe I can run shell commands through a Java program.

Could anybody guide me on this? Thanks

Thanks for the answers you have provided. For those answers I need to go through Gradle or Ant. I will do that if I have to. But, I am looking for alternatives.

like image 228
syed99 Avatar asked Oct 22 '13 07:10

syed99


People also ask

How is an APK generated?

To make an APK file, a program for Android is first compiled using a tool such as Android Studio or Visual Studio and then all of its parts are packaged into one container file. An APK file contains all of a program's code (such as .dex files), resources, assets, certificates, and manifest file.

Are APK files Java?

What Are APK Files? Android applications are distributed as APK files. APK files are basically ZIP files similar to the JAR files used to package Java libraries. An APK file contains app code in the DEX file format, native libraries, resources, assets, etc.

What is APK in Java?

APK: Android Application Package An APK file is the file format used to install the applications on android operating system. A program in android is first compiled, and then all of its parts are packaged into one single file to make it an APK file.


1 Answers

You can use the ANT jars ant.jar and ant-launcher.jar.
In this case the path for build.xml should be fully specified. Call it from your Java class this way:

public class AntTest {
public static void main(String[] args) {
    String build = "D:/xampp/htdocs/aud/TempProject/build.xml";
    generateApkThroughAnt(build);
}
/*
 * Generate APK through ANT API Method
 */
public static void generateApkThroughAnt(String buildPath) {
    File antBuildFile = new File(buildPath);
    Project p = new Project();
    p.setUserProperty("ant.file", antBuildFile.getAbsolutePath());
    DefaultLogger consoleLogger = new DefaultLogger();
    consoleLogger.setErrorPrintStream(System.err);
    consoleLogger.setOutputPrintStream(System.out);
    consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
    p.addBuildListener(consoleLogger);
    BuildException ex = null;
    try {
        p.fireBuildStarted();
        p.init();
        ProjectHelper helper = ProjectHelper.getProjectHelper();
        p.addReference("ant.projectHelper", helper);
        helper.parse(p, antBuildFile);
        p.executeTarget("clean");
        p.executeTarget("release");
    } catch (BuildException e) {
        ex = e;
    } finally {
        p.fireBuildFinished(ex);
    }
   }
   }

To create a build.xml file go to Eclipse=>Your Project=>Right click=>Export=>General=>Ant Buildfiles. After that then you will need to run:

android update project --name <project_name> --target <target_ID> --path <path_to_your_project>
like image 131
mmoghrabi Avatar answered Oct 25 '22 22:10

mmoghrabi