@@ -96,8 +96,6 @@ public class BoxStoreBuilder {
9696
9797 int debugFlags ;
9898
99- private boolean android ;
100-
10199 boolean debugRelations ;
102100
103101 int fileMode ;
@@ -187,9 +185,7 @@ public BoxStoreBuilder name(String name) {
187185 public BoxStoreBuilder directory (File directory ) {
188186 checkIsNull (name , "Already has name, cannot assign directory" );
189187 checkIsNull (inMemory , "Already set to in-memory database, cannot assign directory" );
190- if (!android ) {
191- checkIsNull (baseDirectory , "Already has base directory, cannot assign directory" );
192- }
188+ checkIsNull (baseDirectory , "Already has base directory, cannot assign directory" );
193189 this .directory = directory ;
194190 return this ;
195191 }
@@ -232,41 +228,25 @@ private static void checkIsNull(@Nullable Object value, String errorMessage) {
232228
233229 /**
234230 * Use on Android to pass a <a href="https://developer.android.com/reference/android/content/Context">Context</a>
235- * for loading the native library and, if {@link #inMemory(String)} was not called before, creating the base
231+ * for loading the native library and, if not an {@link #inMemory(String)} database, for creating the base
236232 * directory for database files in the
237233 * <a href="https://developer.android.com/reference/android/content/Context#getFilesDir()">files directory of the app</a>.
238234 * <p>
239- * In more detail, this assigns the base directory (see {@link #baseDirectory}) to
235+ * In more detail, upon {@link #build()} assigns the base directory (see {@link #baseDirectory}) to
240236 * {@code context.getFilesDir() + "/objectbox/"}.
241237 * Thus, when using the default name (also "objectbox", unless overwritten using {@link #name(String)}), the default
242238 * location of database files will be "objectbox/objectbox/" inside the app's files directory.
243239 * If a custom name is specified, for example with {@code name("foobar")}, it would become "objectbox/foobar/".
244240 * <p>
245- * Alternatively, use {@link #baseDirectory(File)} or {@link #directory(File)}.
241+ * Use {@link #baseDirectory(File)} or {@link #directory(File)} to specify a different directory for the database
242+ * files.
246243 */
247244 public BoxStoreBuilder androidContext (Object context ) {
248245 //noinspection ConstantConditions Annotation does not enforce non-null.
249246 if (context == null ) {
250247 throw new NullPointerException ("Context may not be null" );
251248 }
252249 this .context = getApplicationContext (context );
253-
254- // Only create directories if not already an in-memory database.
255- // Note: this will still create directories if this is called before switching to an in-memory database.
256- if (inMemory == null ) {
257- File baseDir = getAndroidBaseDir (context );
258- if (!baseDir .exists ()) {
259- baseDir .mkdir ();
260- if (!baseDir .exists ()) { // check baseDir.exists() because of potential concurrent processes
261- throw new RuntimeException ("Could not init Android base dir at " + baseDir .getAbsolutePath ());
262- }
263- }
264- if (!baseDir .isDirectory ()) {
265- throw new RuntimeException ("Android base dir is not a dir: " + baseDir .getAbsolutePath ());
266- }
267- baseDirectory = baseDir ;
268- }
269- android = true ;
270250 return this ;
271251 }
272252
@@ -627,12 +607,30 @@ byte[] buildFlatStoreOptions(String canonicalPath) {
627607 }
628608
629609 /**
630- * Builds a {@link BoxStore} using any given configuration.
610+ * Builds a {@link BoxStore} using the current configuration of this builder.
611+ *
612+ * <p>If {@link #androidContext(Object)} was called and no {@link #directory(File)} or {@link #baseDirectory(File)}
613+ * is configured, creates and sets {@link #baseDirectory(File)} as explained in {@link #androidContext(Object)}.
631614 */
632615 public BoxStore build () {
616+ // If in-memory, use a special directory (it will never be created)
633617 if (inMemory != null ) {
634618 directory = new File (BoxStore .IN_MEMORY_PREFIX + inMemory );
635619 }
620+ // On Android, create and set base directory if no directory is explicitly configured
621+ if (directory == null && baseDirectory == null && context != null ) {
622+ File baseDir = getAndroidBaseDir (context );
623+ if (!baseDir .exists ()) {
624+ baseDir .mkdir ();
625+ if (!baseDir .exists ()) { // check baseDir.exists() because of potential concurrent processes
626+ throw new RuntimeException ("Could not init Android base dir at " + baseDir .getAbsolutePath ());
627+ }
628+ }
629+ if (!baseDir .isDirectory ()) {
630+ throw new RuntimeException ("Android base dir is not a dir: " + baseDir .getAbsolutePath ());
631+ }
632+ baseDirectory = baseDir ;
633+ }
636634 if (directory == null ) {
637635 directory = getDbDir (baseDirectory , name );
638636 }
0 commit comments