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

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

Open
0x330a opened this issue Sep 12, 2023 · 6 comments
Open

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

0x330a opened this issue Sep 12, 2023 · 6 comments

Comments

@0x330a
Copy link

0x330a commented Sep 12, 2023

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
Copy link

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 #118 (comment), 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
Copy link

xz-dev commented Oct 29, 2023

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

@Chiichen
Copy link

Chiichen commented Jul 30, 2024

You need to place the cargo gradle plugin before others. Otherwise, a wiered "Duplicate Resource" error would likely occur. Details here

@MarijnS95
Copy link

Thanks for linking that @Chiichen, that indeed fixes the Duplicate Resource error when upgrading AGP (com.android.application) beyond 8.2.0, to i.e. the mentioned 8.4.1 or 8.5.0. The dependency change linked above is still required too.

@Chiichen
Copy link

Thanks for linking that @Chiichen, that indeed fixes the Duplicate Resource error when upgrading AGP (com.android.application) beyond 8.2.0, to i.e. the mentioned 8.4.1 or 8.5.0. The dependency change linked above is still required too.

Thanks for your answer! However, I am using AGP with version 8.5.0 but this error still appears in my project. Besides, my build.gradle.kts is as follows, and it seems that it can work properly without adding the rustJniLibs path (is it fixed by the latest version?)

cargo {
  //...
}

project.afterEvaluate {
    tasks.withType(com.nishtahir.CargoBuildTask::class)
        .forEach { buildTask ->
            tasks.withType(com.android.build.gradle.tasks.MergeSourceSetFolders::class)
                .configureEach {
                    this.dependsOn(buildTask)
                }
        }
}

@MarijnS95
Copy link

MarijnS95 commented Jul 30, 2024

@Chiichen this might be caused by a prior successful build: the library will be picked up in the APK, but it will always be an older version. If you skip adding rustJniLibs to the inputs of MergeSourceSetFolders, and do a clean build, the file should be missing.

Note that I've only tested this on my Gradle project, I'll test it on a gradle.kts setup soon. UPDATE: The Duplicate Resources error is solved in the same way: MarijnS95/AndroidVulkanInterop@d05231c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants