How does Ant package third-party JAR packages used by the UIAutomator project, antuiautomator
This article mainly describes the various problems encountered in the normal packaging method when a third-party Jar package is referenced in the UIAutomator project, as well as the Final Solution ideas and methods.
1. Origin
The jar package of the unit test framework hamcrest is referenced in my example project. The following problems occur when ant build is executed in the project directory:
The source code is as follows:
package majcit.com.UIAutomatorDemo;import com.android.uiautomator.core.UiDevice;import com.android.uiautomator.core.UiObject;import com.android.uiautomator.core.UiObjectNotFoundException;import com.android.uiautomator.core.UiScrollable;import com.android.uiautomator.core.UiSelector;import com.android.uiautomator.testrunner.UiAutomatorTestCase;import static org.hamcrest.Matchers.*;import static org.hamcrest.MatcherAssert.assertThat;public class NotePadTest extends UiAutomatorTestCase { public void testDemo() throws UiObjectNotFoundException { UiDevice device = getUiDevice(); device.pressHome(); // Start Notepad UiObject appNotes = new UiObject(new UiSelector().text("Notes")); appNotes.click(); //Sleep 3 seconds till the app get ready try { Thread.sleep(3000); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //Evoke the system menu option device.pressMenu(); UiObject addNote = new UiObject(new UiSelector().text("Add note")); addNote.click(); //Add a new note UiObject noteContent = new UiObject(new UiSelector().className("android.widget.EditText")); noteContent.clearTextField(); noteContent.setText("Note 1"); device.pressMenu(); UiObject save = new UiObject(new UiSelector().text("Save")); save.click(); //Find out the new added note entry UiScrollable noteList = new UiScrollable( new UiSelector().className("android.widget.ListView")); //UiScrollable noteList = new UiScrollable( new UiSelector().scrollable(true)); UiObject note = null; if(noteList.exists()) { note = noteList.getChildByText(new UiSelector().className("android.widget.TextView"), "Note1", true); //note = noteList.getChildByText(new UiSelector().text("Note1"), "Note1", true); } else { note = new UiObject(new UiSelector().text("Note1")); } assertThat(note,notNullValue()); note.longClick(); UiObject delete = new UiObject(new UiSelector().text("Delete")); delete.click(); } }
2. Problem Analysis and Solution
2.1 compilation Problem Analysis
According to the error log, it is obvious that ant did not add the required third-party jar package to compile when ant build is implemented.
According to the description in the previous article "uiautomation or, when packaging the UIAutomator project, we run the "android create uitest-project-n <name>-t <android-sdk-ID>-p <path>" command to go to the top layer of the project. directory to generate a build. xml file, which is used by ant to build the configuration description file required by our UIAutomator project. Naturally, we will think of this file to see if we have included the jar package we need.
When you open the file to view it, you find that it is quite simple and there is not much to read, but you noticed that the file "$ {SDK. dir}/tools/ant/uibuild. xml ":
Open this file, and there are all build-related configurations, so the problem may occur here.
Find the Section related to compilation, and you do not see the classpath with the specified third-party jar package:
2.2 solution to compilation problems
Naturally, we should specify the classpath of our third-party jar package here so that ant can know where to get our third-party package during build.
My example here is to reference all the jar packages contained in the "libs" folder under the top-level directory of my project. This directory is where I store third-party jar packages.
Run "ant build!
2.3 running Problem Analysis
After the build is complete, I am happy to push the compiled jar package to the android machine for running. There is no problem in the early stage, however, when the third-party Jar package related APIs are called, the Exception occurs.
There is no compilation problem and there is a problem during the runtime. It is very likely that the third-party jar package can be found during project Compilation When the compilation problem is solved just now, however, the corresponding jar package is not packaged into the target jar package after compilation.
After some google attacks, I believe it is still a build configuration problem. "$ {sdk. dir}/tools/ant/uibuild. xml ", found that the packaging section does not see the corresponding information of the third-party jar package:
2.4 running problem solution
According to the google prompt, the final modification is as follows, and the problem is finally solved!
How can Ant package third-party jar files?
Android Project A is associated with another Project B (which can be associated with buildpath --> LinkSource) When ant is used, there is not much difference between the method and building an engineer separately. Compile Project B into a jar package (you can use Eclipse to Export the jar package directly ), put it in the libs of Project A and use ant to build the project ~
The preject library third-party package (a project, not a jar) is added to the android project. How can I package it with ant?
Android Project A is associated with another Project B (you can associate it in build path --> Link Source)
When ant is used, the method is not much different from building an engineer separately.
Compile Project B into A jar package (use Eclipse to Export the jar package directly), and put it in the libs of Project.
Use ant to go to The buildA project ~