Navdroid
Navdroid

Reputation: 1549

How to generate an apk file from Android Application programmatically?

This is a weird question . I want to know is there any way we could generate an apk from an android App. Let me explain- An android app which run cmd Commands and could generate apk for another application. Is there any possible way.

I have read that this could be done using ant in Java. I want to know that is this possible on a running android app.

Thanks.

Upvotes: 0

Views: 4489

Answers (4)

Hardik Trivedi
Hardik Trivedi

Reputation: 6102

Scripts can help you. Make a simple scripts which runs all android commands starting from compiling, packaging, creating key-store, signing using that key-store into release mode.

Android has provided everything which can work from command line.Just merge them in form of script and you will be able to generate .apk file.

You can make simple PHP script or ant script.

Upvotes: 3

Naveed Ahmad
Naveed Ahmad

Reputation: 6747

May be this is Possible, but You can do this by Reverse Engineering methods, so after getting source code of that apk file,compile it using Android IDE. Reverse Engineering Strp by Step Method for apk decoding:

Step 1:

Make a new folder and put .apk file in it (which you want to decode). Now rename the extension of this .apk file to .zip (eg.: rename from filename.apk to filename.zip) and save it. Now you get classes.dex files, etc. At this stage you are able to see drawable but not xml and java files, so continue.

Step 2:

Now extract this zip apk file in the same folder (or NEW FOLDER). Now download dex2jar from this link http://code.google.com/p/dex2jar/ and extract it to the same folder (or NEW FOLDER). Now open command prompt and change directory to that folder (or NEW FOLDER). Then write dex2jar classes.dex and press enter. Now you get classes.dex.dex2jar file in the same folder. Then download java decompiler from http://varaneckas.com/jad and now double click on jd-gui and click on open file. Then open classes.dex.dex2jar file from that folder. Now you get class files and save all these class files (click on file then click "save all sources" in jd-gui) by src name. At this stage you get java source but the xml files are still unreadable, so continue.

Step 3:

Now open another new folder and put these files

put .apk file which you want to decode

download apktool v1.x AND apktool install window (both can be downloaded at the same location) and put in the same folder

download framework-res.apk file and put in the same folder (Not all apk file need framework-res.apk file)

Open a command window

Navigate to the root directory of APKtool and type the following command: apktool if framework-res.apk

apktool d "fname".apk ("fname" denotes filename which you want to decode)

now you get a file folder in that folder and now you can easily read xml files also.

Step 4:

It's not any step just copy contents of both folder(in this case both new folder)to the single one

and now enjoy with source code...

Upvotes: 2

M D
M D

Reputation: 47817

You can use the ANT Jars ant.jar and ant-launcher.jar in this case the path for build.xml should be fully specified and call it from 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 you will need to run :

android update project --name <project_name> --target <target_ID> --path <path_to_your_project>

Upvotes: 4

user2459179
user2459179

Reputation: 307

I don't think that's possible.

Because Android doesn't include the "compiler" inside itself. Well I guess If you really want to do it, you can always make a server the generate the apk file, and make your app connect with said server.

Hope that help you :).

Upvotes: 0

Related Questions