Skip to content

Commit 5f1cd97

Browse files
authored
Merge pull request #68 from richaArora08/master
Let Users pass dynamic path to read main and test apks
2 parents 0ff4095 + a9c95be commit 5f1cd97

4 files changed

Lines changed: 344 additions & 13 deletions

File tree

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
id 'com.gradle.plugin-publish' version '0.11.0'
55
}
66

7-
version '3.1.2'
7+
version '3.1.3'
88

99
pluginBundle {
1010
website = 'https://www.browserstack.com'
@@ -17,7 +17,7 @@ pluginBundle {
1717
description = 'Runs Espresso tests on BrowserStack'
1818
tags = ['espresso', 'test', 'browserstack', 'app', 'automate',
1919
'app-automate', 'appautomate', 'app-live', 'applive']
20-
version = '3.1.2'
20+
version = '3.1.3'
2121
}
2222
}
2323
}

src/main/java/com/browserstack/gradle/BrowserStackPlugin.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void apply(Project project) {
4141
task.setGroup(DEFAULT_GROUP);
4242
task.setDescription("Uploads app / tests to AppAutomate and executes them");
4343
// Run Espresso tests without building the apk and test apk
44-
if (!project.hasProperty("skipBuildingApks")) {
44+
if (!project.hasProperty("skipBuildingApks") || Boolean.parseBoolean(project.property("skipBuildingApks").toString()) == false) {
4545
task.dependsOn("assemble" + appVariantName, "assemble" + appVariantName + "AndroidTest");
4646
}
4747
task.setAppVariantBaseName(applicationVariant.getBaseName());
@@ -51,6 +51,12 @@ public void apply(Project project) {
5151
task.setConfigFilePath(browserStackConfigExtension.getConfigFilePath());
5252
task.setHost(Constants.BROWSERSTACK_API_HOST);
5353
task.setDebug(browserStackConfigExtension.isDebug());
54+
if(project.hasProperty("mainAPKPath")){
55+
task.setMainAPKPath(project.property("mainAPKPath").toString());
56+
}
57+
if(project.hasProperty("testAPKPath")){
58+
task.setTestAPKPath(project.property("testAPKPath").toString());
59+
}
5460
});
5561

5662
project.getTasks().create("upload" + appVariantName + "ToBrowserstackAppLive", AppLiveUploadTask.class, (task) -> {

src/main/java/com/browserstack/gradle/BrowserStackTask.java

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.io.IOException;
77
import java.net.HttpURLConnection;
88
import java.nio.file.Files;
9+
import java.nio.file.NoSuchFileException;
910
import java.nio.file.Path;
1011
import java.nio.file.Paths;
1112
import 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

Comments
 (0)