[英]Unable to build android test project with ant
我有一個使用 Eclipse 創建的示例 Android 應用程序,有一個 Activity。 我運行這個命令來為這個項目構建 ant 構建文件:
android create project --target 8 --name SampleApp --path ./SampleApp --activity MainActivity --package com.example.sampleapp
運行“ant debug”成功構建項目並創建apk文件:
-post-build:
debug:
BUILD SUCCESSFUL
Total time: 1 second
現在,我想創建一個 Android 測試項目。 我運行這個命令:
android create test-project -m ../SampleApp -n SampleAppTest -p SampleAppTest
這成功創建了包含構建文件的 SampleAppTest 目錄。 我有一個類,src/com/example/sampleapp/MainActivityTest.java:
package com.example.sampleapp;
import android.test.ActivityInstrumentationTestCase2;
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
public MainActivityTest() {
super("com.example.sampleapp", MainActivity.class);
}
}
我想建立這個項目。 在http://developer.android.com/tools/testing/testing_otheride.html#RunTestsAnt的教程中,它說我應該能夠進行“螞蟻運行測試”。 不幸的是,這個目標不存在:
$ ant run-tests
Buildfile: /Users/cmuraru/Work/androidtest/SampleAppTest/build.xml
BUILD FAILED
Target "run-tests" does not exist in the project "SampleAppTest".
Total time: 0 seconds
如果我嘗試“ant debug”,我會在編譯時收到一個錯誤,指出找不到 MainActivity 類(來自 SampleApp)。
-compile:
[javac] Compiling 2 source files to /Users/cmuraru/Work/androidtest/build/test/classes
[javac] /Users/cmuraru/Work/androidtest/SampleAppTest/src/com/example/sampleapp/MainActivityTest.java:15: cannot find symbol
[javac] symbol: class MainActivity
[javac] public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
[javac] ^
[javac] /Users/cmuraru/Work/androidtest/SampleAppTest/src/com/example/sampleapp/MainActivityTest.java:18: cannot find symbol
[javac] symbol : class MainActivity
[javac] location: class com.example.sampleapp.MainActivityTest
[javac] super("com.example.sampleapp", MainActivity.class);
[javac] ^
[javac] 2 errors
BUILD FAILED
/Users/cmuraru/Work/android-sdk-macosx/tools/ant/build.xml:705: The following error occurred while executing this line:
/Users/cmuraru/Work/android-sdk-macosx/tools/ant/build.xml:718: Compile failed; see the compiler error output for details.
Total time: 1 second
嘗試“螞蟻測試”時,它也失敗了:
test:
[echo] Running tests ...
[exec] INSTRUMENTATION_STATUS: id=ActivityManagerService
[exec] INSTRUMENTATION_STATUS: Error=Unable to find instrumentation info for: ComponentInfo{com.example.sampleapp.tests/android.test.InstrumentationTestRunner}
[exec] INSTRUMENTATION_STATUS_CODE: -1
[exec] android.util.AndroidException: INSTRUMENTATION_FAILED: com.example.sampleapp.tests/android.test.InstrumentationTestRunner
[exec] at com.android.commands.am.Am.runInstrument(Am.java:616)
[exec] at com.android.commands.am.Am.run(Am.java:118)
[exec] at com.android.commands.am.Am.main(Am.java:81)
[exec] at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
[exec] at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:237)
[exec] at dalvik.system.NativeStart.main(Native Method)
我在這里錯過了什么/做錯了什么? 任何幫助都非常感謝。
好的,我發現了問題。 我沒有提到在 ant.properties 中,我已將 out.dir 位置更改為“../build”。 正因為如此,編譯器在構建 SampleAppTest 時不知道從哪里獲取 SampleApp 的類文件。 因此,一種解決方案是刪除 ant.properties 中的 out.dir 條目,但我不想那樣做。 我必須找到一種方法來告訴編譯器從哪里獲取這些類。 我找到的解決方案是像這樣覆蓋 build.xml 中的“-compile”目標:
<property name="SampleApp.build.location" value="../build"/>
<target name="-compile" depends="-build-setup, -pre-build, -code-gen, -pre-compile">
<do-only-if-manifest-hasCode elseText="hasCode = false. Skipping...">
<!-- merge the project's own classpath and the tested project's classpath -->
<path id="project.javac.classpath">
<path refid="project.all.jars.path" />
<path refid="tested.project.classpath" />
</path>
<javac encoding="${java.encoding}"
source="${java.source}" target="${java.target}"
debug="true" extdirs="" includeantruntime="false"
destdir="${out.classes.absolute.dir}"
bootclasspathref="project.target.class.path"
verbose="${verbose}"
classpathref="project.javac.classpath"
fork="${need.javac.fork}">
<src path="${source.absolute.dir}" />
<src path="${gen.absolute.dir}" />
<compilerarg line="${java.compilerargs}" />
<classpath>
<!-- steff: we changed one line here !-->
<fileset dir="${SampleApp.build.location}/classes" includes="*"/>
</classpath>
</javac>
<!-- if the project is instrumented, intrument the classes -->
<if condition="${build.is.instrumented}">
<then>
<echo level="info">Instrumenting classes from ${out.absolute.dir}/classes...</echo>
<!-- build the filter to remove R, Manifest, BuildConfig -->
<getemmafilter
appPackage="${project.app.package}"
libraryPackagesRefId="project.library.packages"
filterOut="emma.default.filter"/>
<!-- define where the .em file is going. This may have been
setup already if this is a library -->
<property name="emma.coverage.absolute.file" location="${out.absolute.dir}/coverage.em" />
<!-- It only instruments class files, not any external libs -->
<emma enabled="true">
<instr verbosity="${verbosity}"
mode="overwrite"
instrpath="${out.absolute.dir}/classes"
outdir="${out.absolute.dir}/classes"
metadatafile="${emma.coverage.absolute.file}">
<filter excludes="${emma.default.filter}" />
<filter value="${emma.filter}" />
</instr>
</emma>
</then>
</if>
<!-- if the project is a library then we generate a jar file -->
<if condition="${project.is.library}">
<then>
<echo level="info">Creating library output jar file...</echo>
<property name="out.library.jar.file" location="${out.absolute.dir}/classes.jar" />
<if>
<condition>
<length string="${android.package.excludes}" trim="true" when="greater" length="0" />
</condition>
<then>
<echo level="info">Custom jar packaging exclusion: ${android.package.excludes}</echo>
</then>
</if>
<propertybyreplace name="project.app.package.path" input="${project.app.package}" replace="." with="/" />
<jar destfile="${out.library.jar.file}">
<fileset dir="${out.classes.absolute.dir}"
includes="**/*.class"
excludes="${project.app.package.path}/R.class ${project.app.package.path}/R$*.class ${project.app.package.path}/BuildConfig.class"/>
<fileset dir="${source.absolute.dir}" excludes="**/*.java ${android.package.excludes}" />
</jar>
</then>
</if>
</do-only-if-manifest-hasCode>
</target>
有一種方法可以代替手動更新 ant.properties。 從命令行,運行以下命令:
android update test-project -m <project path> -p <test project path>
運行此命令后,您可以直接從測試項目運行 ant debug。 注意:如果出現 JUnit 相關錯誤,請在測試項目的 libs 文件夾中添加 Junit jar 文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.