private static boolean packageResources(
      @NotNull AndroidFacet facet, @NotNull CompileContext context) {
    final Module module = facet.getModule();

    try {
      context.processMessage(
          new ProgressMessage(
              AndroidJpsBundle.message(
                  "android.jps.progress.packaging.resources", module.getName())));

      final File manifestFile = AndroidJpsUtil.getManifestFileForCompilationPath(facet);
      if (manifestFile == null) {
        context.processMessage(
            new CompilerMessage(
                BUILDER_NAME,
                BuildMessage.Kind.ERROR,
                AndroidJpsBundle.message(
                    "android.jps.errors.manifest.not.found", module.getName())));
        return false;
      }
      final ArrayList<String> assetsDirPaths = new ArrayList<String>();
      collectAssetDirs(facet, assetsDirPaths);

      final File outputDir =
          AndroidJpsUtil.getOutputDirectoryForPackagedFiles(context.getProjectPaths(), module);
      if (outputDir == null) {
        context.processMessage(
            new CompilerMessage(
                BUILDER_NAME,
                BuildMessage.Kind.ERROR,
                AndroidJpsBundle.message(
                    "android.jps.errors.output.dir.not.specified", module.getName())));
        return false;
      }

      final Pair<AndroidSdk, IAndroidTarget> pair =
          AndroidJpsUtil.getAndroidPlatform(module, context, BUILDER_NAME);
      if (pair == null) {
        return false;
      }
      final IAndroidTarget target = pair.getSecond();

      final String outputFilePath = getPackagedResourcesFile(module, outputDir).getPath();
      final String[] resourceDirPaths =
          AndroidJpsUtil.collectResourceDirsForCompilation(facet, true, context);

      return doPackageResources(
          context,
          manifestFile,
          target,
          resourceDirPaths,
          ArrayUtil.toStringArray(assetsDirPaths),
          outputFilePath,
          AndroidJpsUtil.isReleaseBuild(context));
    } catch (IOException e) {
      AndroidJpsUtil.reportExceptionError(context, null, e, BUILDER_NAME);
      return false;
    }
  }
  private static boolean runPngCaching(
      @NotNull CompileContext context,
      @NotNull Module module,
      @NotNull AndroidFileSetStorage storage,
      @Nullable AndroidFileSetState state)
      throws IOException {
    final AndroidFileSetState savedState = storage.getState(module.getName());
    if (context.isMake() && savedState != null && savedState.equalsTo(state)) {
      return true;
    }

    final AndroidFacet facet = AndroidJpsUtil.getFacet(module);
    if (facet == null) {
      return true;
    }

    context.processMessage(
        new CompilerMessage(
            BUILDER_NAME,
            BuildMessage.Kind.INFO,
            AndroidJpsBundle.message("android.jps.progress.res.caching", module.getName())));

    final File resourceDir = AndroidJpsUtil.getResourceDirForCompilationPath(facet);
    if (resourceDir == null) {
      return true;
    }

    final Pair<AndroidSdk, IAndroidTarget> pair =
        AndroidJpsUtil.getAndroidPlatform(module, context, BUILDER_NAME);
    if (pair == null) {
      return false;
    }

    final File resCacheDir = AndroidJpsUtil.getResourcesCacheDir(context, module);

    if (!resCacheDir.exists()) {
      if (!resCacheDir.mkdirs()) {
        context.processMessage(
            new CompilerMessage(
                BUILDER_NAME,
                BuildMessage.Kind.ERROR,
                "Cannot create directory " + resCacheDir.getPath()));
        return false;
      }
    }

    final IAndroidTarget target = pair.second;

    final Map<AndroidCompilerMessageKind, List<String>> messages =
        AndroidApt.crunch(
            target, Collections.singletonList(resourceDir.getPath()), resCacheDir.getPath());

    AndroidJpsUtil.addMessages(context, messages, BUILDER_NAME);

    final boolean success = messages.get(AndroidCompilerMessageKind.ERROR).isEmpty();
    storage.update(module.getName(), success ? state : null);
    return success;
  }
  private static boolean doPackagingForModule(
      @NotNull CompileContext context,
      @NotNull Module module,
      @NotNull AndroidFileSetStorage apkFileSetStorage,
      @NotNull AndroidApkBuilderConfigStateStorage apkBuilderConfigStateStorage,
      boolean release)
      throws IOException {
    final AndroidFacet facet = AndroidJpsUtil.getFacet(module);
    if (facet == null || facet.isLibrary()) {
      return true;
    }

    final String[] sourceRoots =
        AndroidJpsUtil.toPaths(AndroidJpsUtil.getSourceRootsForModuleAndDependencies(module));
    final ProjectPaths paths = context.getProjectPaths();

    final File outputDir = AndroidJpsUtil.getOutputDirectoryForPackagedFiles(paths, module);
    if (outputDir == null) {
      context.processMessage(
          new CompilerMessage(
              BUILDER_NAME,
              BuildMessage.Kind.ERROR,
              AndroidJpsBundle.message(
                  "android.jps.errors.output.dir.not.specified", module.getName())));
      return false;
    }

    final Pair<AndroidSdk, IAndroidTarget> pair =
        AndroidJpsUtil.getAndroidPlatform(module, context, BUILDER_NAME);
    if (pair == null) {
      return false;
    }

    final Set<String> externalJarsSet = AndroidJpsUtil.getExternalLibraries(paths, module);
    final File resPackage = getPackagedResourcesFile(module, outputDir);

    final File classesDexFile = new File(outputDir.getPath(), AndroidCommonUtils.CLASSES_FILE_NAME);

    final String sdkPath = pair.getFirst().getSdkPath();
    final String outputPath = AndroidJpsUtil.getApkPath(facet, outputDir);
    if (outputPath == null) {
      context.processMessage(
          new CompilerMessage(
              BUILDER_NAME,
              BuildMessage.Kind.ERROR,
              "Cannot compute output path for file " + AndroidJpsUtil.getApkName(module)));
      return false;
    }
    final String customKeyStorePath =
        FileUtil.toSystemDependentName(facet.getCustomDebugKeyStorePath());
    final String[] nativeLibDirs = collectNativeLibsFolders(facet);

    final String resPackagePath =
        release ? resPackage.getPath() + RELEASE_SUFFIX : resPackage.getPath();
    final String outputApkPath = release ? outputPath + UNSIGNED_SUFFIX : outputPath;
    final String classesDexFilePath = classesDexFile.getPath();
    final String[] externalJars = ArrayUtil.toStringArray(externalJarsSet);

    final AndroidFileSetState currentFileSetState =
        buildCurrentApkBuilderState(
            context.getProject(),
            resPackagePath,
            classesDexFilePath,
            nativeLibDirs,
            sourceRoots,
            externalJars,
            release);

    final AndroidApkBuilderConfigState currentApkBuilderConfigState =
        new AndroidApkBuilderConfigState(outputApkPath, customKeyStorePath);

    final AndroidFileSetState savedApkFileSetState = apkFileSetStorage.getState(module.getName());
    final AndroidApkBuilderConfigState savedApkBuilderConfigState =
        apkBuilderConfigStateStorage.getState(module.getName());

    if (context.isMake()
        && currentFileSetState.equalsTo(savedApkFileSetState)
        && currentApkBuilderConfigState.equalsTo(savedApkBuilderConfigState)) {
      return true;
    }
    context.processMessage(
        new ProgressMessage(
            AndroidJpsBundle.message(
                "android.jps.progress.packaging", AndroidJpsUtil.getApkName(module))));

    final Map<AndroidCompilerMessageKind, List<String>> messages =
        AndroidApkBuilder.execute(
            resPackagePath,
            classesDexFilePath,
            sourceRoots,
            externalJars,
            nativeLibDirs,
            outputApkPath,
            release,
            sdkPath,
            customKeyStorePath,
            new MyExcludedSourcesFilter(context.getProject()));

    AndroidJpsUtil.addMessages(context, messages, BUILDER_NAME);
    final boolean success = messages.get(AndroidCompilerMessageKind.ERROR).isEmpty();

    apkFileSetStorage.update(module.getName(), success ? currentFileSetState : null);
    apkBuilderConfigStateStorage.update(
        module.getName(), success ? currentApkBuilderConfigState : null);
    return success;
  }