Skip to content

Commit

Permalink
feat: set up android module bare bone (#7)
Browse files Browse the repository at this point in the history
* feat:set up android module barebone

* feat: re-structure and rename main to core module

* patch: rename

* patch: resolve comments
  • Loading branch information
qingzhuozhen authored Feb 25, 2022
1 parent 341fcf1 commit a8a8dc9
Show file tree
Hide file tree
Showing 45 changed files with 398 additions and 125 deletions.
56 changes: 56 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
}

android {
compileSdk 31

defaultConfig {
multiDexEnabled true

minSdk 16
targetSdk 31
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
testOptions {
unitTests {
includeAndroidResources = true
}
}
}

dependencies {
implementation project(':core')
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'

testImplementation 'io.mockk:mockk:1.10.6'
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
testImplementation 'org.junit.jupiter:junit-jupiter:5.7.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.7.2'
testImplementation 'org.robolectric:robolectric:4.7.3'
testImplementation 'androidx.test:core:1.4.0'
}
Empty file added android/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions android/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.amplitude.android;

import android.content.Context;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.amplitude.android.test", appContext.getPackageName());
}
}
5 changes: 5 additions & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amplitude.android">

</manifest>
13 changes: 13 additions & 0 deletions android/src/main/java/com/amplitude/android/Amplitude.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.amplitude.android

import com.amplitude.core.Amplitude
import com.amplitude.core.platform.plugins.AmplitudeDestination

open class Amplitude(
configuration: Configuration
): Amplitude(configuration) {

override fun build() {
add(AmplitudeDestination())
}
}
24 changes: 24 additions & 0 deletions android/src/main/java/com/amplitude/android/Configuration.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.amplitude.android

import android.content.Context
import com.amplitude.core.Configuration
import com.amplitude.core.LoggerProvider
import com.amplitude.core.StorageProvider
import com.amplitude.android.utilities.AndroidLoggerProvider
import com.amplitude.android.utilities.AndroidStorageProvider
import com.amplitude.core.events.BaseEvent

class Configuration(
apiKey: String,
context: Context,
flushQueueSize: Int = FLUSH_QUEUE_SIZE,
flushIntervalMillis: Int = FLUSH_INTERVAL_MILLIS,
optOut: Boolean = false,
storageProvider: StorageProvider = AndroidStorageProvider(),
loggerProvider: LoggerProvider = AndroidLoggerProvider(),
minIdLength: Int? = null,
callback: ((BaseEvent) -> Unit)? = null,
useAdvertisingIdForDeviceId: Boolean = false,
useAppSetIdForDeviceId: Boolean = false,
enableCoppaControl: Boolean = false
) : Configuration(apiKey, flushQueueSize, flushIntervalMillis, optOut, storageProvider, loggerProvider, minIdLength, callback)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.amplitude.android.plugins

import com.amplitude.core.Amplitude
import com.amplitude.core.events.BaseEvent
import com.amplitude.core.platform.Plugin

class AndroidContextPlugin : Plugin {
override val type: Plugin.Type = Plugin.Type.Before
override lateinit var amplitude: Amplitude

override fun execute(event: BaseEvent): BaseEvent? {
return event
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.amplitude.android.plugins

import com.amplitude.core.Amplitude
import com.amplitude.core.platform.Plugin

class AndroidLifecyclePlugin : Plugin {
override val type: Plugin.Type = Plugin.Type.Utility
override lateinit var amplitude: Amplitude
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.amplitude.android.utilities

import com.amplitude.core.Amplitude
import com.amplitude.core.Logger
import com.amplitude.core.LoggerProvider

class AndroidLogger() : Logger {
override var logMode: Logger.LogMode = Logger.LogMode.INFO

override fun debug(message: String) {
TODO("Not yet implemented")
}

override fun error(message: String) {
TODO("Not yet implemented")
}

override fun info(message: String) {
TODO("Not yet implemented")
}

override fun warn(message: String) {
TODO("Not yet implemented")
}
}

class AndroidLoggerProvider() : LoggerProvider {
override fun getLogger(amplitude: Amplitude): Logger {
return AndroidLogger()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.amplitude.android.utilities

import com.amplitude.core.Amplitude
import com.amplitude.core.Storage
import com.amplitude.core.StorageProvider
import com.amplitude.core.events.BaseEvent

class AndroidStorage(
val amplitude: Amplitude
) : Storage {
override fun write(event: BaseEvent) {
TODO("Not yet implemented")
}

override fun rollover() {
TODO("Not yet implemented")
}

override fun getEvents(): List<String> {
TODO("Not yet implemented")
}
}

class AndroidStorageProvider: StorageProvider {
override fun getStorage(amplitude: Amplitude): Storage {
return AndroidStorage(amplitude)
}
}
17 changes: 17 additions & 0 deletions android/src/test/java/com/amplitude/android/ExampleUnitTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.amplitude.android;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
}
}
12 changes: 4 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ buildscript {
ext.kotlin_version = "1.5.10"
repositories {
mavenCentral()
google()
}

dependencies {
classpath 'com.android.tools.build:gradle:7.0.4'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand All @@ -17,16 +19,10 @@ allprojects{
maven { url "https://kotlin.bintray.com/kotlinx" }
}

apply plugin: "kotlin"

compileKotlin {
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
freeCompilerArgs = ['-Xjvm-default=enable']
jvmTarget = "1.8"
javaParameters = true
}
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
}
12 changes: 7 additions & 5 deletions main/build.gradle → core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id 'java'
id 'kotlin'
id 'org.jetbrains.kotlin.jvm'
}

Expand All @@ -13,15 +14,16 @@ repositories {
mavenCentral()
}

apply plugin: 'idea'

sourceSets.main.java.srcDirs = ['java']

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
// MAIN DEPS
implementation 'org.json:json:20211205'
compileOnly 'org.json:json:20211205'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2'

testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.2'

testImplementation platform("org.junit:junit-bom:5.7.2")
testImplementation "org.junit.jupiter:junit-jupiter"
}

test {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package com.amplitude

import com.amplitude.events.BaseEvent
import com.amplitude.events.Identify
import com.amplitude.events.Revenue
import com.amplitude.events.RevenueEvent
import com.amplitude.platform.Plugin
import com.amplitude.platform.Timeline
import com.amplitude.platform.plugins.AmplitudeDestination
import com.amplitude.platform.plugins.ContextPlugin
package com.amplitude.core

import com.amplitude.core.events.BaseEvent
import com.amplitude.core.events.Identify
import com.amplitude.core.events.Revenue
import com.amplitude.core.events.RevenueEvent
import com.amplitude.core.platform.Plugin
import com.amplitude.core.platform.Timeline
import com.amplitude.core.platform.plugins.AmplitudeDestination
import com.amplitude.core.platform.plugins.ContextPlugin
import kotlinx.coroutines.*
import java.util.concurrent.Executors

open class Amplitude internal constructor(
val configuration: com.amplitude.Configuration,
val store: com.amplitude.State,
val configuration: Configuration,
val store: State,
val amplitudeScope: CoroutineScope = CoroutineScope(SupervisorJob()),
val amplitudeDispatcher: CoroutineDispatcher = Executors.newCachedThreadPool().asCoroutineDispatcher(),
val networkIODispatcher: CoroutineDispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher(),
val storageIODispatcher: CoroutineDispatcher = Executors.newFixedThreadPool(2).asCoroutineDispatcher()
){

internal val timeline: Timeline
val storage: com.amplitude.Storage
val logger: com.amplitude.Logger
val storage: Storage
val logger: Logger

init {
require(configuration.isValid()) { "invalid configuration" }
Expand All @@ -35,9 +35,9 @@ open class Amplitude internal constructor(
/**
* Public Constructor
*/
constructor(configuration: com.amplitude.Configuration) : this(configuration, com.amplitude.State())
constructor(configuration: Configuration) : this(configuration, State())

internal fun build() {
open fun build() {
add(ContextPlugin())
add(AmplitudeDestination())

Expand Down Expand Up @@ -100,12 +100,12 @@ open class Amplitude internal constructor(
}
}

fun add(plugin: Plugin) : com.amplitude.Amplitude {
fun add(plugin: Plugin) : Amplitude {
this.timeline.add(plugin)
return this
}

fun remove(plugin: Plugin): com.amplitude.Amplitude {
fun remove(plugin: Plugin): Amplitude {
this.timeline.remove(plugin)
return this
}
Expand Down
Loading

0 comments on commit a8a8dc9

Please sign in to comment.