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

Implement gradle rust plugin #7319

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Pururun
Copy link
Contributor

@Pururun Pururun commented Dec 10, 2024

PR to implement gradle rust plugin. This enables the android app to essentially do all required steps for building the app within the gradle scope. The implementation mostly relies on the rust gradle plugin to achieve this, but some rust related things are run as standalone gradle tasks.

Details:

  • Configured the cargo build gradle task provided by the plugin to fit our build process. It uses the gradle starter task to difference between our different types of build which is a bit of a hack. It also checks version name which is behaviour moved from the old build-apk.sh script.
  • Added gradle task to generate the relay list.
  • Added gradle task for doing cargo clean that is run before every gradle clean. This behavior can be disabled.
  • Updated and moved build-apk.sh script to the android folder.
  • Updated ci to only use gradle tasks instead of cargo calls in the android build process.
  • Fixed how wireguard-go-rs module detects if it needs to rebuild or not, so that cargo will not rebuild if you do not making any changes to the rust code.

This change is Reviewable

@Pururun Pururun requested review from kl, Rawa and albin-mullvad December 10, 2024 22:20
Copy link

linear bot commented Dec 10, 2024

@Pururun Pururun added the Android Issues related to Android label Dec 10, 2024
@MarkusPettersson98
Copy link
Contributor

MarkusPettersson98 commented Dec 11, 2024

Things left to be decided:

* Can we use a symlink for the maybenot_machines? Will it work on windows?

This should no longer be needed when #7158 is merged 😊

Copy link
Contributor

@MarkusPettersson98 MarkusPettersson98 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 16 files reviewed, 1 unresolved discussion (waiting on @Pururun)


android/.gitignore line 6 at r2 (raw file):

local.properties
# We want to generate relays.json not store it
app/src/main/assets/relays.json

This seems applicable to all platforms! Should we move the ignore to the root .gitignore? 😊

Code quote:

# We want to generate relays.json not store it
app/src/main/assets/relays.json

Copy link
Contributor

@MarkusPettersson98 MarkusPettersson98 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 16 files reviewed, 5 unresolved discussions (waiting on @Pururun)


wireguard-go-rs/build.rs line 236 at r2 (raw file):

    let mut move_command = Command::new("cp");
    move_command
        .arg("-p")

⛏️ move_command should probably be renamed now that cp is used instead of mv 😉

Also, could we document why the -p flag is important? 🙏

Code quote:

    let mut move_command = Command::new("cp");
    move_command
        .arg("-p")

android/build-apk.sh line 21 at r2 (raw file):

RUN_PLAY_PUBLISH_TASKS="no"
PLAY_PUBLISH_TASKS=()
SKIP_STRIPPING=${SKIP_STRIPPING:-"no"}

⛏️ This seems to be unused (🎉)

Code quote:

SKIP_STRIPPING=${SKIP_STRIPPING:-"no"}

android/build-apk.sh line 56 at r2 (raw file):

    elif [[ "$PRODUCT_VERSION" == *"-alpha"* ]]; then
        echo "Removing old Rust build artifacts"
        cargo clean

Is the intention to get rid of all explicit invocations of cargo in this script? Is cargo clean run when invoking gradlew clean? 😊

Code quote:

        echo "Removing old Rust build artifacts"
        cargo clean

android/build-apk.sh line 61 at r2 (raw file):

        PLAY_PUBLISH_TASKS=(publishPlayStagemoleReleaseBundle)
    else
        cargo clean

dito

Code quote:

cargo clean

Copy link
Contributor Author

@Pururun Pururun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is great. :)

Reviewable status: 0 of 16 files reviewed, 3 unresolved discussions (waiting on @MarkusPettersson98)


wireguard-go-rs/build.rs line 236 at r2 (raw file):

Previously, MarkusPettersson98 (Markus Pettersson) wrote…

⛏️ move_command should probably be renamed now that cp is used instead of mv 😉

Also, could we document why the -p flag is important? 🙏

Done


