From 1067e8999c6fab740a41cc0f8b08a3dd575b3bcc Mon Sep 17 00:00:00 2001 From: Mateusz Woda Date: Tue, 6 Dec 2022 16:11:32 +0100 Subject: [PATCH] docs: add android target guide (#1129) Co-authored-by: Kamil Berdychowski --- README.md | 10 ++++-- doc/android.md | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 doc/android.md diff --git a/README.md b/README.md index c7f64e6be..fc4553122 100644 --- a/README.md +++ b/README.md @@ -34,11 +34,14 @@ A current release is on the leading edge of our SDK development, and is intended Getting Started Docs: https://developer.box.com/guides/tooling/sdks/java/ API Reference: https://developer.box.com/reference/ -## Quickstart +## JVM + The SDK can be obtained by adding it as a [maven dependency](http://opensource.box.com/box-java-sdk/), cloning the source into your project, or by downloading one of the precompiled JARs from the [releases page on GitHub](https://github.com/box/box-java-sdk/releases). +If you are developing application for Android visit our [Android guide](doc/android.md) + **IF YOU USE THE JAR, you'll also need to include several dependencies:** 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 [Java Cryptography Extension for IBM JDK](https://www14.software.ibm.com/webapp/iwm/web/preLogin.do?source=jcesdk) An app has to be authorized by the admin of the enterprise before these tests. It's always good to begin with the -[Getting Started Section](https://developer.box.com/docs/setting-up-a-jwt-app) at Box's developer website +[Getting Started Section](https://developer.box.com/docs/setting-up-a-jwt-app) at Box's developer website. + +## Android +If you are developing application for Android visit our [Android guide](doc/android.md). ## Quick Test diff --git a/doc/android.md b/doc/android.md new file mode 100644 index 000000000..d03804c4e --- /dev/null +++ b/doc/android.md @@ -0,0 +1,97 @@ +# Android + +The Java SDK should be compatible with modern Android applications written in both Java and Kotlin. + +To use the Java SDK in an android application add it to the project's gradle file in the `dependencies` block. + +For Groovy + +```groovy +// build.gradle + +dependencies { + implementation "com.box:box-java-sdk:3.8.0" +} +``` + +For Kotlin + +```kotlin +// build.gradle.kts + +dependencies { + implementation("com.box:box-java-sdk:3.8.0") +} +``` + +## Kotlin + +The Java SDK can also be used in Kotlin Android applications through interoperability thanks to the Kotlin design. +You can read more about Kotlin and Java interoperability [here](https://kotlinlang.org/docs/java-interop.html) + +The following example creates an API connection with a developer token: + +```kotlin +val api = BoxAPIConnection("myToken") +``` + +The following example shows how to get current user + +```kotlin +val userID = "33333" +val api = BoxAPIConnection("myToken") +val user = BoxUser(api, userID) +val userInfo = user.getInfo() +``` + +If you are using an IntelliJ-based IDE, you can copy our samples located in the [docs](/doc/) directory +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. + +Note that the current Java SDK does not support Kotlin coroutines. By default, you cannot run network calls on the main thread +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 +coroutine using viewModelScope. + +```kotlin +viewModelScope.launch { + val result = withContext(Dispatchers.IO) { + /* + SDK code goes here + */ + } + // here you can access the result and load it to the viewModel +} +``` + +The following example shows how to get the current items in the root folder, sorted by name in ascending order with additional +"created_by" and "name" fields returned from the API. The items are then loaded to the custom data class defined earlier. + +```kotlin +// data class definition used in viewModel +data class Item( + val isFolder: Boolean, + val name: String, + val createdBy: String +) + +// viewModel init code +viewModelScope.launch { + val result = withContext(Dispatchers.IO) { + val res = BoxFolder(BoxAPIConnection("myToken"), "0") + val iterator: Iterator = + res.getChildren("name", BoxFolder.SortDirection.ASC, "created_by", "name") + .iterator() + val items = mutableListOf() + + when (val itemInfo = iterator.next()) { + is BoxFile.Info -> items.add(Item(false, "File " + itemInfo.name, itemInfo.createdBy.name)) + is BoxFolder.Info -> items.add(Item(true, "Folder " + itemInfo.name, itemInfo.createdBy.name)) + } + items + } +} +``` + +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 +lead to unexpected results. The preferred way is to use an explicit iterator to iterate over the collections returned by the SDK. + +If you find any problem related to the Java SDK in Kotlin-based app feel free to open an issue. \ No newline at end of file