Skip to content

Commit 3df97b5

Browse files
committed
Fix cargo-ndk invocation and auto-detect Rust PATH (v0.7.0)
Critical Fixes: - Fix cargo-ndk invocation: use 'cargo ndk' instead of direct 'cargo-ndk' binary - Add ANDROID_NDK_HOME environment variable for cargo-ndk compatibility - Auto-detect Rust installation path (~/.cargo/bin, CARGO_HOME) - Improve error handling for missing Rust toolchain - Add graceful version detection fallback These fixes resolve: - 'This binary may only be called via cargo ndk' error - Requirement to manually set cargo.bin in local.properties - Build failures when Rust is not in system PATH
1 parent 01f346b commit 3df97b5

File tree

24 files changed

+194
-263
lines changed

24 files changed

+194
-263
lines changed

build.gradle.kts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ plugins {
44
id("com.gradle.plugin-publish") version "1.3.0"
55
}
66

7-
val pluginId = "io.github.MatrixDev.android-rust"
7+
val pluginId = "io.github.rodroidmods.android-rust"
88

99
group = pluginId
10-
version = "0.6.0"
10+
version = "0.7.0"
1111

1212
@Suppress("UnstableApiUsage")
1313
gradlePlugin {
14-
website = "https://github.com/MatrixDev/GradleAndroidRustPlugin"
15-
vcsUrl = "https://github.com/MatrixDev/GradleAndroidRustPlugin.git"
14+
website = "https://github.com/rodroidmods/GradleAndroidRustPlugin"
15+
vcsUrl = "https://github.com/rodroidmods/GradleAndroidRustPlugin.git"
1616

1717
plugins {
1818
create("AndroidRust") {
@@ -44,4 +44,4 @@ dependencies {
4444

4545
implementation(libs.agp)
4646
implementation(libs.agp.api)
47-
}
47+
}

example/app/build.gradle.kts

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
plugins {
22
alias(libs.plugins.android.application)
33
alias(libs.plugins.kotlin.android)
4+
alias(libs.plugins.compose.compiler)
45
alias(libs.plugins.android.rust)
56
}
67

78
android {
8-
namespace = "dev.matrix.rust"
9-
compileSdk = 33
9+
namespace = "dev.rodroid.rust"
10+
compileSdk = 36
1011
ndkVersion = "25.2.9519653"
1112

1213
defaultConfig {
13-
applicationId = "dev.matrix.rust"
14-
minSdk = 21
15-
targetSdk = 33
14+
applicationId = "dev.rodroid.rust"
15+
minSdk = 26
16+
targetSdk = 36
1617
versionCode = 1
1718
versionName = "1.0"
1819
}
1920

21+
buildFeatures {
22+
compose = true
23+
}
24+
25+
composeOptions {
26+
kotlinCompilerExtensionVersion = "1.5.15"
27+
}
28+
2029
buildTypes {
2130
release {
22-
isMinifyEnabled = false
31+
isMinifyEnabled = true
2332
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"))
2433
proguardFiles("proguard-rules.pro")
2534
}
@@ -32,19 +41,20 @@ android {
3241
}
3342

3443
compileOptions {
35-
sourceCompatibility = JavaVersion.VERSION_1_8
36-
targetCompatibility = JavaVersion.VERSION_1_8
44+
sourceCompatibility = JavaVersion.VERSION_21
45+
targetCompatibility = JavaVersion.VERSION_21
3746
}
3847

39-
kotlinOptions {
40-
jvmTarget = "1.8"
48+
kotlin {
49+
jvmToolchain(21)
4150
}
4251
}
4352

4453
androidRust {
4554
module("library") {
4655
path = file("src/main/jni")
47-
56+
//targets = listOf("arm64", "x86_64", "arm", "x86")
57+
targets = listOf("x86_64")
4858
buildType("release") {
4959
runTests = true
5060
}
@@ -53,10 +63,22 @@ androidRust {
5363
}
5464

5565
dependencies {
56-
implementation("androidx.core:core-ktx:1.9.0")
57-
implementation("androidx.appcompat:appcompat:1.6.1")
58-
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
59-
implementation("androidx.navigation:navigation-fragment-ktx:2.5.3")
60-
implementation("androidx.navigation:navigation-ui-ktx:2.5.3")
61-
implementation("com.google.android.material:material:1.8.0")
66+
implementation(platform(libs.androidx.compose.bom))
67+
68+
implementation(libs.androidx.core.ktx)
69+
implementation(libs.androidx.appcompat)
70+
implementation(libs.androidx.constraintlayout)
71+
implementation(libs.google.material)
72+
73+
implementation(libs.androidx.compose.ui)
74+
implementation(libs.androidx.compose.ui.graphics)
75+
implementation(libs.androidx.compose.ui.tooling.preview)
76+
implementation(libs.androidx.compose.material3)
77+
implementation(libs.androidx.compose.foundation)
78+
implementation(libs.androidx.compose.animation)
79+
implementation(libs.androidx.activity.compose)
80+
implementation(libs.androidx.lifecycle.runtime.compose)
81+
82+
debugImplementation(libs.androidx.compose.ui.tooling)
83+
debugImplementation(libs.androidx.compose.ui.test.manifest)
6284
}

example/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
<application
66
android:allowBackup="true"
7-
android:dataExtractionRules="@xml/data_extraction_rules"
87
android:fullBackupContent="@xml/backup_rules"
98
android:icon="@mipmap/ic_launcher"
109
android:label="@string/app_name"
@@ -13,7 +12,7 @@
1312
android:theme="@style/Theme.GradlePlugin"
1413
tools:targetApi="31">
1514
<activity
16-
android:name="dev.matrix.rust.MainActivity"
15+
android:name="dev.rodroid.rust.MainActivity"
1716
android:exported="true"
1817
android:label="@string/app_name"
1918
android:theme="@style/Theme.GradlePlugin.NoActionBar">

example/app/src/main/java/dev/matrix/rust/MainActivity.kt

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package dev.rodroid.rust
2+
3+
import android.os.Bundle
4+
import androidx.activity.ComponentActivity
5+
import androidx.activity.compose.setContent
6+
import androidx.compose.foundation.background
7+
import androidx.compose.foundation.layout.Box
8+
import androidx.compose.foundation.layout.fillMaxSize
9+
import androidx.compose.material3.MaterialTheme
10+
import androidx.compose.material3.Text
11+
import androidx.compose.material3.Surface
12+
import androidx.compose.runtime.LaunchedEffect
13+
import androidx.compose.ui.Alignment
14+
import androidx.compose.ui.Modifier
15+
import androidx.compose.ui.graphics.Color
16+
import kotlinx.coroutines.delay
17+
import androidx.compose.ui.text.style.TextAlign
18+
19+
class MainActivity : ComponentActivity() {
20+
21+
private external fun callRustCode(): String
22+
23+
override fun onCreate(savedInstanceState: Bundle?) {
24+
super.onCreate(savedInstanceState)
25+
System.loadLibrary("rust_library")
26+
27+
setContent {
28+
MaterialTheme {
29+
Surface(
30+
modifier = Modifier.fillMaxSize(),
31+
color = Color.Black
32+
) {
33+
Box(
34+
modifier = Modifier
35+
.fillMaxSize()
36+
.background(Color.Black),
37+
contentAlignment = Alignment.Center
38+
) {
39+
Text(
40+
text = callRustCode(),
41+
color = Color.Red,
42+
style = MaterialTheme.typography.headlineLarge,
43+
textAlign = TextAlign.Center
44+
)
45+
}
46+
}
47+
}
48+
}
49+
}
50+
}
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
[package]
22
name = "rust_library"
33
version = "0.1.0"
4-
edition = "2021"
4+
edition = "2024"
55

66
[lib]
77
crate-type = ["cdylib"]
88

99
[dependencies]
1010
jni = "0.19.0"
11+
12+
[profile.release]
13+
opt-level = "z"
14+
lto = true
15+
codegen-units = 1
16+
panic = "abort"
17+
strip = "symbols"
18+
debug = false
19+
overflow-checks = false
20+
incremental = false
21+
22+
[profile.release.package."*"]
23+
opt-level = "z"
24+
codegen-units = 1
25+
strip = true
26+
debug = false
27+
overflow-checks = false
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
use jni::JNIEnv;
2-
use jni::objects::JObject;
1+
use jni::{JNIEnv, objects::JObject};
32
use jni::sys::jstring;
43

5-
#[no_mangle]
6-
extern "C" fn Java_dev_matrix_rust_MainActivity_callRustCode(env: JNIEnv, _: JObject) -> jstring {
7-
env.new_string("Hello from rust!").unwrap().into_inner()
8-
}
9-
10-
#[cfg(test)]
11-
mod test {
12-
#[test]
13-
fn test() {
14-
println!("Hello from rust test!")
15-
}
16-
}
4+
#[unsafe(no_mangle)]
5+
pub extern "C" fn Java_dev_rodroid_rust_MainActivity_callRustCode(
6+
env: JNIEnv,
7+
_: JObject,
8+
) -> jstring {
9+
let message = "Hello from Rust";
10+
let java_string = env.new_string(message).expect("Couldn't create java string!");
11+
java_string.into_inner()
12+
}

example/app/src/main/res/layout/activity_main.xml

Lines changed: 0 additions & 34 deletions
This file was deleted.

example/app/src/main/res/layout/content_main.xml

Lines changed: 0 additions & 19 deletions
This file was deleted.

example/app/src/main/res/layout/fragment_first.xml

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)