Skip to content

Commit 687ca26

Browse files
authored
Merge pull request #132 from algolia/feature/target-android
Feature/target android
2 parents 3fde390 + 4ade351 commit 687ca26

File tree

19 files changed

+371
-23
lines changed

19 files changed

+371
-23
lines changed

build.gradle.kts

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
import com.android.build.gradle.LibraryExtension
12
import com.jfrog.bintray.gradle.tasks.BintrayUploadTask
23
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
34
import java.net.URI
45

6+
buildscript {
7+
repositories {
8+
google()
9+
}
10+
dependencies {
11+
classpath("com.android.tools.build:gradle:3.5.0")
12+
}
13+
}
514

615
plugins {
716
id("kotlin-multiplatform") version "1.3.50"
@@ -11,16 +20,48 @@ plugins {
1120
id("com.github.kukuhyoniatmoko.buildconfigkotlin") version "1.0.5"
1221
}
1322

14-
version = Library.version
15-
group = Library.group
23+
apply(plugin = "com.android.library")
1624

1725
repositories {
1826
jcenter()
27+
google()
1928
mavenCentral()
2029
maven { url = URI("https://dl.bintray.com/kotlin/ktor") }
2130
maven { url = URI("https://kotlin.bintray.com/kotlinx") }
2231
}
2332

33+
version = Library.version
34+
group = Library.group
35+
36+
extensions.getByType(LibraryExtension::class.java).apply {
37+
compileSdkVersion(28)
38+
39+
defaultConfig {
40+
minSdkVersion(17)
41+
targetSdkVersion(28)
42+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
43+
}
44+
45+
testOptions.unitTests.isIncludeAndroidResources = true
46+
47+
sourceSets {
48+
getByName("main") {
49+
manifest.srcFile("src/androidMain/AndroidManifest.xml")
50+
java.srcDirs("src/androidMain/kotlin")
51+
res.srcDirs("src/androidMain/res")
52+
}
53+
getByName("test") {
54+
java.srcDirs("src/androidTest/kotlin")
55+
res.srcDirs("src/androidTest/res")
56+
}
57+
}
58+
59+
compileOptions {
60+
sourceCompatibility = JavaVersion.VERSION_1_8
61+
targetCompatibility = JavaVersion.VERSION_1_8
62+
}
63+
}
64+
2465
buildConfigKotlin {
2566
sourceSet("metadata") {
2667
buildConfig(name = "version", value = Library.version)
@@ -35,6 +76,17 @@ kotlin {
3576
}
3677
}
3778
}
79+
android {
80+
mavenPublication {
81+
artifactId = "${Library.artifact}-android"
82+
}
83+
publishLibraryVariants("release")
84+
compilations.all {
85+
kotlinOptions {
86+
jvmTarget = "1.8"
87+
}
88+
}
89+
}
3890
sourceSets {
3991
all {
4092
languageSettings.progressiveMode = true
@@ -76,6 +128,26 @@ kotlin {
76128
implementation(Ktor("client-android"))
77129
}
78130
}
131+
val androidMain by getting {
132+
dependencies {
133+
api(kotlin("stdlib-jdk8"))
134+
api(Ktor("client-core-jvm"))
135+
api(Ktor("client-json-jvm"))
136+
api(Ktor("client-logging-jvm"))
137+
api(Ktor("client-serialization-jvm"))
138+
}
139+
}
140+
val androidTest by getting {
141+
dependencies {
142+
implementation(kotlin("test"))
143+
implementation(kotlin("test-junit"))
144+
implementation(Ktor("client-mock-jvm"))
145+
implementation(Ktor("client-android"))
146+
implementation(AndroidTestRunner())
147+
implementation(AndroidTestExtRunner())
148+
implementation(Robolectric())
149+
}
150+
}
79151
}
80152
}
81153

@@ -151,9 +223,8 @@ bintray {
151223

152224
tasks {
153225
val bintrayUpload by getting(BintrayUploadTask::class) {
154-
dependsOn(publishToMavenLocal)
155226
doFirst {
156-
setPublications("jvm", "metadata")
227+
setPublications("jvm", "metadata", "androidRelease")
157228
}
158229
}
159230
withType<KotlinCompile> {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
object AndroidTestExtRunner : Dependency {
2+
3+
override val group = "androidx.test.ext"
4+
override val artifact = "junit"
5+
override val version = "1.1.1"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
object AndroidTestRunner : Dependency {
2+
3+
override val group = "androidx.test"
4+
override val artifact = "runner"
5+
override val version = "1.2.0"
6+
}

buildSrc/src/main/kotlin/Dependency.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ interface Dependency {
44
val artifact: String
55
val version: String
66

7-
operator fun invoke(module: String): String {
8-
return "$group:$artifact-$module:$version"
7+
operator fun invoke(module: String? = null): String {
8+
val optionalModule = if (module != null) "-$module" else ""
9+
10+
return "$group:$artifact$optionalModule:$version"
911
}
1012
}

buildSrc/src/main/kotlin/Library.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ object Library: Dependency {
22

33
override val group = "com.algolia"
44
override val artifact = "algoliasearch-client-kotlin"
5-
override val version = "1.1.4"
5+
override val version = "1.2.0-beta01"
66
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
object Robolectric : Dependency {
2+
3+
override val group = "org.robolectric"
4+
override val artifact = "robolectric"
5+
override val version = "4.3"
6+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.3.1-all.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

settings.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import java.net.URI
22

33
pluginManagement {
44
repositories {
5+
google()
56
mavenCentral()
7+
jcenter()
68
maven { url = URI("https://plugins.gradle.org/m2/") }
79
}
810
resolutionStrategy {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest package="com.algolia.search"/>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.algolia.search.helper
2+
3+
import java.text.SimpleDateFormat
4+
import java.util.*
5+
6+
7+
internal actual object DateISO8601 {
8+
9+
val dateISO8601 = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US).apply {
10+
timeZone = TimeZone.getTimeZone("UTC")
11+
}
12+
13+
val dateISO8601Millis = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US).apply {
14+
timeZone = TimeZone.getTimeZone("UTC")
15+
}
16+
17+
actual fun format(timestamp: Long, inMilliseconds: Boolean): String {
18+
return if (inMilliseconds) dateISO8601Millis.format(Date(timestamp)) else dateISO8601.format(Date(timestamp))
19+
}
20+
21+
actual fun parse(date: String, inMilliseconds: Boolean): Long {
22+
return if (inMilliseconds) dateISO8601Millis.parse(date)?.time ?: -1 else dateISO8601.parse(date)?.time ?: -1
23+
}
24+
}

0 commit comments

Comments
 (0)