android/build-apk.sh line 21 at r2 (raw file):

Previously, MarkusPettersson98 (Markus Pettersson) wrote…

⛏️ This seems to be unused (🎉)

Nice catch!. Removed.


android/build-apk.sh line 56 at r2 (raw file):

Previously, MarkusPettersson98 (Markus Pettersson) wrote…

Is the intention to get rid of all explicit invocations of cargo in this script? Is cargo clean run when invoking gradlew clean? 😊

Cargo clean is not invoked when running gradle clean. Maybe we want to do it, I'll add it to the list.

Copy link
Contributor Author

@Pururun Pururun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 17 files reviewed, 3 unresolved discussions (waiting on @MarkusPettersson98)


android/.gitignore line 6 at r2 (raw file):

Previously, MarkusPettersson98 (Markus Pettersson) wrote…

This seems applicable to all platforms! Should we move the ignore to the root .gitignore? 😊

We decided to move the file back into build so it will be ignored.

@Pururun Pururun force-pushed the implement-gradle-rust-plugin-droid-1377 branch from d096cde to 1b940e2 Compare December 12, 2024 11:38
@Rawa Rawa force-pushed the implement-gradle-rust-plugin-droid-1377 branch 2 times, most recently from 06ebd1d to e18506b Compare December 12, 2024 15:39
@Pururun Pururun force-pushed the implement-gradle-rust-plugin-droid-1377 branch 2 times, most recently from a2cd891 to 6786224 Compare December 17, 2024 14:20
@Rawa Rawa force-pushed the implement-gradle-rust-plugin-droid-1377 branch from 2702f7d to cdea054 Compare December 18, 2024 12:57
@Pururun Pururun force-pushed the implement-gradle-rust-plugin-droid-1377 branch from cdea054 to eded09b Compare December 18, 2024 13:09
@Rawa Rawa force-pushed the implement-gradle-rust-plugin-droid-1377 branch 2 times, most recently from 6509729 to 5c4a2cb Compare December 18, 2024 15:07
@Rawa Rawa marked this pull request as ready for review December 18, 2024 15:21
@Rawa Rawa requested a review from faern as a code owner December 18, 2024 15:21
Copy link
Contributor

@Rawa Rawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 7 of 16 files at r1, 2 of 4 files at r3, 2 of 4 files at r7, 2 of 5 files at r8, 2 of 5 files at r9, 2 of 3 files at r10, 1 of 1 files at r11, all commit messages.
Reviewable status: all files reviewed, 12 unresolved discussions (waiting on @MarkusPettersson98 and @Pururun)


android/app/build.gradle.kts line 298 at r11 (raw file):

        }
    }
    exec = { spec, _ -> println(spec.commandLine) }

Should maybe remove?


android/app/build.gradle.kts line 315 at r11 (raw file):

        val output = standardOutput as ByteArrayOutputStream
        // Create file if needed
        File("$extraAssetsDirectory").mkdirs()

Should we reference parent of relayListPath maybe?


android/app/build.gradle.kts line 317 at r11 (raw file):

        File("$extraAssetsDirectory").mkdirs()
        File("$extraAssetsDirectory/relays.json").createNewFile()
        FileOutputStream("$extraAssetsDirectory/relays.json").use { it.write(output.toByteArray()) }

We should reuse the relayListPath here, right now we seem to construct it multiple times.


