问题描述
这问题可能接缝怪异给别人,但我要问它。这里有云我需要生成或通过一些Java程序在那里我选择Android开源项目,可能是我有网页上的按钮,点击它时生成的.apk文件建立apk文件。
This question may seams to be weird to somebody, But i need to ask it.Here it goes i need to Generate or build apk file through some java program where i select the Android source project and may be i have a button on the web page and when clicked it generate .apk file.
我已经看到了,我们可以通过构建蚂蚁和摇篮,而是通过CMD apk文件。我不想这样做在cmd中,我想写一个java程序。在这里可能是我可以通过java程序运行CMD命令。
I have seen we can build the apk file through ant and gradle but through cmd. i dont want to do it in cmd, i want to write a java program. where may be i can run cmd commands through java program.
可以任何机构指导我在此。谢谢
could any body guide me on this. Thanks
感谢您提供的答案,对于我需要经过摇篮或蚂蚁,我做的,如果我有..但我在看的替代品。
Thanks for the Answers you provide, for that i need to go through gradle or ant which i do if i have to ..but i am looking at the alternatives.
推荐答案
您可以使用ANT罐的ant.jar和蚂蚁launcher.jar
在这种情况下的build.xml的路径应充分规定从Java类调用它是这样的:
You can use the ANT Jars "ant.jar and ant-launcher.jar"
in this case the path for build.xml should be fully specifiedand 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);
}
}
}
要创建一个build.xml文件,请转到Eclipse =>您的项目=>右键单击=>导出=>常规=>蚂蚁构建文件之后,你将需要运行:
To create a build.xml file go to Eclipse=>Your Project=>Right click=>Export=>General=>Ant Buildfilesafter that you will need to run :
android update project --name <project_name> --target <target_ID> --path <path_to_your_project>
这篇关于如何通过编程的java code生成apk文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!