8

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.

Kirby
  • 15,127
  • 10
  • 89
  • 104
syed99
  • 332
  • 1
  • 5
  • 20

4 Answers4

5

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>
Kirby
  • 15,127
  • 10
  • 89
  • 104
mmoghrabi
  • 1,233
  • 1
  • 14
  • 23
  • I am able to execute the program but getting error like this "BUILD FAILED Target "release" does not exist in the project "Myproject". – syed99 Oct 22 '13 at 15:27
  • you have to edit the files local.properties project.properties to refer to the right path sdk.dir= as well build.properties and ant.properties if you want to sign the APK and you have to excute this android update project in the project template that has the build.xml file – mmoghrabi Oct 23 '13 at 05:21
  • Thank you very much, this code worked and generated .apk file, but i need to run "android update project -p ." at my project location to generate build.xml(have to do this bcz project copied from eclipse and it didnt have build.xml file), so i need to execute above "update" command before running the generateApkThroughAnt method, how can i perform that too in the same java file. – syed99 Oct 25 '13 at 13:50
1

I think you answer yourself to your question : use Runtime.getRuntime() and build the apk using ant or gradle.

lithos35
  • 250
  • 2
  • 9
  • 1
    yes you r right, i would have used if i know commands by which i can build it in ant or gradle. – syed99 Oct 22 '13 at 09:39
  • You have the basics in the [Android doc](http://developer.android.com/tools/building/building-cmdline.html), and you can customize the build rules too. It's really simple to use in basic way. – lithos35 Oct 22 '13 at 09:46
  • If you dont want to use the build tools provided with the SDK (like the ant one), you have to do the [all process](http://developer.android.com/tools/building/index.html#detailed-build) by yourself. – lithos35 Oct 22 '13 at 10:08
  • That's building a house using chopsticks – Ben Apr 08 '18 at 12:40
0

An apk file is basically zip file. It's contains your resources and class dex files. Zygote create dalvik process, dalvikvm execute your java codes. You cannot create apk file programaticcally because it's look like classdex+resourcesmap. If you create programmatically apk write some ascii chars in text file wher is your drawable? Where is your app icon? But you will execute java class use dalvikvm. (Basiccally run class command -c ...)

nurisezgin
  • 1,530
  • 12
  • 19
-1

Check out this question and replace the cmd commands in the example with your build commands.

Community
  • 1
  • 1
npace
  • 4,218
  • 1
  • 25
  • 35