我想用代码编译一个现有的Android项目。如果只是在控制台中键入ant debug
,它就可以工作,但如果我试图从代码中构建它,它就会失败。
我尝试使用以下代码运行脚本:
private static boolean runAntScript(String scriptName) {
File buildFile = new File(scriptName);
Project project = new Project();
DefaultLogger myLogger = new DefaultLogger();
project.setUserProperty("ant.file", buildFile.getAbsolutePath());
myLogger.setErrorPrintStream(System.err);
myLogger.setOutputPrintStream(System.out);
myLogger.setMessageOutputLevel(Project.MSG_VERBOSE);
project.addBuildListener(myLogger);
try {
project.fireBuildStarted();
project.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
project.addReference("ant.projectHelper", helper);
helper.parse(project, buildFile);
project.executeTarget("debug");
project.fireBuildFinished(null);
} catch (Exception e) {
project.fireBuildFinished(e);
return false;
}
return true;
}
日志(更新的日志在最后):
Detected Java version: 1.5 in: /usr/lib/jvm/java-7-openjdk-i386/jre
Detected OS: Linux
parsing buildfile /home/axi/workspace/Tripwire/AndroidCalculatorTest/build.xml with URI = file:///home/axi/workspace/Tripwire/AndroidCalculatorTest/build.xml
Project base dir set to: /home/axi/workspace/Tripwire/AndroidCalculatorTest
[property] Loading /home/axi/workspace/Tripwire/AndroidCalculatorTest/local.properties
[property] Loading /home/axi/workspace/Tripwire/AndroidCalculatorTest/ant.properties
[property] Unable to find property file: /home/axi/workspace/Tripwire/AndroidCalculatorTest/ant.properties
Importing file /home/axi/android-sdk-linux/tools/ant/build.xml from /home/axi/workspace/Tripwire/AndroidCalculatorTest/build.xml
parsing buildfile /home/axi/android-sdk-linux/tools/ant/build.xml with URI = file:///home/axi/android-sdk-linux/tools/ant/build.xml
Property ${key.store} has not been set
Build sequence for target(s) `debug' is [-set-mode-check, -set-debug-files, -set-debug-mode, -debug-obfuscation-check, -setup, -build-setup, -pre-build, -code-gen, -pre-compile, -compile, -post-compile, -obfuscate, -dex, -crunch, -package-resources, -package, -do-debug, debug]
Complete build sequence is [-set-mode-check, -set-debug-files, -set-debug-mode, -debug-obfuscation-check, -setup, -build-setup, -pre-build, -code-gen, -pre-compile, -compile, -post-compile, -obfuscate, -dex, -crunch, -package-resources, -package, -do-debug, debug, uninstall, -set-instrumented-mode, install, installi, installd, -test-project-check, test, -pre-clean, emma, -release-prompt-for-password, all, -set-release-mode, -release-obfuscation-check, -release-nosign, release, help, instrument, clean, installt, installr, nodeps, ]
-set-mode-check:
-set-debug-files:
-set-debug-mode:
-debug-obfuscation-check:
-setup:
[echo] Gathering info for AndroidCalculatorTest...
BUILD FAILED
/home/axi/android-sdk-linux/tools/ant/build.xml:440: java.lang.NullPointerException
at org.apache.tools.ant.Task.perform(Task.java:373)
at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:64)
at com.android.ant.IfElseTask.execute(IfElseTask.java:120)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
at at.tugraz.iaik.amd.util.CompileAnt.runAntScript(CompileAnt.java:36)
at at.tugraz.iaik.amd.util.CompileAnt.main(CompileAnt.java:15)
Caused by: java.lang.NullPointerException
at com.android.ant.NewSetupTask.getVersion(NewSetupTask.java:654)
at com.android.ant.NewSetupTask.execute(NewSetupTask.java:178)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
... 10 more
--- Nested Exception ---
java.lang.NullPointerException
at com.android.ant.NewSetupTask.getVersion(NewSetupTask.java:654)
at com.android.ant.NewSetupTask.execute(NewSetupTask.java:178)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:64)
at com.android.ant.IfElseTask.execute(IfElseTask.java:120)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
at at.tugraz.iaik.amd.util.CompileAnt.runAntScript(CompileAnt.java:36)
at at.tugraz.iaik.amd.util.CompileAnt.main(CompileAnt.java:15)
Total time: 0 seconds
控制台呼叫:
$ ant debug
Buildfile: /home/axi/workspace/Tripwire/AndroidCalculatorTest/build.xml
-set-mode-check:
-set-debug-files:
-set-debug-mode:
-debug-obfuscation-check:
-setup:
[echo] Gathering info for AndroidCalculatorTest...
[setup] Android SDK Tools Revision 16
[setup] Project Target: Android 2.2
[setup] API level: 8
[setup]
[setup] ------------------
[setup] Resolving library dependencies:
[setup] No library dependencies.
[setup]
[setup] ------------------
[setup]
[setup] WARNING: Attribute minSdkVersion in AndroidManifest.xml (7) is lower than the project target API level (8)
-build-setup:
[echo] Creating output directories if needed...
-pre-build:
-code-gen:
[echo] ----------
[echo] Handling aidl files...
[aidl] No AIDL files to compile.
[echo] ----------
[echo] Handling RenderScript files...
[renderscript] No RenderScript files to compile.
[echo] ----------
[echo] Handling Resources...
[aapt] No changed resources. R.java and Manifest.java untouched.
-pre-compile:
-compile:
-post-compile:
-obfuscate:
-dex:
[dex] Converting compiled files and external libraries into /home/axi/workspace/Tripwire/AndroidCalculatorTest/bin/classes.dex...
-crunch:
[crunch] Crunching PNG Files in source dir: /home/axi/workspace/Tripwire/AndroidCalculatorTest/res
[crunch] To destination dir: /home/axi/workspace/Tripwire/AndroidCalculatorTest/bin/res
[crunch] Processing image to cache: /home/axi/workspace/Tripwire/AndroidCalculatorTest/res/drawable-hdpi/ic_launcher.png => /home/axi/workspace/Tripwire/AndroidCalculatorTest/bin/res/drawable-hdpi/ic_launcher.png
[crunch] (processed image to cache entry /home/axi/workspace/Tripwire/AndroidCalculatorTest/bin/res/drawable-hdpi/ic_launcher.png: 0% size of source)
[crunch] Processing image to cache: /home/axi/workspace/Tripwire/AndroidCalculatorTest/res/drawable-ldpi/ic_launcher.png => /home/axi/workspace/Tripwire/AndroidCalculatorTest/bin/res/drawable-ldpi/ic_launcher.png
[crunch] (processed image to cache entry /home/axi/workspace/Tripwire/AndroidCalculatorTest/bin/res/drawable-ldpi/ic_launcher.png: 0% size of source)
[crunch] Processing image to cache: /home/axi/workspace/Tripwire/AndroidCalculatorTest/res/drawable-mdpi/ic_launcher.png => /home/axi/workspace/Tripwire/AndroidCalculatorTest/bin/res/drawable-mdpi/ic_launcher.png
[crunch] (processed image to cache entry /home/axi/workspace/Tripwire/AndroidCalculatorTest/bin/res/drawable-mdpi/ic_launcher.png: 0% size of source)
[crunch] Crunched 3 PNG files to update cache
-package-resources:
[aapt] Creating full resource package...
-package:
[apkbuilder] Current build type is different than previous build: forced apkbuilder run.
[apkbuilder] Creating AndroidCalculatorTest-debug-unaligned.apk and signing it with a debug key...
-do-debug:
[zipalign] Running zip align on final apk...
[echo] Debug Package: /home/axi/workspace/Tripwire/AndroidCalculatorTest/bin/AndroidCalculatorTest-debug.apk
debug:
[propertyfile] Creating new property file: /home/axi/workspace/Tripwire/AndroidCalculatorTest/bin/build.prop
[propertyfile] Updating property file: /home/axi/workspace/Tripwire/AndroidCalculatorTest/bin/build.prop
[propertyfile] Updating property file: /home/axi/workspace/Tripwire/AndroidCalculatorTest/bin/build.prop
[propertyfile] Updating property file: /home/axi/workspace/Tripwire/AndroidCalculatorTest/bin/build.prop
BUILD SUCCESSFUL
Total time: 6 seconds
如果我加上:
project.setProperty("key.store", "/home/axi/.android/debug.keystore");
project.setProperty("ant.home", "/home/axi/Downloads/apache-ant-1.8.3/bin");
project.setProperty("ant.version", "Apache Ant(TM) version 1.8.3 compiled on February 26 2012");
现在的问题是:
/home/axi/android-sdk-linux/tools/ant/build.xml:445: java.lang.NoSuchMethodError: com.android.ant.TaskHelper.getSdkLocation(Lorg/apache/tools/ant/Project;)Ljava/io/File;
at com.android.ant.NewSetupTask.execute(NewSetupTask.java:189)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
at com.android.ant.IfElseTask.execute(IfElseTask.java:120)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:392)
at org.apache.tools.ant.Target.performTasks(Target.java:413)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at at.tugraz.iaik.amd.util.CompileAnt.runAntScript(CompileAnt.java:44)
at at.tugraz.iaik.amd.util.CompileAnt.main(CompileAnt.java:15)
在将所有(可能不是所有)Android Sdk和Ant libs以及tools.jar添加到类路径后,它就工作了:)。