android/app/build.gradle.kts line 320 at r11 (raw file):

        // Old ensure exists tasks
        if (!relayListPath.exists()) {

Does this serve any purpose? Shouldn't it exist if the thing above successfully ran?


android/app/build.gradle.kts line 327 at r11 (raw file):

fun isReleaseBuild() =
    gradle.startParameter.getTaskNames().any { it.contains("release", ignoreCase = true) }

We probably should add a comment and DROID-X issue link to something in our backlog to address this at some point, or what do you think?


android/build.gradle.kts line 21 at r11 (raw file):

    alias(libs.plugins.detekt) apply true
    alias(libs.plugins.dependency.versions) apply true

Nit: remove whitespace


android/build.gradle.kts line 74 at r11 (raw file):

        classpath("$prebuilt:[email protected]")

        classpath("org.mozilla.rust-android-gradle:plugin:${libs.versions.rust.android.gradle}")

Should this also be extracted to some toml file??


wireguard-go-rs/build.rs line 174 at r11 (raw file):

    // or if the libwg.so file has been changed. The latter is required since the
    // libwg.so file could be deleted. It however means that this build will need
    // to run two times before it is properly cached.

We should probably add some issue to track this post merging this. Unless we have any idea on how we can make this work for real.


.github/workflows/android-app.yml line 171 at r11 (raw file):

          - arch: "armv7"
            abi: "armeabi-v7a"
            taskVariant: "Arm"

Generally we seem to name the keys with dashes instead of camelCase, should we change?

Copy link
Contributor

@Rawa Rawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 14 unresolved discussions (waiting on @MarkusPettersson98 and @Pururun)


android/build-apk.sh line 64 at r11 (raw file):

fi

pushd "$SCRIPT_DIR"

We should already be in the correct folder I believe so we should be able to remove this pushd & popd below,


android/app/build.gradle.kts line 332 at r11 (raw file):

    val localProperties = gradleLocalProperties(rootProject.projectDir, providers)
    val versionName = generateVersionName(localProperties)
    return versionName.contains("dev", ignoreCase = true) ||

Does it make sense to ignore case? Or would it better to safeg

Copy link
Contributor

@MarkusPettersson98 MarkusPettersson98 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 3 of 16 files at r1, 1 of 4 files at r3, 1 of 4 files at r7, all commit messages.
Reviewable status: all files reviewed, 11 unresolved discussions (waiting on @Pururun and @Rawa)


wireguard-go-rs/build.rs line 236 at r2 (raw file):

Previously, Pururun (Jonatan Rhodin) wrote…

Done

⛏️ It should say -p, not P. p and P are different flags in cp

Also, it would be nice if the comment would mention that we use cp -p because we want to preserve ownership & timestamp to not re-trigger cargo accidentally :)


wireguard-go-rs/build.rs line 174 at r11 (raw file):

Previously, Rawa (David Göransson) wrote…

We should probably add some issue to track this post merging this. Unless we have any idea on how we can make this work for real.

I agree. Also, I think this deserves to be prefixed with "FIXME" to stand out more / increase grep-ability.

Copy link
Contributor Author

@Pururun Pururun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 17 of 18 files reviewed, 10 unresolved discussions (waiting on @Rawa)


.github/workflows/android-app.yml line 171 at r11 (raw file):

Previously, Rawa (David Göransson) wrote…

Generally we seem to name the keys with dashes instead of camelCase, should we change?

Good catch, fixed.


android/app/build.gradle.kts line 298 at r11 (raw file):

Previously, Rawa (David Göransson) wrote…

Should maybe remove?

Indeed, we could add it again if we need for testing purposes


android/app/build.gradle.kts line 317 at r11 (raw file):

Previously, Rawa (David Göransson) wrote…

We should reuse the relayListPath here, right now we seem to construct it multiple times.

Yes fixed!


android/app/build.gradle.kts line 320 at r11 (raw file):

Previously, Rawa (David Göransson) wrote…

Does this serve any purpose? Shouldn't it exist if the thing above successfully ran?

Yeah I'll remove it.


android/app/build.gradle.kts line 327 at r11 (raw file):

Previously, Rawa (David Göransson) wrote…

We probably should add a comment and DROID-X issue link to something in our backlog to address this at some point, or what do you think?

Yes will create an issue.


android/app/build.gradle.kts line 332 at r11 (raw file):

Previously, Rawa (David Göransson) wrote…

Does it make sense to ignore case? Or would it better to safeg

Was a copy paste from the releaes build check, will remove the ignore case.


wireguard-go-rs/build.rs line 236 at r2 (raw file):

