 (defn namespaces-to-compile
   "Takes project and returns a set of namespaces that should be AOT-compiled."
   [{{:keys [aot aot-exclude-ns]} :android :as project}]
-  (-> (case aot
-        :all
-          (seq (leiningen.compile/stale-namespaces (assoc project :aot :all)))
-        :all-with-unused
-          (namespaces-on-classpath :classpath
-                                   (map io/file (get-classpath project)))
-        ;; else
-          (map symbol aot))
-      set
-      (sets/difference (set (map symbol aot-exclude-ns)))))
+  (let [all-nses (bultitude/namespaces-on-classpath
+                  :classpath (map io/file (get-classpath project)))
+        include (case aot
+                  :all (stale-namespaces (assoc project :aot :all))
+                  :all-with-unused all-nses
+                  aot)
+        exclude aot-exclude-ns
+
+        {include-nses false, include-regexps true}
+        (group-by #(instance? Pattern %) include)
+
+        {exclude-nses false, exclude-regexps true}
+        (group-by #(instance? Pattern %) exclude)]
+    (->> (set/difference (set (map str (if (seq include-regexps)
+                                         all-nses include-nses)))
+                         (set exclude-nses))
+         (filter (fn [ns] (if (seq include-regexps)
+                           (some #(re-matches % ns) include-regexps)
+                           true)))
+         (remove (fn [ns] (if (seq exclude-regexps)
+                           (some #(re-matches % ns) exclude-regexps)
+                           false)))
+         (concat (if (seq include-regexps)
+                   include-nses ()))
+         (map symbol))))
 
 (defn compile-clojure
   "Compiles Clojure files into .class files.

        external-resources
        "-I" android-jar
        "-J" gen-path
        "--generate-dependencies"))
  project)

(defn code-gen
  "Generates R.java and builds a manifest with the appropriate version

(ns leiningen.droid.compile
  "This part of the plugin is responsible for the project compilation."
  (:refer-clojure :exclude [compile])
  (:require [bultitude.core :as bultitude]
            [clojure.java.io :as io]
            [clojure.set :as set]
            [clojure.string :as str]
            [clostache.parser :as clostache]
            [leiningen.compile :refer [stale-namespaces]]
            [leiningen.core.classpath :refer [get-classpath]]
            [leiningen.core.eval :as eval]
            [leiningen.core.main :refer [debug info abort]]
            [leiningen.droid.manifest :refer [get-package-name generate-manifest]]
            [leiningen.droid.utils :refer [get-sdk-android-jar sdk-binary
                                           ensure-paths sh dev-build?]]
            leiningen.javac
            [leiningen.new.templates :refer [slurp-resource sanitize]])
  (:import java.util.regex.Pattern))

;; ### Pre-compilation tasks

