@Test
public void rewriteManifestPaths2() throws Exception {

  // Like testRewriteManifestPaths1, but with a source comment at the end of the file
  // (which happens when there is only a single manifest, so no files are merged and it's a straight
  // file copy in the gradle plugin)
  File tempDir = Files.createTempDir();
  sourceFile = new File(tempDir, ANDROID_MANIFEST_XML);
  sourceFilePath = sourceFile.getAbsolutePath();
  File source = new File(tempDir, "real-manifest.xml");
  Files.write("<<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
              "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
              "    package=\"com.example.app\"\n" +
              "    android:versionCode=\"1\"\n" +
              "    android:versionName=\"1.0\" >\n" +
              "\n" +
              "    <uses-sdk\n" +
              "        android:minSdkVersion=\"7\"\n" +
              "        android:targetSdkVersion=\"19\" />\n" +
              "\n" +
              "    <application\n" +
              "        android:allowBackup=\"true\"\n" +
              "        android:icon=\"@drawable/ic_xlauncher\"\n" +
              "        android:label=\"@string/app_name\"\n" +
              "        android:theme=\"@style/AppTheme\" >\n" +
              "        <activity\n" +
              "            android:name=\"com.example.app.MainActivity\"\n" +
              "            android:label=\"@string/app_name\" >\n" +
              "            <intent-filter>\n" +
              "                <action android:name=\"android.intent.action.MAIN\" />\n" +
              "\n" +
              "                <category android:name=\"android.intent.category.LAUNCHER\" />\n" +
              "            </intent-filter>\n" +
              "        </activity>\n" +
              "    </application>\n" +
              "\n" +
              "</manifest>\n", source, StandardCharsets.UTF_8);
  source.deleteOnExit();
  Files.write("<<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
              "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
              "    package=\"com.example.app\"\n" +
              "    android:versionCode=\"1\"\n" +
              "    android:versionName=\"1.0\" >\n" +
              "\n" +
              "    <uses-sdk\n" +
              "        android:minSdkVersion=\"7\"\n" +
              "        android:targetSdkVersion=\"19\" />\n" +
              "\n" +
              "    <application\n" +
              "        android:allowBackup=\"true\"\n" +
              "        android:icon=\"@drawable/ic_xlauncher\"\n" +
              "        android:label=\"@string/app_name\"\n" +
              "        android:theme=\"@style/AppTheme\" >\n" +
              "        <activity\n" +
              "            android:name=\"com.example.app.MainActivity\"\n" +
              "            android:label=\"@string/app_name\" >\n" +
              "            <intent-filter>\n" +
              "                <action android:name=\"android.intent.action.MAIN\" />\n" +
              "\n" +
              "                <category android:name=\"android.intent.category.LAUNCHER\" />\n" +
              "            </intent-filter>\n" +
              "        </activity>\n" +
              "    </application>\n" +
              "\n" +
              "</manifest><!-- " + createPathComment(source, false) + " -->", sourceFile, StandardCharsets.UTF_8);
  String output =
    "Relying on packaging to define the extension of the main artifact has been deprecated and is scheduled to be removed in Gradle 2.0\n" +
    ":App:compileDefaultFlavorDebugNdk UP-TO-DATE\n" +
    ":App:preBuild UP-TO-DATE\n" +
    ":App:preDefaultFlavorDebugBuild UP-TO-DATE\n" +
    ":App:preDefaultFlavorReleaseBuild UP-TO-DATE\n" +
    ":App:prepareComAndroidSupportAppcompatV71900Library UP-TO-DATE\n" +
    ":App:prepareDefaultFlavorDebugDependencies\n" +
    ":App:compileDefaultFlavorDebugAidl UP-TO-DATE\n" +
    ":App:compileDefaultFlavorDebugRenderscript UP-TO-DATE\n" +
    ":App:generateDefaultFlavorDebugBuildConfig UP-TO-DATE\n" +
    ":App:mergeDefaultFlavorDebugAssets UP-TO-DATE\n" +
    ":App:mergeDefaultFlavorDebugResources UP-TO-DATE\n" +
    ":App:processDefaultFlavorDebugManifest UP-TO-DATE\n" +
    ":App:processDefaultFlavorDebugResources\n" +
    sourceFilePath + ":7: error: Error: No resource found that matches the given name (at 'icon' with value '@drawable/ic_xlauncher').\n" +
    ":App:processDefaultFlavorDebugResources FAILED\n" +
    "\n" +
    "FAILURE: Build failed with an exception.\n" +
    "\n" +
    "* What went wrong:\n" +
    "Execution failed for task ':App:processDefaultFlavorDebugResources'.\n" +
    "> com.android.ide.common.internal.LoggedErrorException: Failed to run command:\n" +
    "  \tSDK/build-tools/android-4.4/aapt package -f --no-crunch " +
    "-I SDK/platforms/android-19/android.jar " +
    "-M PROJECT/App/build/manifests/defaultFlavor/debug/AndroidManifest.xml " +
    "-S PROJECT/App/build/res/all/defaultFlavor/debug " +
    "-A PROJECT/App/build/assets/defaultFlavor/debug " +
    "-m -J PROJECT/App/build/source/r/defaultFlavor/debug " +
    "-F PROJECT/App/build/libs/App-defaultFlavor-debug.ap_ " +
    "--debug-mode --custom-package com.example.app " +
    "--output-text-symbols PROJECT/App/build/symbols/defaultFlavor/debug\n" +
    "  Error Code:\n" +
    "  \t1\n" +
    "  Output:\n" +
    "  \t" + sourceFilePath + ":7: error: Error: No resource found that matches the given name (at 'icon' with value '@drawable/ic_xlauncher').\n" +
    "\n" +
    "\n" +
    "* Try:\n" +
    "Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.\n" +
    "\n" +
    "BUILD FAILED\n" +
    "\n" +
    "Total time: 7.024 secs";
  assertEquals("0: Simple::App:compileDefaultFlavorDebugNdk UP-TO-DATE\n" +
               "1: Simple::App:preBuild UP-TO-DATE\n" +
               "2: Simple::App:preDefaultFlavorDebugBuild UP-TO-DATE\n" +
               "3: Simple::App:preDefaultFlavorReleaseBuild UP-TO-DATE\n" +
               "4: Simple::App:prepareComAndroidSupportAppcompatV71900Library UP-TO-DATE\n" +
               "5: Simple::App:prepareDefaultFlavorDebugDependencies\n" +
               "6: Simple::App:compileDefaultFlavorDebugAidl UP-TO-DATE\n" +
               "7: Simple::App:compileDefaultFlavorDebugRenderscript UP-TO-DATE\n" +
               "8: Simple::App:generateDefaultFlavorDebugBuildConfig UP-TO-DATE\n" +
               "9: Simple::App:mergeDefaultFlavorDebugAssets UP-TO-DATE\n" +
               "10: Simple::App:mergeDefaultFlavorDebugResources UP-TO-DATE\n" +
               "11: Simple::App:processDefaultFlavorDebugManifest UP-TO-DATE\n" +
               "12: Simple::App:processDefaultFlavorDebugResources\n" +
               "13: Error:No resource found that matches the given name (at 'icon' with value '@drawable/ic_xlauncher').\n" +
               "\t" + source.getPath() + ":13:23\n" +
               "14: Simple::App:processDefaultFlavorDebugResources FAILED\n" +
               "15: Error:Execution failed for task ':App:processDefaultFlavorDebugResources'.\n" +
               "> com.android.ide.common.internal.LoggedErrorException: Failed to run command:\n" +
               "  \tSDK/build-tools/android-4.4/aapt package -f --no-crunch -I SDK/platforms/android-19/android.jar" +
               " -M PROJECT/App/build/manifests/defaultFlavor/debug/AndroidManifest.xml" +
               " -S PROJECT/App/build/res/all/defaultFlavor/debug" +
               " -A PROJECT/App/build/assets/defaultFlavor/debug" +
               " -m -J PROJECT/App/build/source/r/defaultFlavor/debug" +
               " -F PROJECT/App/build/libs/App-defaultFlavor-debug.ap_" +
               " --debug-mode --custom-package com.example.app" +
               " --output-text-symbols PROJECT/App/build/symbols/defaultFlavor/debug\n" +
               "  Error Code:\n" +
               "  \t1\n" +
               "  Output:\n" +
               "  \t" + sourceFilePath + ":7: error: Error: No resource found that matches the given name (at 'icon' with value '@drawable/ic_xlauncher').\n" +
               "16: Info:BUILD FAILED\n" +
               "17: Info:Total time: 7.024 secs\n",
               toString(parser.parseGradleOutput(output)));
  sourceFile.delete();
  source.delete();
  tempDir.delete();
}