Skip to content
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

feat(android): setup uniffi infra #8828

Draft
wants to merge 1 commit into
base: canary
Choose a base branch
from
Draft
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
549 changes: 549 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ members = [
"./packages/backend/native",
"./packages/common/native",
"./packages/frontend/native",
"./packages/frontend/native/schema"
"./packages/frontend/native/schema",
"./packages/frontend/mobile-native",
]
resolver = "2"

[workspace.dependencies]
affine_common = { path = "./packages/common/native" }
criterion2 = { version = "2", default-features = false }
anyhow = "1"
chrono = "0.4"
dotenv = "0.15"
Expand Down
61 changes: 57 additions & 4 deletions packages/frontend/apps/android/App/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: 'capacitor.build.gradle'

android {
namespace "app.affine.pro"
namespace "app.affine.pro"
compileSdk rootProject.ext.compileSdkVersion
defaultConfig {
applicationId "app.affine.pro"
Expand All @@ -22,6 +27,13 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

ndkVersion "28.0.12433566"

compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
}

repositories {
Expand All @@ -40,15 +52,56 @@ dependencies {
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
implementation project(':capacitor-cordova-android-plugins')
implementation "net.java.dev.jna:jna:5.15.0@aar"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0"
implementation 'androidx.core:core-ktx:1.15.0'
}

apply from: 'capacitor.build.gradle'

try {
def servicesJSON = file('google-services.json')
if (servicesJSON.text) {
apply plugin: 'com.google.gms.google-services'
}
} catch(Exception e) {
} catch(Exception ignored) {
logger.info("google-services.json not found, google-services plugin not applied. Push Notifications won't work")
}

apply plugin: 'org.mozilla.rust-android-gradle.rust-android'

cargo {
module = "../../../../mobile-native" // Or whatever directory contains your Cargo.toml
libname = "affine_mobile_native" // Or whatever matches Cargo.toml's [package] name.
targets = ["arm64"]
pythonCommand = "python3.12"
targetDirectory = "../../../../../../target"
extraCargoBuildArguments = ["--lib"]
}

kotlin {
compilerOptions {
apiVersion = KotlinVersion.KOTLIN_2_0
jvmTarget = JvmTarget.JVM_17
}
}

afterEvaluate {
// The `cargoBuild` task isn't available until after evaluation.
android.applicationVariants.configureEach { variant ->
def productFlavor = ""
variant.productFlavors.each {
productFlavor += "${it.name.capitalize()}"
}
def buildType = "${variant.buildType.name.capitalize()}"
tasks["generate${productFlavor}${buildType}Assets"].dependsOn(tasks["cargoBuild"])
}
}

android.applicationVariants.configureEach { variant ->
def t = tasks.register("generate${variant.name.capitalize()}UniFFIBindings", Exec) {
workingDir "${project.projectDir}"
// Runs the bindings generation, note that you must have uniffi-bindgen installed and in your PATH environment variable
commandLine 'cargo', 'run', '--bin', 'uniffi-bindgen', 'generate', '--library', "${buildDir}/rustJniLibs/android/arm64-v8a/libaffine_mobile_native.so", '--language', 'kotlin', '--out-dir', "${project.projectDir}/src/main/java"
dependsOn("cargoBuild")
}
variant.javaCompileProvider.get().dependsOn(t)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ android {
apply from: "../capacitor-cordova-android-plugins/cordova.variables.gradle"
dependencies {


}


Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package app.affine.pro

import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.annotation.RequiresApi
import com.getcapacitor.BridgeActivity
import uniffi.affine_mobile_native.hashcashMint;

class MainActivity : BridgeActivity() {
init {
System.loadLibrary("affine_mobile_native")
}

external fun hello(): Long

@RequiresApi(Build.VERSION_CODES.R)
override fun onCreate(savedInstanceState: Bundle?) {
val ret = hello()
println("ret from rust side $ret")
val resource = "hello"
val hashed = hashcashMint(resource)
println("Hashed string from Rust $hashed")
super.onCreate(savedInstanceState)
}
}
Loading
Loading