mozilla / rust-android-gradle

Apache License 2.0
1.03k stars 67 forks source link

How to add two crates? #123

Closed ssrlive closed 1 year ago

ssrlive commented 1 year ago

I have two crates, and I want to build it into two .so files. How I do it? I hope the follow settings can works, but when I clear up my project, I find that only the last project built (libmylib2.so), the first lib not built.

How to resolve it?

cargo {
    module  = "../mylib1"
    libname = "mylib1"
    targets = ["arm64", "x86", "x86_64", "arm"]
    prebuiltToolchains = true
    apiLevel = 21
}

cargo {
    module  = "../mylib2"
    libname = "mylib2"
    targets = ["arm64", "x86", "x86_64", "arm"]
    prebuiltToolchains = true
    apiLevel = 21
}
ncalexan commented 1 year ago

Hi! This isn't supported by the plugin; there's just a single cargo block (per Gradle file/project). There's no really hard reason for this, it's just a lot harder to write Gradle plugins that support the full generality you're looking for.

If you really want to achieve this, I can think of two ways. First, configure Cargo to produce two .so files and then use plugin affordances like targetIncludes to pick up both outputs. Second, arrange for two Gradle projects (i.e., two Android library projects or one Android library project used within an Android application project) two get both .so files included.

I'm going to transmute this ticket in case we ever implement this. Good luck working around!

ssrlive commented 1 year ago

Thanks for your reply, looking forward to good news.

ssrlive commented 1 year ago

I achieved my purpose. In my root Cargo.toml file

[workspace]
members = ["./mylib1", "./mylib2"]

and in my android/app/build.gradle file

cargo {
    module  = "../../rust"
    libname = "mylib1"
    targets = ["arm64", "x86", "x86_64", "arm"]
    prebuiltToolchains = true
    apiLevel = 21
    targetIncludes = ['libmylib1.so', 'libmylib2.so']
}

All things looks perfect. here is my demo Thanks alot.

ncalexan commented 1 year ago

Great! That looks as I expect.