Previously, MarkusPettersson98 (Markus Pettersson) wrote…

⛏️ It should say -p, not P. p and P are different flags in cp

Also, it would be nice if the comment would mention that we use cp -p because we want to preserve ownership & timestamp to not re-trigger cargo accidentally :)

Updated text!


wireguard-go-rs/build.rs line 174 at r11 (raw file):

Previously, MarkusPettersson98 (Markus Pettersson) wrote…

I agree. Also, I think this deserves to be prefixed with "FIXME" to stand out more / increase grep-ability.

Created issue and added FIXME.


android/build-apk.sh line 61 at r2 (raw file):

Previously, MarkusPettersson98 (Markus Pettersson) wrote…

dito

Removed! It will now always clean cargo.


android/build-apk.sh line 64 at r11 (raw file):

Previously, Rawa (David Göransson) wrote…

We should already be in the correct folder I believe so we should be able to remove this pushd & popd below,

Removed!

Copy link
Contributor Author

@Pururun Pururun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 13 of 18 files reviewed, 10 unresolved discussions (waiting on @MarkusPettersson98 and @Rawa)


android/build.gradle.kts line 74 at r11 (raw file):

Previously, Rawa (David Göransson) wrote…

Should this also be extracted to some toml file??

Probably, could not really find a good way to do this?


android/app/build.gradle.kts line 315 at r11 (raw file):

Previously, Rawa (David Göransson) wrote…

Should we reference parent of relayListPath maybe?

Done

Copy link
Contributor

@MarkusPettersson98 MarkusPettersson98 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 5 files at r13.
Reviewable status: 14 of 18 files reviewed, 10 unresolved discussions (waiting on @Rawa)


android/build-apk.sh line 61 at r2 (raw file):

Previously, Pururun (Jonatan Rhodin) wrote…

Removed! It will now always clean cargo.

🙌

Copy link
Contributor

@Rawa Rawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 3 of 5 files at r13.
Reviewable status: 17 of 18 files reviewed, 2 unresolved discussions (waiting on @Pururun)


android/app/build.gradle.kts line 315 at r11 (raw file):

Previously, Pururun (Jonatan Rhodin) wrote…

Done

Seems like we set it toextraAssetsDirectory not relayListPath.parent(). This has the implicit dependency of relayListPath being a direct child of extraAssetsDirectory.


android/app/build.gradle.kts line 322 at r13 (raw file):

tasks.register<Exec>("cargoClean") {
    workingDir = File(repoRootPath)
    commandLine("cargo", "clean")

🌟

@albin-mullvad albin-mullvad requested a review from Rawa December 19, 2024 11:55
Copy link
Collaborator

@albin-mullvad albin-mullvad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 17 of 18 files reviewed, 3 unresolved discussions (waiting on @Pururun and @Rawa)


a discussion (no related file):
Can we rename android/build-apk.sh to android/build.sh?

@Rawa Rawa force-pushed the implement-gradle-rust-plugin-droid-1377 branch 3 times, most recently from 862cfb9 to e55f598 Compare December 19, 2024 13:35
Copy link
Collaborator

@albin-mullvad albin-mullvad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 3 of 16 files at r1, 2 of 4 files at r7, 1 of 5 files at r8, 3 of 7 files at r14, 3 of 5 files at r15.
Reviewable status: 14 of 19 files reviewed, 3 unresolved discussions (waiting on @Pururun and @Rawa)

@Rawa Rawa force-pushed the implement-gradle-rust-plugin-droid-1377 branch from e55f598 to d2dcf05 Compare December 19, 2024 13:44
Rawa
Rawa previously approved these changes Dec 19, 2024
Copy link
Contributor

@Rawa Rawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 2 of 5 files at r13, 3 of 7 files at r14, 5 of 5 files at r15, 1 of 1 files at r16, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @Pururun)

