Search in sources :

Example 1 with CompileStepBeforeRun

use of com.intellij.compiler.options.CompileStepBeforeRun in project android by JetBrains.

the class MakeBeforeRunTaskProvider method executeTask.

@Override
public boolean executeTask(DataContext context, RunConfiguration configuration, ExecutionEnvironment env, MakeBeforeRunTask task) {
    if (!AndroidProjectInfo.getInstance(myProject).requiresAndroidModel() || !isDirectGradleInvocationEnabled(myProject)) {
        CompileStepBeforeRun regularMake = new CompileStepBeforeRun(myProject);
        return regularMake.executeTask(context, configuration, env, new CompileStepBeforeRun.MakeBeforeRunTask());
    }
    AtomicReference<String> errorMsgRef = new AtomicReference<>();
    if (AndroidGradleBuildConfiguration.getInstance(myProject).SYNC_PROJECT_BEFORE_BUILD) {
        // If the model needs a sync, we need to sync "synchronously" before running.
        // See: https://code.google.com/p/android/issues/detail?id=70718
        GradleSyncState syncState = GradleSyncState.getInstance(myProject);
        if (syncState.isSyncNeeded() != ThreeState.NO) {
            GradleSyncInvoker.Request request = new GradleSyncInvoker.Request().setRunInBackground(false);
            GradleSyncInvoker.getInstance().requestProjectSync(myProject, request, new GradleSyncListener.Adapter() {

                @Override
                public void syncFailed(@NotNull Project project, @NotNull String errorMessage) {
                    errorMsgRef.set(errorMessage);
                }
            });
        }
    }
    String errorMsg = errorMsgRef.get();
    if (errorMsg != null) {
        // Sync failed. There is no point on continuing, because most likely the model is either not there, or has stale information,
        // including the path of the APK.
        LOG.info("Unable to launch '" + TASK_NAME + "' task. Project sync failed with message: " + errorMsg);
        return false;
    }
    if (myProject.isDisposed()) {
        return false;
    }
    // Some configurations (e.g. native attach) don't require a build while running the configuration
    if (configuration instanceof RunConfigurationBase && ((RunConfigurationBase) configuration).excludeCompileBeforeLaunchOption()) {
        return true;
    }
    // Note: this before run task provider may be invoked from a context such as Java unit tests, in which case it doesn't have
    // the android run config context
    AndroidRunConfigContext runConfigContext = env.getCopyableUserData(AndroidRunConfigContext.KEY);
    DeviceFutures deviceFutures = runConfigContext == null ? null : runConfigContext.getTargetDevices();
    List<AndroidDevice> targetDevices = deviceFutures == null ? Collections.emptyList() : deviceFutures.getDevices();
    List<String> cmdLineArgs = getCommonArguments(configuration, targetDevices);
    BeforeRunBuilder builder = createBuilder(env, getModules(myProject, context, configuration), configuration, runConfigContext, task.getGoal());
    try {
        boolean success = builder.build(GradleTaskRunner.newRunner(myProject), cmdLineArgs);
        LOG.info("Gradle invocation complete, success = " + success);
        return success;
    } catch (InvocationTargetException e) {
        LOG.info("Unexpected error while launching gradle before run tasks", e);
        return false;
    } catch (InterruptedException e) {
        LOG.info("Interrupted while launching gradle before run tasks");
        Thread.currentThread().interrupt();
        return false;
    }
}
Also used : GradleSyncInvoker(com.android.tools.idea.gradle.project.sync.GradleSyncInvoker) AtomicReference(java.util.concurrent.atomic.AtomicReference) InvocationTargetException(java.lang.reflect.InvocationTargetException) GradleSyncState(com.android.tools.idea.gradle.project.sync.GradleSyncState) RunConfigurationBase(com.intellij.execution.configurations.RunConfigurationBase) AndroidProject(com.android.builder.model.AndroidProject) Project(com.intellij.openapi.project.Project) ApkProjects.isApkProject(com.android.tools.idea.apk.ApkProjects.isApkProject) GradleSyncListener(com.android.tools.idea.gradle.project.sync.GradleSyncListener) CompileStepBeforeRun(com.intellij.compiler.options.CompileStepBeforeRun)

Aggregations

AndroidProject (com.android.builder.model.AndroidProject)1 ApkProjects.isApkProject (com.android.tools.idea.apk.ApkProjects.isApkProject)1 GradleSyncInvoker (com.android.tools.idea.gradle.project.sync.GradleSyncInvoker)1 GradleSyncListener (com.android.tools.idea.gradle.project.sync.GradleSyncListener)1 GradleSyncState (com.android.tools.idea.gradle.project.sync.GradleSyncState)1 CompileStepBeforeRun (com.intellij.compiler.options.CompileStepBeforeRun)1 RunConfigurationBase (com.intellij.execution.configurations.RunConfigurationBase)1 Project (com.intellij.openapi.project.Project)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1