mozilla / rust-android-gradle

Apache License 2.0
1.03k stars 67 forks source link

Add build.gradle.kts plugin setup specifics to readme #133

Open 0x330a opened 9 months ago

0x330a commented 9 months ago

Needed to do some digging around to how to set up a build.gradle.kts with a lot of trial and error, I think I've got it mostly.

to apply the plugin in a module's build.gradle.kts:

plugins {
    // ...
    id("org.mozilla.rust-android-gradle.rust-android") version "0.9.3"
}

to configure the plugin in a module's build.gradle.kts

extensions.configure(CargoExtension::class) {
    module = "[module name]"
    libname = "[libname]"
    // ...
}

to set a build dependency (This seems to work for me but not sure if it's the best method):

tasks.preBuild.configure {
    dependsOn.add(tasks.withType(CargoBuildTask::class.java))
}

Additionally, it seems that the most recent template for adding plugins in an Android build.gradle.kts setup puts the plugin (without applying) in the project level build.gradle.kts like so:

plugins {
    // ...
    id("org.mozilla.rust-android-gradle.rust-android") version "0.9.3" apply false
}

Could probably do a PR myself but not sure where exactly these instructions would best fit

MarijnS95 commented 8 months ago

Thanks a lot for these details! I ended up changing the build-time dependencies to apply to merge{Debug,Release}JniLibFolders instead using MergeSourceSetFolders as described in https://github.com/mozilla/rust-android-gradle/issues/118#issuecomment-1777952460, and had a little struggle to figure out how to turn an ["arm64"] array into a list. I can still configure the extension directly via a cargo object in the root though:

cargo {
    module = "../android_vulkan_interop"
    libname = "android_vulkan_interop"
    targets = listOf("arm64")
}

project.afterEvaluate {
    tasks.withType(com.nishtahir.CargoBuildTask::class)
        .forEach { buildTask ->
            tasks.withType(com.android.build.gradle.tasks.MergeSourceSetFolders::class)
                .configureEach {
                    this.inputs.dir(
                        layout.buildDirectory.dir("rustJniLibs" + File.separatorChar + buildTask.toolchain!!.folder)
                    )
                    this.dependsOn(buildTask)
                }
        }
}

See here for the whole project: https://github.com/MarijnS95/AndroidVulkanInterop

xz-dev commented 8 months ago

Thanks, the last step is very important and also we need add pluginManagement as that