Skip to content

Commit

Permalink
feat(android): setup uniffi infra
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Nov 22, 2024
1 parent cd30e1a commit 2288985
Show file tree
Hide file tree
Showing 17 changed files with 1,856 additions and 28 deletions.
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
65 changes: 63 additions & 2 deletions packages/frontend/apps/android/App/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile

apply plugin: 'com.android.application'

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

ndkVersion "28.0.12433566"

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

repositories {
Expand All @@ -40,15 +51,65 @@ 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.7.1"
implementation 'androidx.core:core-ktx:1.15.0'
}

apply plugin: 'kotlin-android'
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", "arm", "darwin-aarch64", "x86_64"]
pythonCommand = "python3.12"
targetDirectory = "../../../../../../target"
extraCargoBuildArguments = ["--lib"]
}

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

tasks.withType(KotlinJvmCompile).configureEach {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_17)
freeCompilerArgs.add("-opt-in=kotlin.RequiresOptIn")
}
}

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"
}
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,22 @@
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")
// }

@RequiresApi(Build.VERSION_CODES.R)
override fun onCreate(savedInstanceState: Bundle?) {
val resource = "hello";
val hashed = hashcashMint(resource)
Log.d("MainActivity", "onCreate: $hashed")
super.onCreate(savedInstanceState)
}
}
Loading

0 comments on commit 2288985

Please sign in to comment.