Merge tag android-5.1.0_r1 into AOSP_5.1_MERGE

Change-Id: I8663cfecf8bc6a68337b44df489e0e0641dbef74
diff --git a/build.gradle b/build.gradle
new file mode 100644
index 0000000..f8306d5
--- /dev/null
+++ b/build.gradle
@@ -0,0 +1,159 @@
+buildscript {
+    repositories {
+        maven { url '../../prebuilts/gradle-plugin' }
+        maven { url '../../prebuilts/tools/common/m2/repository' }
+        maven { url '../../prebuilts/tools/common/m2/internal' }
+    }
+    dependencies {
+        classpath 'com.android.tools.build:gradle:0.10.0'
+    }
+}
+
+ext.supportVersion = '1.0.1'
+ext.extraVersion = 10
+ext.supportRepoOut = ''
+ext.buildToolsVersion = '19.0.3'
+
+/*
+ * With the build server you are given two env variables.
+ * The OUT_DIR is a temporary directory you can use to put things during the build.
+ * The DIST_DIR is where you want to save things from the build.
+ *
+ * The build server will copy the contents of DIST_DIR to somewhere and make it available.
+ */
+if (System.env.DIST_DIR != null && System.env.OUT_DIR != null) {
+    buildDir = new File(System.env.OUT_DIR + '/gradle/frameworks/support/build').getCanonicalFile()
+    project.ext.distDir = new File(System.env.DIST_DIR).getCanonicalFile()
+} else {
+    buildDir = file('../../out/host/gradle/frameworks/support/build')
+    project.ext.distDir = file('../../out/dist')
+}
+
+ext.supportRepoOut = new File(buildDir, 'support_repo')
+
+// Main task called by the build server.
+task(createArchive) << {
+}
+
+// upload anchor for subprojects to upload their artifacts
+// to the local repo.
+task(mainUpload) << {
+}
+
+// repository creation task
+task createRepository(type: Zip, dependsOn: mainUpload) {
+    from project.ext.supportRepoOut
+    destinationDir project.ext.distDir
+    into 'm2repository'
+    baseName = String.format("android_m2repository_r%02d", project.ext.extraVersion)
+}
+createArchive.dependsOn createRepository
+
+// prepare repository with older versions
+task unzipRepo(type: Copy) {
+    from "$rootDir/../../prebuilts/maven_repo/android"
+    into project.ext.supportRepoOut
+}
+
+unzipRepo.doFirst {
+    project.ext.supportRepoOut.deleteDir()
+    project.ext.supportRepoOut.mkdirs()
+}
+
+// anchor for prepare repo. This is post unzip + sourceProp.
+task(prepareRepo) << {
+}
+
+import com.google.common.io.Files
+import com.google.common.base.Charsets
+
+task(createXml) << {
+    def repoArchive = createRepository.archivePath
+    def repoArchiveName = createRepository.archiveName
+    def size = repoArchive.length()
+    def sha1 = getSha1(repoArchive)
+
+    def xml =
+"<sdk:sdk-addon xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:sdk=\"http://schemas.android.com/sdk/android/addon/6\">\n\
+  <sdk:extra>\n\
+    <sdk:revision>\n\
+      <sdk:major>${project.ext.extraVersion}</sdk:major>\n\
+    </sdk:revision>\n\
+    <sdk:vendor-display>Android</sdk:vendor-display>\n\
+    <sdk:vendor-id>android</sdk:vendor-id>\n\
+    <sdk:name-display>Local Maven repository for Support Libraries</sdk:name-display>\n\
+    <sdk:path>m2repository</sdk:path>\n\
+    <sdk:archives>\n\
+      <sdk:archive os=\"any\" arch=\"any\">\n\
+       <sdk:size>${size}</sdk:size>\n\
+       <sdk:checksum type=\"sha1\">${sha1}</sdk:checksum>\n\
+       <sdk:url>${repoArchiveName}</sdk:url>\n\
+      </sdk:archive>\n\
+    </sdk:archives>\n\
+  </sdk:extra>\n\
+</sdk:sdk-addon>"
+
+    Files.write(xml, new File(project.ext.distDir, 'repo-extras.xml'), Charsets.UTF_8)
+}
+createArchive.dependsOn createXml
+
+task(createSourceProp) << {
+    def sourceProp =
+"Extra.VendorDisplay=Android\n\
+Extra.Path=m2repository\n\
+Archive.Arch=ANY\n\
+Extra.NameDisplay=Android Support Repository\n\
+Archive.Os=ANY\n\
+Pkg.Revision=${project.ext.extraVersion}.0.0\n\
+Extra.VendorId=android"
+
+    Files.write(sourceProp, new File(project.ext.supportRepoOut, 'source.properties'), Charsets.UTF_8)
+}
+createSourceProp.dependsOn unzipRepo
+prepareRepo.dependsOn createSourceProp
+
+
+import com.google.common.hash.HashCode
+import com.google.common.hash.HashFunction
+import com.google.common.hash.Hashing
+
+def getSha1(File inputFile) {
+    HashFunction hashFunction = Hashing.sha1()
+    HashCode hashCode = hashFunction.hashString(inputFile.getAbsolutePath())
+    return hashCode.toString()
+}
+
+subprojects {
+    // Change buildDir first so that all plugins pick up the new value.
+    project.buildDir = project.file("$project.parent.buildDir/../$project.name/build")
+
+    apply plugin: 'maven'
+
+    version = rootProject.ext.supportVersion
+    group = 'com.android.support'
+
+    task release(type: Upload) {
+        configuration = configurations.archives
+        repositories {
+            mavenDeployer {
+                repository(url: uri("$rootProject.ext.supportRepoOut"))
+            }
+        }
+    }
+
+    // before the upload, make sure the repo is ready.
+    release.dependsOn rootProject.tasks.prepareRepo
+    // make the mainupload depend on this one.
+    mainUpload.dependsOn release
+
+    project.plugins.whenPluginAdded { plugin ->
+        if ("com.android.build.gradle.LibraryPlugin".equals(plugin.class.name)) {
+            project.android.buildToolsVersion = rootProject.buildToolsVersion
+        }
+    }
+}
+
+FileCollection getAndroidPrebuilt(String apiLevel) {
+    files("$rootDir/../../prebuilts/sdk/$apiLevel/android.jar")
+}
+
diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar
new file mode 100644
index 0000000..3c7abdf
--- /dev/null
+++ b/gradle/wrapper/gradle-wrapper.jar
Binary files differ
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
new file mode 100644
index 0000000..127125b
--- /dev/null
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -0,0 +1,6 @@
+#Tue Mar 18 16:49:12 PDT 2014
+distributionBase=GRADLE_USER_HOME
+distributionPath=wrapper/dists
+zipStoreBase=GRADLE_USER_HOME
+zipStorePath=wrapper/dists
+distributionUrl=../../../../tools/external/gradle/gradle-1.11-bin.zip
diff --git a/gradlew b/gradlew
new file mode 100755
index 0000000..89509d7
--- /dev/null
+++ b/gradlew
@@ -0,0 +1,174 @@
+#!/usr/bin/env bash
+
+##############################################################################
+##
+##  Gradle start up script for UN*X
+##
+##############################################################################
+
+# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
+DEFAULT_JVM_OPTS=""
+
+APP_NAME="Gradle"
+APP_BASE_NAME=`basename "$0"`
+
+# Use the maximum available, or set MAX_FD != -1 to use that value.
+MAX_FD="maximum"
+
+warn ( ) {
+    echo "$*"
+}
+
+die ( ) {
+    echo
+    echo "$*"
+    echo
+    exit 1
+}
+
+# OS specific support (must be 'true' or 'false').
+cygwin=false
+msys=false
+darwin=false
+case "`uname`" in
+  CYGWIN* )
+    cygwin=true
+    ;;
+  Darwin* )
+    darwin=true
+    ;;
+  MINGW* )
+    msys=true
+    ;;
+esac
+
+# For Cygwin, ensure paths are in UNIX format before anything is touched.
+if $cygwin ; then
+    [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
+fi
+
+# Attempt to set APP_HOME
+# Resolve links: $0 may be a link
+PRG="$0"
+# Need this for relative symlinks.
+while [ -h "$PRG" ] ; do
+    ls=`ls -ld "$PRG"`
+    link=`expr "$ls" : '.*-> \(.*\)$'`
+    if expr "$link" : '/.*' > /dev/null; then
+        PRG="$link"
+    else
+        PRG=`dirname "$PRG"`"/$link"
+    fi
+done
+SAVED="`pwd`"
+cd "`dirname \"$PRG\"`/" >&-
+APP_HOME="`pwd -P`"
+cd "$SAVED" >&-
+
+CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
+
+# Determine the Java command to use to start the JVM.
+if [ -n "$JAVA_HOME" ] ; then
+    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
+        # IBM's JDK on AIX uses strange locations for the executables
+        JAVACMD="$JAVA_HOME/jre/sh/java"
+    else
+        JAVACMD="$JAVA_HOME/bin/java"
+    fi
+    if [ ! -x "$JAVACMD" ] ; then
+        die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+    fi
+else
+    JAVACMD="java"
+    which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
+
+Please set the JAVA_HOME variable in your environment to match the
+location of your Java installation."
+fi
+
+# Increase the maximum file descriptors if we can.
+if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
+    MAX_FD_LIMIT=`ulimit -H -n`
+    if [ $? -eq 0 ] ; then
+        if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
+            MAX_FD="$MAX_FD_LIMIT"
+        fi
+        ulimit -n $MAX_FD
+        if [ $? -ne 0 ] ; then
+            warn "Could not set maximum file descriptor limit: $MAX_FD"
+        fi
+    else
+        warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
+    fi
+fi
+
+# For Darwin, add options to specify how the application appears in the dock
+if $darwin; then
+    GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
+fi
+
+# For Cygwin, switch paths to Windows format before running java
+if $cygwin ; then
+    APP_HOME=`cygpath --path --mixed "$APP_HOME"`
+    CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
+
+    # We build the pattern for arguments to be converted via cygpath
+    ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
+    SEP=""
+    for dir in $ROOTDIRSRAW ; do
+        ROOTDIRS="$ROOTDIRS$SEP$dir"
+        SEP="|"
+    done
+    OURCYGPATTERN="(^($ROOTDIRS))"
+    # Add a user-defined pattern to the cygpath arguments
+    if [ "$GRADLE_CYGPATTERN" != "" ] ; then
+        OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
+    fi
+    # Now convert the arguments - kludge to limit ourselves to /bin/sh
+    i=0
+    for arg in "$@" ; do
+        CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
+        CHECK2=`echo "$arg"|egrep -c "^-"`                                 ### Determine if an option
+
+        if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then                    ### Added a condition
+            eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
+        else
+            eval `echo args$i`="\"$arg\""
+        fi
+        i=$((i+1))
+    done
+    case $i in
+        (0) set -- ;;
+        (1) set -- "$args0" ;;
+        (2) set -- "$args0" "$args1" ;;
+        (3) set -- "$args0" "$args1" "$args2" ;;
+        (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
+        (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
+        (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
+        (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
+        (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
+        (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
+    esac
+fi
+
+# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
+function splitJvmOpts() {
+    JVM_OPTS=("$@")
+}
+eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
+JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
+
+# Change the project's .gradle to the android out dir.
+ANDROID_GRADLE_ROOT="$APP_HOME/../../out/host/gradle/frameworks/support"
+if [[ -z "$ANDROID_CACHE_DIR" ]]; then
+  ANDROID_CACHE_DIR="$ANDROID_GRADLE_ROOT/.gradle"
+fi
+
+# Change the local user directories to be under the android out dir
+export GRADLE_USER_HOME="$ANDROID_GRADLE_ROOT/.gradle"
+export M2_HOME="$ANDROID_GRADLE_ROOT/.m2"
+
+exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain --project-cache-dir=$ANDROID_CACHE_DIR "$@"
diff --git a/instrumentation/Android.mk b/instrumentation/Android.mk
index dce8f93..59805be 100644
--- a/instrumentation/Android.mk
+++ b/instrumentation/Android.mk
@@ -19,4 +19,24 @@
 LOCAL_JAVA_LIBRARIES := android-support-multidex
 LOCAL_SDK_VERSION := 4
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+
+ifneq "" "$(filter eng.%,$(BUILD_NUMBER))"
+  ASMDI_VERSION_NAME_TAG := eng.$(USER)
+else
+  ASMDI_VERSION_NAME_TAG := $(BUILD_NUMBER)
+endif
+
+ASMDI_GIT_VERSION_TAG := `cd $(LOCAL_PATH); git log --format="%H" -n 1`
+
+ASMDI_VERSION_INTERMEDIATE = $(call intermediates-dir-for,JAVA_LIBRARIES,$(LOCAL_MODULE),,COMMON)/$(LOCAL_MODULE).version.txt
+$(ASMDI_VERSION_INTERMEDIATE):
+	$(hide) mkdir -p $(dir $@)
+	$(hide) echo "build.version=$(ASMDI_VERSION_NAME_TAG)" > $@
+	$(hide) echo "build.id=$(BUILD_ID)" >> $@
+	$(hide) echo "git.version=$(ASMDI_GIT_VERSION_TAG)" >> $@
+
+LOCAL_JAVA_RESOURCE_FILES := $(ASMDI_VERSION_INTERMEDIATE)
+
+
 include $(BUILD_STATIC_JAVA_LIBRARY)
diff --git a/instrumentation/AndroidManifest.xml b/instrumentation/AndroidManifest.xml
index f29fe15..6107fd3 100644
--- a/instrumentation/AndroidManifest.xml
+++ b/instrumentation/AndroidManifest.xml
@@ -14,6 +14,7 @@
      limitations under the License.
 -->
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
-          package="android.support.multidex">
+          package="android.support.multidex.instrumentation">
     <uses-sdk android:minSdkVersion="4"/>
+    <application />
 </manifest>
diff --git a/instrumentation/build.gradle b/instrumentation/build.gradle
new file mode 100644
index 0000000..15ea3e8
--- /dev/null
+++ b/instrumentation/build.gradle
@@ -0,0 +1,58 @@
+apply plugin: 'android-library'
+archivesBaseName = 'multidex-instrumentation'
+
+android {
+    compileSdkVersion 4
+
+    sourceSets {
+        main {
+            java.srcDirs         = ['src']
+            resources.srcDirs    = ['res']
+            res.srcDirs          = ['src']
+            manifest.srcFile 'AndroidManifest.xml'
+        }
+    }
+
+    lintOptions {
+        // TODO: fix errors and reenable.
+        abortOnError false
+    }
+}
+
+dependencies {
+    compile project(':library')
+}
+
+uploadArchives {
+    repositories {
+        mavenDeployer {
+            repository(url: uri(rootProject.ext.supportRepoOut)) {
+            }
+
+            pom.project {
+                name 'Android Multi-Dex Instrumentation Library'
+                description "Library for legacy multi-dex support"
+                url ''
+                inceptionYear '2013'
+
+                licenses {
+                    license {
+                        name 'The Apache Software License, Version 2.0'
+                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
+                        distribution 'repo'
+                    }
+                }
+
+                scm {
+                    url "http://source.android.com"
+                    connection "scm:git:https://android.googlesource.com/platform/frameworks/multidex"
+                }
+                developers {
+                    developer {
+                        name 'The Android Open Source Project'
+                    }
+                }
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/instrumentation/project.properties b/instrumentation/project.properties
index 1b8c5a3..22e1c48 100644
--- a/instrumentation/project.properties
+++ b/instrumentation/project.properties
@@ -11,5 +11,5 @@
 #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
 
 # Project target.
-target=android-18
+target=android-14
 android.library=true
diff --git a/library/Android.mk b/library/Android.mk
index 40a3f68..dd8a6ff 100644
--- a/library/Android.mk
+++ b/library/Android.mk
@@ -16,6 +16,29 @@
 
 include $(CLEAR_VARS)
 LOCAL_MODULE := android-support-multidex
-LOCAL_SDK_VERSION := 11
+LOCAL_SDK_VERSION := 4
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+ifneq "" "$(filter eng.%,$(BUILD_NUMBER))"
+  ASMD_VERSION_NAME_TAG := eng.$(USER)
+else
+  ASMD_VERSION_NAME_TAG := $(BUILD_NUMBER)
+endif
+
+ASMD_GIT_VERSION_TAG := `cd $(LOCAL_PATH); git log --format="%H" -n 1`
+
+ASMD_VERSION_INTERMEDIATE = $(call intermediates-dir-for,JAVA_LIBRARIES,$(LOCAL_MODULE),,COMMON)/$(LOCAL_MODULE).version.txt
+$(ASMD_VERSION_INTERMEDIATE):
+	$(hide) mkdir -p $(dir $@)
+	$(hide) echo "build.version=$(ASMD_VERSION_NAME_TAG)" > $@
+	$(hide) echo "build.id=$(BUILD_ID)" >> $@
+	$(hide) echo "git.version=$(ASMD_GIT_VERSION_TAG)" >> $@
+
+LOCAL_JAVA_RESOURCE_FILES := $(ASMD_VERSION_INTERMEDIATE)
+
+# Use a dir because we have files in subfolders and we want to keep the path into the archive
+#
+# use "resources" instead of "res" because res is already used for packaging the SDK project
+LOCAL_JAVA_RESOURCE_DIRS := resources
+
 include $(BUILD_STATIC_JAVA_LIBRARY)
diff --git a/library/README.txt b/library/README.txt
index df4772a..62e8737 100644
--- a/library/README.txt
+++ b/library/README.txt
@@ -1,7 +1,13 @@
-Library Project including compatibility multi dex loader.
+Library Project including a multidex loader.
 
-This can be used by an Android project to install classloader
-with multiple dex of applications running on API 14+.
+This can be used by an Android project to install multiple dexes
+in the classloader of an application running on API 4+.
+
+Note that multidexing will allow to go over the dex index limit.
+It can also help with the linearalloc limit during installation but it
+won't help with linearalloc at execution time. This means that
+most applications requiring multidexing because of the dex index
+limit won't execute on API below 14 because of linearalloc limit.
 
 There is technically no source, but the src folder is necessary
 to ensure that the build system works.  The content is actually
diff --git a/library/build.gradle b/library/build.gradle
index 0fe5882..f04615d 100644
--- a/library/build.gradle
+++ b/library/build.gradle
@@ -1,6 +1,9 @@
 apply plugin: 'android-library'
+archivesBaseName = 'multidex'
 
 android {
+    compileSdkVersion 4
+
     sourceSets {
         main {
             java.srcDirs         = ['src']
@@ -15,3 +18,37 @@
         abortOnError false
     }
 }
+
+uploadArchives {
+    repositories {
+        mavenDeployer {
+            repository(url: uri(rootProject.ext.supportRepoOut)) {
+            }
+
+            pom.project {
+                name 'Android Multi-Dex Library'
+                description "Library for legacy multi-dex support"
+                url ''
+                inceptionYear '2013'
+
+                licenses {
+                    license {
+                        name 'The Apache Software License, Version 2.0'
+                        url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
+                        distribution 'repo'
+                    }
+                }
+
+                scm {
+                    url "http://source.android.com"
+                    connection "scm:git:https://android.googlesource.com/platform/frameworks/multidex"
+                }
+                developers {
+                    developer {
+                        name 'The Android Open Source Project'
+                    }
+                }
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/library/project.properties b/library/project.properties
index 1b8c5a3..22e1c48 100644
--- a/library/project.properties
+++ b/library/project.properties
@@ -11,5 +11,5 @@
 #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
 
 # Project target.
-target=android-18
+target=android-14
 android.library=true
diff --git a/library/resources/JACK-INF/legacyMultidexInstallation.jpp b/library/resources/JACK-INF/legacyMultidexInstallation.jpp
new file mode 100644
index 0000000..8a81eba
--- /dev/null
+++ b/library/resources/JACK-INF/legacyMultidexInstallation.jpp
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+multidexInstanciable:
+  @@com.android.jack.annotations.MultiDexInstaller
+  class *
+    is {
+      public
+    }
+    extends {
+        class android.app.Instrumentation
+      | class android.app.Activity
+      | class android.app.Service
+      | class android.content.ContentProvider
+      | class android.content.BroadcastReceiver
+      | class android.app.backup.BackupAgent
+      | class android.app.Application
+    }
+    do {
+      @@com.android.jack.annotations.MultiDexInstaller
+      method void <init>();
+    }
+
+multidexInstaller:
+  class *
+    is {
+      public
+    }
+    extends {
+      class android.app.Application
+    }
+    do {
+      @@com.android.jack.annotations.MultiDexInstaller
+      method void attachBaseContext(class android.content.Context)
+      ;
+    }
diff --git a/library/src/android/support/multidex/MultiDexExtractor.java b/library/src/android/support/multidex/MultiDexExtractor.java
index b7e0a11..27da6b3 100644
--- a/library/src/android/support/multidex/MultiDexExtractor.java
+++ b/library/src/android/support/multidex/MultiDexExtractor.java
@@ -243,9 +243,9 @@
 
     private static SharedPreferences getMultiDexPreferences(Context context) {
         return context.getSharedPreferences(PREFS_FILE,
-                Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB
+                Build.VERSION.SDK_INT < 11 /* Build.VERSION_CODES.HONEYCOMB */
                         ? Context.MODE_PRIVATE
-                        : Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS);
+                        : Context.MODE_PRIVATE | 0x0004 /* Context.MODE_MULTI_PROCESS */);
     }
 
     /**
@@ -253,10 +253,12 @@
      */
     private static void prepareDexDir(File dexDir, final String extractedFilePrefix)
             throws IOException {
-        dexDir.mkdirs();
-        if (!dexDir.isDirectory()) {
-            throw new IOException("Failed to create dex directory " + dexDir.getPath());
-        }
+        /* mkdirs() has some bugs, especially before jb-mr1 and we have only a maximum of one parent
+         * to create, lets stick to mkdir().
+         */
+        File cache = dexDir.getParentFile();
+        mkdirChecked(cache);
+        mkdirChecked(dexDir);
 
         // Clean possible old files
         FileFilter filter = new FileFilter() {
@@ -282,6 +284,24 @@
         }
     }
 
+    private static void mkdirChecked(File dir) throws IOException {
+        dir.mkdir();
+        if (!dir.isDirectory()) {
+            File parent = dir.getParentFile();
+            if (parent == null) {
+                Log.e(TAG, "Failed to create dir " + dir.getPath() + ". Parent file is null.");
+            } else {
+                Log.e(TAG, "Failed to create dir " + dir.getPath() +
+                        ". parent file is a dir " + parent.isDirectory() +
+                        ", a file " + parent.isFile() +
+                        ", exists " + parent.exists() +
+                        ", readable " + parent.canRead() +
+                        ", writable " + parent.canWrite());
+            }
+            throw new IOException("Failed to create cache directory " + dir.getPath());
+        }
+    }
+
     private static void extract(ZipFile apk, ZipEntry dexFile, File extractTo,
             String extractedFilePrefix) throws IOException, FileNotFoundException {
 
diff --git a/settings.gradle b/settings.gradle
new file mode 100644
index 0000000..c1c64f3
--- /dev/null
+++ b/settings.gradle
@@ -0,0 +1,2 @@
+include ':library'
+include ':instrumentation'