@Rawa Rawa force-pushed the implement-gradle-rust-plugin-droid-1377 branch from d2dcf05 to 99f37a3 Compare December 19, 2024 14:31
@Rawa Rawa force-pushed the implement-gradle-rust-plugin-droid-1377 branch from 99f37a3 to 4e04ac2 Compare December 19, 2024 15:24
Rawa
Rawa previously approved these changes Dec 20, 2024
Copy link
Contributor

@Rawa Rawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 1 files at r20, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @Pururun)

Copy link
Collaborator

@albin-mullvad albin-mullvad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 3 files at r10, 2 of 5 files at r13, 1 of 7 files at r14, 1 of 5 files at r15, 1 of 1 files at r16, 1 of 1 files at r20, all commit messages.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @Pururun)


wireguard-go-rs/build.rs line 235 at r20 (raw file):

    std::fs::create_dir_all(parent_of_output)?;

    let mut copy_command = Command::new("cp");

Is there any way we could avoid the copy? Asking since a copy is usually much more experience.


wireguard-go-rs/build.rs line 237 at r20 (raw file):

    let mut copy_command = Command::new("cp");
    // -p command is required to preserve ownership and timestamp of the file to prevent a
    // rebuild of this module everytime.

nit: every time

Code quote:

everytime

.github/workflows/android-app.yml line 132 at r19 (raw file):

      - name: Generate
        if: steps.cache-relay-list.outputs.cache-hit != 'true'
        uses: burrunan/gradle-cache-action@v1

I suggest we skip using the cache action here unless we see a lot of value in keeping it. The relay list will be cached on a daily basis (see cache-relay-list) so this should only run once a day unless the caching mechnaism is broken.


.github/workflows/android-app.yml line 158 at r19 (raw file):

      image: "${{ needs.prepare.outputs.container_image }}"
    strategy:
      matrix:

Can this matrix be simplified a bit or are all values needed?

Copy link
Collaborator

@albin-mullvad albin-mullvad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 5 files at r15.
Reviewable status: all files reviewed, 6 unresolved discussions (waiting on @Pururun)


android/app/build.gradle.kts line 270 at r20 (raw file):

    // https://github.com/mozilla/rust-android-gradle/tree/master?tab=readme-ov-file#targets
    targets =
        gradleLocalProperties(rootProject.projectDir, providers)

I believe we want to avoid local.properties: https://linear.app/mullvad/issue/DROID-1430/rework-localproperties-and-e2eproperties

I guess it's fine to not address now if too much work


android/app/build.gradle.kts line 300 at r20 (raw file):

    // Set this if you get a cargo not found error
    // environment =

Can we elaborate a bit here? What would we set it to and are there any relevant internal or external issues we can link to?

Code quote:

    // Set this if you get a cargo not found error
    // environment =

Copy link
Contributor Author

@Pururun Pururun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @albin-mullvad)


.github/workflows/android-app.yml line 158 at r19 (raw file):

Previously, albin-mullvad wrote…

Can this matrix be simplified a bit or are all values needed?

Removed arch as it is no longer needed.


android/app/build.gradle.kts line 270 at r20 (raw file):

Previously, albin-mullvad wrote…

I believe we want to avoid local.properties: https://linear.app/mullvad/issue/DROID-1430/rework-localproperties-and-e2eproperties

I guess it's fine to not address now if too much work

We decided to keep using local.properties as the plugin is still using it and it would make more sense to have them at the same place. We should probably have some kind issue for moving them in the future though.


android/app/build.gradle.kts line 300 at r20 (raw file):

Previously, albin-mullvad wrote…

Can we elaborate a bit here? What would we set it to and are there any relevant internal or external issues we can link to?

There is a potential issue with Android Studio and environment variables, but that should not be documented here. Removed.


wireguard-go-rs/build.rs line 237 at r20 (raw file):

Previously, albin-mullvad wrote…

nit: every time

Done

@Pururun Pururun force-pushed the implement-gradle-rust-plugin-droid-1377 branch from 32aeeaa to ab3d61f Compare December 20, 2024 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Android Issues related to Android
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants