Skip to content

android example #1349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
### Idea examples
* [plugin example](kotlin-dataframe-plugin-example) IDEA project with a
[Kotlin DataFrame Compiler Plugin](https://kotlin.github.io/dataframe/compiler-plugin.html) example.
* [android example](android-example) A minimal Android project showcasing integration with Kotlin DataFrame.
Also includes [Kotlin DataFrame Compiler Plugin](https://kotlin.github.io/dataframe/compiler-plugin.html).
* [movies](idea-examples/movies) Using extension properties [Access API](https://kotlin.github.io/dataframe/apilevels.html) to perform a data cleaning task
* [titanic](idea-examples/titanic)
* [youtube](idea-examples/youtube)
Expand Down
15 changes: 15 additions & 0 deletions examples/android-example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
13 changes: 13 additions & 0 deletions examples/android-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 📱 Android Example

A minimal Android project showcasing integration with **Kotlin DataFrame**.

<p align="center">
<img src="screen.jpg" alt="App screenshot" height="320"/>
</p>

It also includes the [Kotlin DataFrame Compiler Plugin](https://kotlin.github.io/dataframe/compiler-plugin.html).

> **Note:** The generated [extension properties](https://kotlin.github.io/dataframe/extensionpropertiesapi.html)
> may not be highlighted correctly in current stable versions of Android Studio due to limited IDE support.
> However, they work as expected at runtime and during compilation.
1 change: 1 addition & 0 deletions examples/android-example/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
86 changes: 86 additions & 0 deletions examples/android-example/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.compose)
kotlin("plugin.dataframe") version "2.2.20-Beta1"
}

android {
namespace = "com.example.myapplication"
compileSdk = 36

defaultConfig {
applicationId = "com.example.myapplication"
minSdk = 26
targetSdk = 36
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
compose = true
}
packaging {
resources {
pickFirsts += listOf(
"META-INF/AL2.0",
"META-INF/LGPL2.1",
"META-INF/ASL-2.0.txt",
"META-INF/LICENSE.md",
"META-INF/NOTICE.md",
"META-INF/LGPL-3.0.txt",
"META-INF/thirdparty-LICENSE",
)
excludes += listOf(
"META-INF/kotlin-jupyter-libraries/libraries.json",
"META-INF/{INDEX.LIST,DEPENDENCIES}",
"{draftv3,draftv4}/schema",
"arrow-git.properties",
)
}
}
}

dependencies {

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.activity.compose)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.ui)
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.ui.test.junit4)
debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)

// TODO update version
// Core Kotlin DataFrame API & JSON IO.
// See custom Gradle setup:
// https://kotlin.github.io/dataframe/setupcustomgradle.html
implementation("org.jetbrains.kotlinx:dataframe-core:1.0.0-dev-7831")
implementation("org.jetbrains.kotlinx:dataframe-json:1.0.0-dev-7831")
}
21 changes: 21 additions & 0 deletions examples/android-example/app/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,24 @@
package com.example.myapplication

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

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

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.example.myapplication", appContext.packageName)
}
}
27 changes: 27 additions & 0 deletions examples/android-example/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication" >
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.MyApplication" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package com.example.myapplication

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
import org.jetbrains.kotlinx.dataframe.api.cast
import org.jetbrains.kotlinx.dataframe.api.dataFrameOf
import org.jetbrains.kotlinx.dataframe.api.filter
import org.jetbrains.kotlinx.dataframe.api.rows

@DataSchema
data class Person(
val age: Int,
val name: String
)

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()

val df = dataFrameOf(
"name" to listOf("Andrei", "Nikita", "Jolan"),
"age" to listOf(22, 16, 37)
).cast<Person>()

setContent {
MaterialTheme {
Surface(modifier = Modifier.fillMaxSize()) {
DataFrameScreen(df)
}
}
}
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be nice to add a preview too:

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    val df = dataFrameOf(
        "name" to listOf("Andrei", "Nikita", "Jolan"),
        "age" to listOf(22, 16, 37)
    ).cast<Person>()
    DataFrameScreen(df)
}

That way people don't have to build+run the app on an emulator/device but they can preview what their dataframe looks like in Android Studio

@Preview(showBackground = true)
@Composable
fun DefaultDataFrameScreenPreview() {
val df = dataFrameOf(
"name" to listOf("Andrei", "Nikita", "Jolan"),
"age" to listOf(22, 16, 37)
).cast<Person>()
DataFrameScreen(df)
}

@Composable
fun DataFrameScreen(df: DataFrame<Person>) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(top = 48.dp, start = 16.dp, end = 16.dp)
) {
Text(
text = "Kotlin DataFrame on Android",
style = MaterialTheme.typography.headlineSmall,
modifier = Modifier.padding(bottom = 16.dp)
)

Text(
text = "df",
modifier = Modifier
.background(color = Color.LightGray)
.padding(2.dp)
)

DataFrameTable(df)

Text(
text = "df.filter { age >= 20 }",
modifier = Modifier
.background(color = Color.LightGray)
.padding(2.dp)
)

DataFrameTable(df.filter { age >= 20 })
}
}

@Preview(showBackground = true)
@Composable
fun DefaultDataFrameTablePreview() {
val df = dataFrameOf(
"name" to listOf("Andrei", "Nikita", "Jolan"),
"age" to listOf(22, 16, 37)
).cast<Person>()
DataFrameTable(df)
}

@Composable
fun DataFrameTable(df: DataFrame<*>) {
val columnNames = df.columnNames()

// Header
Row {
for (name in columnNames) {
Text(
text = name,
modifier = Modifier
.weight(1f)
.padding(4.dp),
style = MaterialTheme.typography.labelLarge
)
}
}

Spacer(Modifier.height(4.dp))

// Rows
LazyColumn {
items(df.rows().toList()) { row ->
Row {
for (cell in row.values()) {
Text(
text = cell.toString(),
modifier = Modifier
.weight(1f)
.padding(4.dp)
)
}
}
}
}
}
Loading