Skip to content

Commit a9e9626

Browse files
README: improve and fix Gradle setup instructions
Add all required plugins, add missing repository declaration for plugins syntax. Add inline instructions. Also make TOML variant the default, move others to an alternatives section.
1 parent 1e9a7a6 commit a9e9626

File tree

1 file changed

+106
-30
lines changed

1 file changed

+106
-30
lines changed

README.md

Lines changed: 106 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ box.remove(person) // Delete
108108
> [!NOTE]
109109
> Prefer to look at example code? Check out [our examples repository](https://github.com/objectbox/objectbox-examples).
110110
111-
You can add the ObjectBox Java SDK to your project using:
111+
You can add the ObjectBox Java SDK using a:
112112

113-
- a [Gradle setup](#gradle-setup)
114-
- a [Maven setup](#maven-setup)
113+
- [Gradle setup](#gradle-setup)
114+
- [Maven setup](#maven-setup)
115115

116116
ObjectBox tools and dependencies are available on [the Maven Central repository](https://central.sonatype.com/namespace/io.objectbox).
117117

@@ -131,31 +131,68 @@ The APIs and tools of the ObjectBox Java SDK support:
131131

132132
### Gradle setup
133133

134-
For Gradle projects, add the ObjectBox Gradle plugin to your root Gradle script.
134+
For Gradle projects, add the required plugins to your root Gradle script.
135135

136-
When using a TOML version catalog and plugins syntax (for alternatives see below):
136+
When using a [TOML version catalog](https://docs.gradle.org/current/userguide/version_catalogs.html) and plugins syntax
137+
(for alternatives see below):
137138

138139
```toml
139140
# gradle/libs.versions.toml
141+
140142
[versions]
143+
# Define a variable for the version of the plugin
141144
objectbox = "5.1.0"
142145

146+
# For an Android project
147+
agp = "<AGP_VERSION>"
148+
149+
# If using Kotlin
150+
kotlin = "<KOTLIN_VERSION>"
151+
143152
[plugins]
153+
# Add an alias for the plugin
144154
objectbox = { id = "io.objectbox", version.ref = "objectbox" }
155+
156+
# For an Android project
157+
android-application = { id = "com.android.application", version.ref = "agp" }
158+
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
159+
kotlin-kapt = { id = "org.jetbrains.kotlin.kapt", version.ref = "kotlin" }
160+
161+
# For a JVM project, if using Kotlin
162+
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
163+
kotlin-kapt = { id = "org.jetbrains.kotlin.kapt", version.ref = "kotlin" }
145164
```
146165

147166
```kotlin
148167
// build.gradle.kts
168+
149169
plugins {
170+
// Add the plugin
150171
alias(libs.plugins.objectbox) apply false
172+
173+
// For an Android project
174+
alias(libs.plugins.android.application) apply false
175+
alias(libs.plugins.kotlin.android) apply false
176+
alias(libs.plugins.kotlin.kapt) apply false
177+
178+
// For a JVM project, if using Kotlin
179+
alias(libs.plugins.kotlin.jvm) apply false
180+
alias(libs.plugins.kotlin.kapt) apply false
151181
}
152182
```
153183

154184
```kotlin
155185
// settings.gradle.kts
186+
156187
pluginManagement {
188+
repositories {
189+
// Add the Maven Central repository
190+
mavenCentral()
191+
}
192+
157193
resolutionStrategy {
158194
eachPlugin {
195+
// Map the plugin ID to the Maven artifact
159196
if (requested.id.id == "io.objectbox") {
160197
useModule("io.objectbox:objectbox-gradle-plugin:${requested.version}")
161198
}
@@ -164,22 +201,57 @@ pluginManagement {
164201
}
165202
```
166203

167-
Alternatives:
204+
Then, in the Gradle script of your subproject apply the necessary plugins:
205+
206+
```kotlin
207+
// app/build.gradle.kts
208+
209+
plugins {
210+
// For an Android project
211+
alias(libs.plugins.android.application)
212+
alias(libs.plugins.kotlin.android)
213+
alias(libs.plugins.kotlin.kapt)
214+
215+
// For a JVM project
216+
id("application") // or id("java-library")
217+
// Optional, if using Kotlin
218+
alias(libs.plugins.kotlin.jvm)
219+
alias(libs.plugins.kotlin.kapt)
220+
221+
// Finally, apply the plugin
222+
alias(libs.plugins.objectbox)
223+
}
224+
```
225+
226+
Finally, sync the Gradle project with your IDE (for ex. using "Sync Project with Gradle Files" in Android Studio).
227+
228+
Your project can now use ObjectBox, continue by [defining entity classes](https://docs.objectbox.io/getting-started#define-entity-classes).
168229

169-
<details><summary>Using plugins syntax</summary>
230+
#### Alternatives
231+
232+
<details><summary>Using plugins syntax with plugin IDs</summary>
170233

171234
```kotlin
172235
// build.gradle.kts
236+
173237
plugins {
238+
// Add the plugin
174239
id("io.objectbox") version "5.1.0" apply false
175240
}
176241
```
177242

178243
```kotlin
179244
// settings.gradle.kts
245+
180246
pluginManagement {
247+
repositories {
248+
// Add the Maven Central repository
249+
mavenCentral()
250+
}
251+
181252
resolutionStrategy {
182253
eachPlugin {
254+
// Map the plugin ID to the Maven artifact
183255
if (requested.id.id == "io.objectbox") {
184256
useModule("io.objectbox:objectbox-gradle-plugin:${requested.version}")
185257
}
@@ -194,12 +266,18 @@ pluginManagement {
194266

195267
```kotlin
196268
// build.gradle.kts
269+
197270
buildscript {
271+
// Define a variable for the plugin version
198272
val objectboxVersion by extra("5.1.0")
199-
repositories {
273+
274+
repositories {
275+
// Add the Maven Central repository
200276
mavenCentral()
201277
}
278+
202279
dependencies {
280+
// Add the plugin
203281
classpath("io.objectbox:objectbox-gradle-plugin:$objectboxVersion")
204282
}
205283
}
@@ -211,49 +289,47 @@ buildscript {
211289

212290
```groovy
213291
// build.gradle
292+
214293
buildscript {
294+
// Define a variable for the plugin version
215295
ext.objectboxVersion = "5.1.0"
296+
216297
repositories {
298+
// Add the Maven Central repository
217299
mavenCentral()
218300
}
301+
219302
dependencies {
303+
// Add the plugin
220304
classpath("io.objectbox:objectbox-gradle-plugin:$objectboxVersion")
221305
}
222306
}
223307
```
224308

225309
</details>
226310

227-
Then, in the Gradle script of your subproject apply the plugin:
311+
Then, in the Gradle script of your subproject apply the necessary plugins using their IDs:
228312

229313
```kotlin
230314
// app/build.gradle.kts
231-
plugins {
232-
alias(libs.plugins.android.application) // When used in an Android project
233-
alias(libs.plugins.kotlin.android) // When used in an Android project
234-
alias(libs.plugins.kotlin.kapt) // When used in an Android or Kotlin project
235-
alias(libs.plugins.objectbox) // Add after other plugins
236-
}
237-
```
238-
239-
<details><summary>Alternative: when not using a version catalog, using the plugin id</summary>
240315

241-
```kotlin
242-
// app/build.gradle.kts
243316
plugins {
244-
id("com.android.application") // When used in an Android project
245-
kotlin("android") // When used in an Android project
246-
kotlin("kapt") // When used in an Android or Kotlin project
247-
id("io.objectbox") // Add after other plugins
317+
// For an Android project
318+
id("com.android.application") // or id("com.android.library")
319+
id("org.jetbrains.kotlin.android") // or kotlin("android")
320+
id("org.jetbrains.kotlin.kapt") // or kotlin("kapt")
321+
322+
// For a JVM project
323+
id("application") // or id("java-library")
324+
// Optional, if using Kotlin
325+
id("org.jetbrains.kotlin.jvm") // or kotlin("jvm")
326+
id("org.jetbrains.kotlin.kapt") // or kotlin("kapt")
327+
328+
// Finally, apply the plugin
329+
id("io.objectbox")
248330
}
249331
```
250332

251-
</details>
252-
253-
Finally, sync the Gradle project with your IDE (for ex. using "Sync Project with Gradle Files" in Android Studio).
254-
255-
Your project can now use ObjectBox, continue by [defining entity classes](https://docs.objectbox.io/getting-started#define-entity-classes).
256-
257333
### Maven setup
258334

259335
This is currently only supported for JVM projects.

0 commit comments

Comments
 (0)