Convert shared code and android library to Java

There may still be some use cases where having the library in Java makes sense, such as to keep the aar file as small as possible. The server code is also in Java and while size isn't a constraint, I'd like to keep memory down where possible by avoiding unnecessary dependencies being loaded into the classpath at runtime. Whether or not excluding Kotlin makes a huge difference may even be a trivial concern but I don't have the time or desire to benchmark it.

Signed-off-by: William Brawner <me@wbrawner.com>
This commit is contained in:
William Brawner 2020-09-04 14:20:02 -07:00
parent a7c682bed0
commit 10a5bf9e7b
6 changed files with 169 additions and 59 deletions

View file

@ -1,6 +1,4 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
repositories {
jcenter()
@ -8,10 +6,10 @@ repositories {
}
android {
compileSdkVersion(29)
compileSdkVersion(30)
defaultConfig {
minSdkVersion(23)
targetSdkVersion(29)
targetSdkVersion(30)
versionCode 1
versionName "0.1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
@ -26,6 +24,4 @@ android {
dependencies {
implementation project(":shared")
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
implementation "io.ktor:ktor-client-android:$ktorVersion"
}

View file

@ -1,20 +1,8 @@
apply plugin: 'kotlin'
apply plugin: 'java'
repositories {
mavenCentral()
}
sourceSets {
main {
java.srcDir("src")
resources.srcDir("resources")
}
test {
java.srcDir("test")
resources.srcDir("testresources")
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
}

View file

@ -1,6 +1,25 @@
package com.wbrawner.flayre
package com.wbrawner.flayre;
class App(
val id: String = randomId(32),
val name: String
)
import static com.wbrawner.flayre.Utils.randomId;
class App {
private final String id;
private final String name;
App(String name) {
this(randomId(32), name);
}
App(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
}

View file

@ -0,0 +1,129 @@
package com.wbrawner.flayre;
import java.util.Date;
import static com.wbrawner.flayre.Utils.randomId;
class Event {
private final String id;
private final String appId;
private final Date date;
private final InteractionType type;
private final String userAgent;
private final String platform;
private final String manufacturer;
private final String model;
private final String version;
private final String locale;
private final String sessionId;
private final String data;
public Event(String appId,
Date date,
InteractionType type,
String userAgent,
String platform,
String manufacturer,
String model,
String version,
String locale,
String sessionId,
String data) {
this(
randomId(32),
appId,
date,
type,
userAgent,
platform,
manufacturer,
model,
version,
locale,
sessionId,
data
);
}
public Event(String id,
String appId,
Date date,
InteractionType type,
String userAgent,
String platform,
String manufacturer,
String model,
String version,
String locale,
String sessionId,
String data) {
this.id = id;
this.appId = appId;
this.date = date;
this.type = type;
this.userAgent = userAgent;
this.platform = platform;
this.manufacturer = manufacturer;
this.model = model;
this.version = version;
this.locale = locale;
this.sessionId = sessionId;
this.data = data;
}
public String getId() {
return id;
}
public String getAppId() {
return appId;
}
public Date getDate() {
return date;
}
public InteractionType getType() {
return type;
}
public String getUserAgent() {
return userAgent;
}
public String getPlatform() {
return platform;
}
public String getManufacturer() {
return manufacturer;
}
public String getModel() {
return model;
}
public String getVersion() {
return version;
}
public String getLocale() {
return locale;
}
public String getSessionId() {
return sessionId;
}
public String getData() {
return data;
}
enum InteractionType {
VIEW,
CLICK,
ERROR,
CRASH
}
}

View file

@ -1,26 +0,0 @@
package com.wbrawner.flayre
import java.util.*
class Event(
val id: String = randomId(32),
val appId: String,
val date: Date,
val type: InteractionType,
val userAgent: String? = null, // For web use only
val platform: String? = null,
val manufacturer: String? = null,
val model: String? = null,
val version: String? = null,
val locale: String? = null,
val sessionId: String? = null,
val data: String? = null
) {
enum class InteractionType {
VIEW,
CLICK,
ERROR,
CRASH
}
}

View file

@ -1,12 +1,16 @@
package com.wbrawner.flayre
package com.wbrawner.flayre;
import kotlin.random.Random
import java.util.Random;
private val characters = ('a'..'z') + ('A'..'Z') + (0..9)
fun randomId(length: Int): String {
val id = StringBuilder("")
while (id.length < length) {
id.append(characters[Random.nextInt(0, characters.size)])
public final class Utils {
private static final String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
public static String randomId(int length) {
final var id = new StringBuilder();
final var random = new Random();
while (id.length() < length) {
id.append(characters.charAt(random.nextInt(characters.length())));
}
return id.toString();
}
return id.toString()
}