66import java .io .IOException ;
77import java .net .HttpURLConnection ;
88import java .nio .file .Files ;
9+ import java .nio .file .NoSuchFileException ;
910import java .nio .file .Path ;
1011import java .nio .file .Paths ;
1112import java .nio .file .attribute .BasicFileAttributes ;
@@ -40,6 +41,14 @@ public class BrowserStackTask extends DefaultTask {
4041 @ Optional
4142 public String command ;
4243
44+ @ Input
45+ @ Optional
46+ public String mainAPKPath ;
47+
48+ @ Input
49+ @ Optional
50+ public String testAPKPath ;
51+
4352 public void setAppVariantBaseName (String appVariantBaseName ) {
4453 this .appVariantBaseName = appVariantBaseName ;
4554 }
@@ -80,6 +89,14 @@ public void setHost(String host) {
8089
8190 public void setCommand (String command ) { this .command = command ; }
8291
92+ public String getMainAPKPath () { return mainAPKPath ; }
93+
94+ public void setMainAPKPath (String mainAPKPath ) { this .mainAPKPath = mainAPKPath ; }
95+
96+ public String getTestAPKPath () {return testAPKPath ; }
97+
98+ public void setTestAPKPath (String testAPKPath ) { this .testAPKPath = testAPKPath ; }
99+
83100 protected JSONObject constructDefaultBuildParams () { JSONObject params = new JSONObject ();
84101
85102 params .put ("app" , app );
@@ -152,21 +169,60 @@ public static Path findMostRecentPath(List<Path> paths) {
152169 return mostRecentPath ;
153170 }
154171
172+ private boolean isPathRelative (String apkPath ){
173+ if (apkPath .startsWith ("./" )){
174+ return true ;
175+ }
176+ return false ;
177+ }
178+ private String getAbsolutePath (String apkPath , String currentWorkingDirectory ){
179+ if (isPathRelative (apkPath )){
180+ return currentWorkingDirectory + apkPath .substring (1 );
181+ }
182+ return apkPath ;
183+ }
155184 public Map <String , Path > locateApks (boolean ignoreTestPath ) throws IOException {
156185 Path debugApkPath ;
157186 Path testApkPath ;
158187 String dir = System .getProperty ("user.dir" );
159188 List <Path > appApkFiles = new ArrayList <>();
160189 List <Path > testApkFiles = new ArrayList <>();
161- Files .find (Paths .get (dir ), Constants .APP_SEARCH_MAX_DEPTH , (filePath , fileAttr ) -> isValidFile (filePath , fileAttr ))
162- .forEach (f -> {
163-
164- if (f .toString ().endsWith ("-androidTest.apk" )) {
165- testApkFiles .add (f );
166- } else {
167- appApkFiles .add (f );
168- }
169- });
190+ final Boolean [] isAPKFileCreated = {false ,false }; // 1st element stores true if main apk is read from path provided by client and false otherwise. 2nd element is for test apk.
191+ if (mainAPKPath != null ){
192+ isAPKFileCreated [0 ] = true ;
193+ try {
194+ Files .find (Paths .get (getAbsolutePath (mainAPKPath , dir )), 1 , (filePath , fileAttr ) -> isValidAPKFile (filePath , fileAttr ))
195+ .forEach (f -> {
196+ appApkFiles .add (f );
197+ });
198+ }catch (NoSuchFileException e ){
199+ throw new IOException ("Invalid File Path: Please provide a valid main APK path" );
200+ }
201+ }
202+ if (testAPKPath != null ){
203+ isAPKFileCreated [1 ] = true ;
204+ try {
205+ Files .find (Paths .get (getAbsolutePath (testAPKPath , dir )), 1 , (filePath , fileAttr ) -> isValidAPKFile (filePath , fileAttr ))
206+ .forEach (f -> {
207+ testApkFiles .add (f );
208+ });
209+ }catch (NoSuchFileException e ){
210+ throw new IOException ("Invalid File Path: Please provide a valid test APK path" );
211+ }
212+ }
213+
214+ if (!isAPKFileCreated [0 ] || !isAPKFileCreated [1 ]) {
215+ Files .find (Paths .get (dir ), Constants .APP_SEARCH_MAX_DEPTH , (filePath , fileAttr ) -> isValidFile (filePath , fileAttr ))
216+ .forEach (f -> {
217+ if (f .toString ().endsWith ("-androidTest.apk" )) {
218+ if (!isAPKFileCreated [1 ]) {
219+ testApkFiles .add (f );
220+ }
221+ } else if (!isAPKFileCreated [0 ]) {
222+ appApkFiles .add (f );
223+ }
224+ });
225+ }
170226 debugApkPath = findMostRecentPath (appApkFiles );
171227 testApkPath = findMostRecentPath (testApkFiles );
172228
@@ -188,7 +244,10 @@ public Map<String, Path> locateApks(boolean ignoreTestPath) throws IOException {
188244 }
189245
190246 private boolean isValidFile (Path filePath , BasicFileAttributes fileAttr ) {
191- return fileAttr . isRegularFile () && filePath . toString (). endsWith ( ".apk" ) && filePath .getFileName ().toString ()
247+ return isValidAPKFile ( filePath , fileAttr ) && filePath .getFileName ().toString ()
192248 .contains (appVariantBaseName );
193249 }
250+ private boolean isValidAPKFile (Path filePath , BasicFileAttributes fileAttr ) {
251+ return fileAttr .isRegularFile () && filePath .toString ().endsWith (".apk" ) ;
252+ }
194253}
0 commit comments