Skip to content

Commit 1067e89

Browse files
mwwodaantusus
andauthored
docs: add android target guide (#1129)
Co-authored-by: Kamil Berdychowski <[email protected]>
1 parent dee4389 commit 1067e89

File tree

2 files changed

+105
-2
lines changed

2 files changed

+105
-2
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@ A current release is on the leading edge of our SDK development, and is intended
3434
Getting Started Docs: https://developer.box.com/guides/tooling/sdks/java/
3535
API Reference: https://developer.box.com/reference/
3636

37-
## Quickstart
37+
## JVM
38+
3839
The SDK can be obtained by adding it as a [maven dependency](http://opensource.box.com/box-java-sdk/),
3940
cloning the source into your project, or by downloading one of the precompiled JARs from the
4041
[releases page on GitHub](https://github.com/box/box-java-sdk/releases).
4142

43+
If you are developing application for Android visit our [Android guide](doc/android.md)
44+
4245
**IF YOU USE THE JAR, you'll also need to include several dependencies:**
4346

4447
1. [minimal-json v0.9.5](https://github.com/ralfstx/minimal-json)
@@ -55,7 +58,10 @@ cloning the source into your project, or by downloading one of the precompiled J
5558
[Java Cryptography Extension for IBM JDK](https://www14.software.ibm.com/webapp/iwm/web/preLogin.do?source=jcesdk)
5659

5760
An app has to be authorized by the admin of the enterprise before these tests. It's always good to begin with the
58-
[Getting Started Section](https://developer.box.com/docs/setting-up-a-jwt-app) at Box's developer website
61+
[Getting Started Section](https://developer.box.com/docs/setting-up-a-jwt-app) at Box's developer website.
62+
63+
## Android
64+
If you are developing application for Android visit our [Android guide](doc/android.md).
5965

6066
## Quick Test
6167

doc/android.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Android
2+
3+
The Java SDK should be compatible with modern Android applications written in both Java and Kotlin.
4+
5+
To use the Java SDK in an android application add it to the project's gradle file in the `dependencies` block.
6+
7+
For Groovy
8+
9+
```groovy
10+
// build.gradle
11+
12+
dependencies {
13+
implementation "com.box:box-java-sdk:3.8.0"
14+
}
15+
```
16+
17+
For Kotlin
18+
19+
```kotlin
20+
// build.gradle.kts
21+
22+
dependencies {
23+
implementation("com.box:box-java-sdk:3.8.0")
24+
}
25+
```
26+
27+
## Kotlin
28+
29+
The Java SDK can also be used in Kotlin Android applications through interoperability thanks to the Kotlin design.
30+
You can read more about Kotlin and Java interoperability [here](https://kotlinlang.org/docs/java-interop.html)
31+
32+
The following example creates an API connection with a developer token:
33+
34+
```kotlin
35+
val api = BoxAPIConnection("myToken")
36+
```
37+
38+
The following example shows how to get current user
39+
40+
```kotlin
41+
val userID = "33333"
42+
val api = BoxAPIConnection("myToken")
43+
val user = BoxUser(api, userID)
44+
val userInfo = user.getInfo()
45+
```
46+
47+
If you are using an IntelliJ-based IDE, you can copy our samples located in the [docs](/doc/) directory
48+
and paste them into your file. The IDE should ask you to convert the pasted Java sample to Kotlin. Most samples still work after conversion using this approach.
49+
50+
Note that the current Java SDK does not support Kotlin coroutines. By default, you cannot run network calls on the main thread
51+
in an Android application. There are various ways to overcome this. For example, if you are in a viewModel context, you can run the SDK method as a
52+
coroutine using viewModelScope.
53+
54+
```kotlin
55+
viewModelScope.launch {
56+
val result = withContext(Dispatchers.IO) {
57+
/*
58+
SDK code goes here
59+
*/
60+
}
61+
// here you can access the result and load it to the viewModel
62+
}
63+
```
64+
65+
The following example shows how to get the current items in the root folder, sorted by name in ascending order with additional
66+
"created_by" and "name" fields returned from the API. The items are then loaded to the custom data class defined earlier.
67+
68+
```kotlin
69+
// data class definition used in viewModel
70+
data class Item(
71+
val isFolder: Boolean,
72+
val name: String,
73+
val createdBy: String
74+
)
75+
76+
// viewModel init code
77+
viewModelScope.launch {
78+
val result = withContext(Dispatchers.IO) {
79+
val res = BoxFolder(BoxAPIConnection("myToken"), "0")
80+
val iterator: Iterator<BoxItem.Info> =
81+
res.getChildren("name", BoxFolder.SortDirection.ASC, "created_by", "name")
82+
.iterator()
83+
val items = mutableListOf<Item>()
84+
85+
when (val itemInfo = iterator.next()) {
86+
is BoxFile.Info -> items.add(Item(false, "File " + itemInfo.name, itemInfo.createdBy.name))
87+
is BoxFolder.Info -> items.add(Item(true, "Folder " + itemInfo.name, itemInfo.createdBy.name))
88+
}
89+
items
90+
}
91+
}
92+
```
93+
94+
If you are familiar with Kotlin syntax, you might have noticed that we could have used the `.map` function (or a similar function) to map the API result to a list of items. Due to current limitations, using `.map` and similar operations on collections is not always possible and may
95+
lead to unexpected results. The preferred way is to use an explicit iterator to iterate over the collections returned by the SDK.
96+
97+
If you find any problem related to the Java SDK in Kotlin-based app feel free to open an issue.

0 commit comments

Comments
 (0)