use of com.google.devtools.build.lib.rules.android.deployinfo.AndroidDeployInfoOuterClass.AndroidDeployInfo in project intellij by bazelbuild.
the class MobileInstallBuildStep method build.
@Override
public void build(BlazeContext context, BlazeAndroidDeviceSelector.DeviceSession deviceSession) {
BlazeProjectData projectData = BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
if (projectData == null) {
IssueOutput.error("Missing project data. Please sync and try again.").submit(context);
return;
}
DeviceFutures deviceFutures = deviceSession.deviceFutures;
if (deviceFutures == null) {
IssueOutput.error("Error fetching devices!").submit(context);
return;
}
context.output(new StatusOutput("Waiting for target device..."));
IDevice device = resolveDevice(context, deviceFutures);
if (device == null) {
return;
}
BuildInvoker invoker = Blaze.getBuildSystemProvider(project).getBuildSystem().getBuildInvoker(project, context);
BlazeCommand.Builder command = BlazeCommand.builder(invoker, BlazeCommandName.MOBILE_INSTALL);
if (passAdbArgWithSerialToMi.getValue()) {
// Redundant, but we need this to get around bug in bazel.
// https://github.com/bazelbuild/bazel/issues/4922
command.addBlazeFlags(BlazeFlags.ADB_ARG + "-s ", BlazeFlags.ADB_ARG + device.getSerialNumber());
}
if (!StudioDeployerExperiment.isEnabled()) {
MobileInstallAdbLocationProvider.getAdbLocationForMobileInstall(project).ifPresent((location) -> command.addBlazeFlags(BlazeFlags.ADB, location));
}
WorkspaceRoot workspaceRoot = WorkspaceRoot.fromProject(project);
final String deployInfoSuffix = getDeployInfoSuffix(Blaze.getBuildSystemName(project));
try (BuildResultHelper buildResultHelper = invoker.createBuildResultHelper();
AdbTunnelConfigurator tunnelConfig = getTunnelConfigurator(context)) {
tunnelConfig.setupConnection(context);
if (!StudioDeployerExperiment.isEnabled()) {
String deviceFlag = device.getSerialNumber();
if (tunnelConfig.isActive()) {
deviceFlag += ":tcp:" + tunnelConfig.getAdbServerPort();
} else {
InetSocketAddress adbAddr = AndroidDebugBridge.getSocketAddress();
if (adbAddr == null) {
IssueOutput.warn("Can't get ADB server port, please ensure ADB server is running. Will fallback" + " to the default adb server.").submit(context);
} else {
deviceFlag += ":tcp:" + adbAddr.getPort();
}
}
command.addBlazeFlags(BlazeFlags.DEVICE, deviceFlag);
}
command.addTargets(label).addBlazeFlags(blazeFlags).addBlazeFlags(buildResultHelper.getBuildFlags()).addExeFlags(exeFlags).addExeFlags("--nolaunch_app");
if (StudioDeployerExperiment.isEnabled()) {
command.addExeFlags("--nodeploy");
}
SaveUtil.saveAllFiles();
context.output(new StatusOutput("Invoking mobile-install..."));
ExternalTask task = ExternalTask.builder(workspaceRoot).addBlazeCommand(command.build()).context(context).stdout(LineProcessingOutputStream.of(new PrintOutputLineProcessor(context))).stderr(LineProcessingOutputStream.of(BlazeConsoleLineProcessorProvider.getAllStderrLineProcessors(context))).build();
Stopwatch s = Stopwatch.createStarted();
int exitCode = task.run();
logBuildTime(launchId, StudioDeployerExperiment.isEnabled(), s.elapsed(), exitCode);
if (exitCode != 0) {
IssueOutput.error("Blaze build failed. See Blaze Console for details.").submit(context);
return;
}
ListenableFuture<Void> unusedFuture = FileCaches.refresh(project, context, BlazeBuildOutputs.noOutputs(BuildResult.fromExitCode(exitCode)));
context.output(new StatusOutput("Reading deployment information..."));
String executionRoot = ExecRootUtil.getExecutionRoot(buildResultHelper, project, blazeFlags, context);
if (executionRoot == null) {
IssueOutput.error("Could not locate execroot!").submit(context);
return;
}
AndroidDeployInfo deployInfoProto = deployInfoHelper.readDeployInfoProtoForTarget(label, buildResultHelper, fileName -> fileName.endsWith(deployInfoSuffix));
deployInfo = deployInfoHelper.extractDeployInfoAndInvalidateManifests(project, new File(executionRoot), deployInfoProto);
String msg;
if (StudioDeployerExperiment.isEnabled()) {
msg = "mobile-install build completed, deploying split apks...";
} else {
msg = "Done.";
}
context.output(new StatusOutput(msg));
} catch (GetArtifactsException e) {
IssueOutput.error("Could not read BEP output: " + e.getMessage()).submit(context);
} catch (GetDeployInfoException e) {
IssueOutput.error("Could not read apk deploy info from build: " + e.getMessage()).submit(context);
}
}
use of com.google.devtools.build.lib.rules.android.deployinfo.AndroidDeployInfoOuterClass.AndroidDeployInfo in project intellij by bazelbuild.
the class BlazeInstrumentationTestApkBuildStepIntegrationTest method deployInfoBuiltCorrectly_selfInstrumentingTest.
@Test
public void deployInfoBuiltCorrectly_selfInstrumentingTest() throws GetDeployInfoException, ApkProvisionException {
setupProject();
Label testTarget = Label.create("//java/com/foo/app:self_instrumenting_test");
Label instrumentorTarget = Label.create("//java/com/foo/app:test_app_self_instrumenting");
BlazeContext context = BlazeContext.create();
ImmutableList<String> blazeFlags = ImmutableList.of("some_blaze_flag", "some_other_flag");
// Setup interceptor for fake running of blaze commands and capture details.
ExternalTaskInterceptor externalTaskInterceptor = new ExternalTaskInterceptor();
registerApplicationService(ExternalTaskProvider.class, externalTaskInterceptor);
// Return fake deploy info proto and mocked deploy info data object.
BlazeApkDeployInfoProtoHelper helper = mock(BlazeApkDeployInfoProtoHelper.class);
AndroidDeployInfo fakeInstrumentorProto = AndroidDeployInfo.newBuilder().build();
BlazeAndroidDeployInfo mockDeployInfo = mock(BlazeAndroidDeployInfo.class);
when(helper.readDeployInfoProtoForTarget(eq(instrumentorTarget), any(BuildResultHelper.class), any())).thenReturn(fakeInstrumentorProto);
when(helper.extractDeployInfoAndInvalidateManifests(eq(getProject()), eq(new File(getExecRoot())), eq(fakeInstrumentorProto))).thenReturn(mockDeployInfo);
// Perform
BlazeInstrumentationTestApkBuildStep buildStep = new BlazeInstrumentationTestApkBuildStep(getProject(), testTarget, blazeFlags, helper);
buildStep.build(context, new DeviceSession(null, null, null));
// Verify
assertThat(buildStep.getDeployInfo()).isNotNull();
assertThat(buildStep.getDeployInfo()).isEqualTo(mockDeployInfo);
assertThat(externalTaskInterceptor.context).isEqualTo(context);
assertThat(externalTaskInterceptor.command).contains(instrumentorTarget.toString());
assertThat(externalTaskInterceptor.command).contains("--output_groups=+android_deploy_info");
assertThat(externalTaskInterceptor.command).containsAllIn(blazeFlags);
}
use of com.google.devtools.build.lib.rules.android.deployinfo.AndroidDeployInfoOuterClass.AndroidDeployInfo in project intellij by bazelbuild.
the class BlazeInstrumentationTestApkBuildStepIntegrationTest method exceptionDuringDeployInfoExtraction.
@Test
public void exceptionDuringDeployInfoExtraction() throws GetDeployInfoException {
setupProject();
Label testTarget = Label.create("//java/com/foo/app:instrumentation_test");
Label instrumentorTarget = Label.create("//java/com/foo/app:test_app");
Label appTarget = Label.create("//java/com/foo/app:app");
MessageCollector messageCollector = new MessageCollector();
BlazeContext context = BlazeContext.create();
context.addOutputSink(IssueOutput.class, messageCollector);
// Make blaze command invocation always pass.
registerApplicationService(ExternalTaskProvider.class, builder -> scopes -> 0);
// Return fake deploy info proto and mocked deploy info data object.
BlazeApkDeployInfoProtoHelper helper = mock(BlazeApkDeployInfoProtoHelper.class);
AndroidDeployInfo fakeInstrumentorProto = AndroidDeployInfo.newBuilder().build();
AndroidDeployInfo fakeAppProto = AndroidDeployInfo.newBuilder().build();
when(helper.readDeployInfoProtoForTarget(eq(instrumentorTarget), any(BuildResultHelper.class), any())).thenReturn(fakeInstrumentorProto);
when(helper.readDeployInfoProtoForTarget(eq(appTarget), any(BuildResultHelper.class), any())).thenReturn(fakeAppProto);
when(helper.extractInstrumentationTestDeployInfoAndInvalidateManifests(any(), any(), any(), any())).thenThrow(new GetDeployInfoException("Fake Exception"));
// Perform
BlazeInstrumentationTestApkBuildStep buildStep = new BlazeInstrumentationTestApkBuildStep(getProject(), testTarget, ImmutableList.of(), helper);
buildStep.build(context, new DeviceSession(null, null, null));
// Verify
assertThat(context.hasErrors()).isTrue();
assertThat(messageCollector.getMessages()).contains("Could not read apk deploy info from build: Fake Exception");
}
use of com.google.devtools.build.lib.rules.android.deployinfo.AndroidDeployInfoOuterClass.AndroidDeployInfo in project intellij by bazelbuild.
the class BlazeInstrumentationTestApkBuildStepIntegrationTest method blazeCommandFailed.
@Test
public void blazeCommandFailed() throws GetDeployInfoException {
setupProject();
Label testTarget = Label.create("//java/com/foo/app:instrumentation_test");
Label instrumentorTarget = Label.create("//java/com/foo/app:test_app");
Label appTarget = Label.create("//java/com/foo/app:app");
MessageCollector messageCollector = new MessageCollector();
BlazeContext context = BlazeContext.create();
context.addOutputSink(IssueOutput.class, messageCollector);
// Return a non-zero value to indicate blaze command run failure.
registerApplicationService(ExternalTaskProvider.class, builder -> scopes -> 1337);
// Return fake deploy info proto and mocked deploy info data object.
BlazeApkDeployInfoProtoHelper helper = mock(BlazeApkDeployInfoProtoHelper.class);
AndroidDeployInfo fakeInstrumentorProto = AndroidDeployInfo.newBuilder().build();
AndroidDeployInfo fakeAppProto = AndroidDeployInfo.newBuilder().build();
BlazeAndroidDeployInfo mockDeployInfo = mock(BlazeAndroidDeployInfo.class);
when(helper.readDeployInfoProtoForTarget(eq(instrumentorTarget), any(BuildResultHelper.class), any())).thenReturn(fakeInstrumentorProto);
when(helper.readDeployInfoProtoForTarget(eq(appTarget), any(BuildResultHelper.class), any())).thenReturn(fakeAppProto);
when(helper.extractInstrumentationTestDeployInfoAndInvalidateManifests(eq(getProject()), eq(new File(getExecRoot())), eq(fakeInstrumentorProto), eq(fakeAppProto))).thenReturn(mockDeployInfo);
// Perform
BlazeInstrumentationTestApkBuildStep buildStep = new BlazeInstrumentationTestApkBuildStep(getProject(), testTarget, ImmutableList.of(), helper);
buildStep.build(context, new DeviceSession(null, null, null));
// Verify
assertThat(context.hasErrors()).isTrue();
assertThat(messageCollector.getMessages()).contains("Blaze build failed. See Blaze Console for details.");
}
use of com.google.devtools.build.lib.rules.android.deployinfo.AndroidDeployInfoOuterClass.AndroidDeployInfo in project intellij by bazelbuild.
the class FullApkBuildStepIntegrationTest method build_withRemoteApkButDisabledRemoteApkFetching.
@Test
public void build_withRemoteApkButDisabledRemoteApkFetching() throws Exception {
// Setup remote APK downloader for ensuring the download method is called
RemoteApkDownloader mockDownloader = mock(RemoteApkDownloader.class);
when(mockDownloader.canDownload(any())).thenReturn(true);
registerExtension(RemoteApkDownloader.EP_NAME, mockDownloader);
// Disable remote APK fetching
MockExperimentService mockExperimentService = new MockExperimentService();
mockExperimentService.setExperiment(FullApkBuildStep.FETCH_REMOTE_APKS, false);
ServiceHelper.registerApplicationComponent(ExperimentService.class, mockExperimentService, getTestRootDisposable());
// Return fake deploy info proto and mocked deploy info data object.
BlazeAndroidDeployInfo mockDeployInfo = mock(BlazeAndroidDeployInfo.class);
File apkFile = new File("/path/to/apk");
when(mockDeployInfo.getApksToDeploy()).thenReturn(ImmutableList.of(apkFile));
BlazeApkDeployInfoProtoHelper helper = mock(BlazeApkDeployInfoProtoHelper.class);
AndroidDeployInfo fakeProto = AndroidDeployInfo.newBuilder().build();
when(helper.readDeployInfoProtoForTarget(eq(buildTarget), any(BuildResultHelper.class), any())).thenReturn(fakeProto);
when(helper.extractDeployInfoAndInvalidateManifests(eq(getProject()), eq(new File(getExecRoot())), eq(fakeProto))).thenReturn(mockDeployInfo);
// Perform
FullApkBuildStep buildStep = new FullApkBuildStep(getProject(), buildTarget, blazeFlags, helper);
buildStep.build(context, new DeviceSession(null, null, null));
// Verify
assertThat(buildStep.getDeployInfo()).isNotNull();
assertThat(buildStep.getDeployInfo().getApksToDeploy()).containsExactly(apkFile);
assertThat(externalTaskInterceptor.command).contains(buildTarget.toString());
assertThat(externalTaskInterceptor.command).contains("--output_groups=+android_deploy_info");
assertThat(externalTaskInterceptor.command).containsAllIn(blazeFlags);
verify(mockDownloader, times(0)).download(any(), any());
}
Aggregations