From fcdc0a8097f2fb18f6e271662d7bc64cca9f64eb Mon Sep 17 00:00:00 2001 From: micbakos-rdx <125959264+micbakos-rdx@users.noreply.github.com> Date: Mon, 30 Sep 2024 10:50:04 +0300 Subject: [PATCH] Android Local Development (#226) * Provide ability to publish package to maven local for local development * Update readme * Add suffix explanation --- .github/workflows/release-android.yml | 2 +- .github/workflows/release-desktop.yml | 5 +- .github/workflows/test.yml | 3 - README.md | 34 +++++++ .../com/radixdlt/cargo/toml/SargonVersion.kt | 6 +- jvm/sargon-android/build.gradle.kts | 98 ++++++++++++------- 6 files changed, 104 insertions(+), 44 deletions(-) diff --git a/.github/workflows/release-android.yml b/.github/workflows/release-android.yml index 794564498..49f075a01 100644 --- a/.github/workflows/release-android.yml +++ b/.github/workflows/release-android.yml @@ -62,7 +62,7 @@ jobs: - name: Build and publish Android uses: RDXWorks-actions/gradle-build-action@main with: - arguments: sargon-android:publishAndroidPublicationToGitHubPackagesRepository + arguments: sargon-android:publishAndroidReleasePublicationToGitHubPackagesRepository build-root-directory: jvm env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/release-desktop.yml b/.github/workflows/release-desktop.yml index d52c25082..4ad0f69d8 100644 --- a/.github/workflows/release-desktop.yml +++ b/.github/workflows/release-desktop.yml @@ -46,9 +46,6 @@ jobs: target: ${{ matrix.build-target.toolchain }} default: 'true' - - name: Rustc version - run: cargo --version --verbose - - name: Set up JDK 17 uses: RDXWorks-actions/setup-java@v3 with: @@ -92,7 +89,7 @@ jobs: - name: Publish desktop binaries uses: RDXWorks-actions/gradle-build-action@main with: - arguments: sargon-android:publishDesktopPublicationToGitHubPackagesRepository + arguments: sargon-android:publishDesktopReleasePublicationToGitHubPackagesRepository build-root-directory: jvm env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fe4ff0f1d..ffeb701bf 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -165,9 +165,6 @@ jobs: target: x86_64-unknown-linux-gnu default: 'true' - - name: Rustc version - run: cargo --version --verbose - - name: Set up JDK 17 uses: RDXWorks-actions/setup-java@v3 with: diff --git a/README.md b/README.md index 7e38e1d89..bb411ab2e 100644 --- a/README.md +++ b/README.md @@ -368,6 +368,40 @@ See [`.github/workflows/release.yml`](.github/workflows/release.yml) ## Android +### Locally +In order to build sargon for local development we will leverage the local maven repository. Instead of publishing the package in a maven server, we can publish it locally. + +In order to publish both android and desktop binaries with a simple command run +```sh +cd jvm/ +./gradlew sargon-android:buildForLocalDev // This builds both sargon-android and sargon-desktop-bins +``` +This will produce the following message when successfully finished +```txt +> Task :sargon-android:buildForLocalDev +✅ Library is published in maven local with version: +1.1.19-c74d9cbf-SNAPSHOT +``` +Note that such local maven builds are in debug mode and have a `-SNAPSHOT` suffix. + +Copy the version name to your project but make sure that `mavenLocal()` is included in your project's `settings.gradle` +```gradle +dependencyResolutionManagement { + ... + repositories { + mavenLocal() + ... + } +} +``` +> [!TIP] +> The libraries that are published in local maven will reside in: +> ``` +> $HOME/.m2/repository/com/radixdlt/sargon +> ``` + +### CD + Two modules are published in [Github's maven](https://github.com/radixdlt/sargon/packages/). - `sargon-android` diff --git a/jvm/buildSrc/src/main/java/com/radixdlt/cargo/toml/SargonVersion.kt b/jvm/buildSrc/src/main/java/com/radixdlt/cargo/toml/SargonVersion.kt index 25bc2ed00..17b27639a 100644 --- a/jvm/buildSrc/src/main/java/com/radixdlt/cargo/toml/SargonVersion.kt +++ b/jvm/buildSrc/src/main/java/com/radixdlt/cargo/toml/SargonVersion.kt @@ -22,12 +22,14 @@ private fun Project.parseGitHash(): String { return String(out.toByteArray(), Charsets.UTF_8).trim() } -fun Project.sargonVersion(): String { +fun Project.sargonVersion(isDebug: Boolean): String { val customBuildName = System.getenv("CUSTOM_BUILD_NAME")?.takeIf { it.isNotBlank() }?.replace("\\s+".toRegex(), "-")?.let { "-${it}" }.orEmpty() - return "${parseTomlVersion()}${customBuildName}-${parseGitHash()}" + val snapshot = if (isDebug) "-SNAPSHOT" else "" + + return "${parseTomlVersion()}${customBuildName}-${parseGitHash()}$snapshot" } \ No newline at end of file diff --git a/jvm/sargon-android/build.gradle.kts b/jvm/sargon-android/build.gradle.kts index 7105ee300..898b7243e 100644 --- a/jvm/sargon-android/build.gradle.kts +++ b/jvm/sargon-android/build.gradle.kts @@ -1,6 +1,8 @@ import com.radixdlt.cargo.desktop.DesktopTargetTriple import com.radixdlt.cargo.desktop.currentTargetTriple import com.radixdlt.cargo.toml.sargonVersion +import org.gradle.configurationcache.extensions.capitalized +import org.gradle.internal.logging.text.StyledTextOutput import org.jetbrains.kotlin.gradle.tasks.KotlinCompile import java.nio.file.Files @@ -70,14 +72,24 @@ android { ) } - // This task is used when publishing `sargon-desktop-bins`. - // Before generating a Jar we need all native libs to have been built for all desktop - // architectures. - // The building is handled by github. After that the `copyExternalArtifacts` needs to copy - // all the built libraries into the resources directory. - tasks.register("desktopJar") { - from("${buildDir}/generated/src/resources") - dependsOn("copyExternalArtifacts") + buildTypes.forEach { + val buildTypeVariant = it.name.capitalized() + tasks.register("desktopJar${buildTypeVariant}") { + from("${buildDir}/generated/src/resources") + + if (it.isDebuggable) { + // For debug we only need to build for the current architecture. Used by maven publication in + // debug mode. + dependsOn("buildCargoDesktopDebug") + } else { + // This task is used when publishing `sargon-desktop-bins`. + // Before generating a Jar we need all native libs to have been built for all desktop + // architectures. + // The building is handled by github. After that the `copyExternalArtifacts` needs to copy + // all the built libraries into the resources directory. + dependsOn("copyExternalArtifacts") + } + } } } @@ -169,37 +181,41 @@ dependencies { publishing { publications { - // Publishing the android library we just need to build the library from the release component - register("android") { - groupId = "com.radixdlt.sargon" - artifactId = "sargon-android" - version = project.sargonVersion() - - afterEvaluate { - from(components["release"]) - } - } + android.buildTypes.forEach { + val buildTypeVariant = it.name.capitalized() - // Publishing the desktop bins we need to run the `desktopJar` task. For more info check - // the comments of that task. - register("desktop") { - groupId = "com.radixdlt.sargon" - artifactId = "sargon-desktop-bins" - version = project.sargonVersion() + // Publishing the android library we just need to build the library from the release component + register("android$buildTypeVariant") { + groupId = "com.radixdlt.sargon" + artifactId = "sargon-android" + version = project.sargonVersion(it.isDebuggable) - afterEvaluate { - artifact(tasks.getByName("desktopJar")) + afterEvaluate { + from(components[it.name]) + } } - pom { - withXml { - val dependencies = asNode().appendNode("dependencies") + // Publishing the desktop bins we need to run the `desktopJar` task. For more info check + // the comments of that task. + register("desktop$buildTypeVariant") { + groupId = "com.radixdlt.sargon" + artifactId = "sargon-desktop-bins" + version = project.sargonVersion(it.isDebuggable) - val jni = dependencies.appendNode("dependency") - jni.appendNode("groupId", "net.java.dev.jna") - jni.appendNode("artifactId", "jna") - jni.appendNode("version", libs.versions.jna.get()) - jni.appendNode("scope", "runtime") + afterEvaluate { + artifact(tasks.getByName("desktopJar$buildTypeVariant")) + } + + pom { + withXml { + val dependencies = asNode().appendNode("dependencies") + + val jni = dependencies.appendNode("dependency") + jni.appendNode("groupId", "net.java.dev.jna") + jni.appendNode("artifactId", "jna") + jni.appendNode("version", libs.versions.jna.get()) + jni.appendNode("scope", "runtime") + } } } } @@ -294,6 +310,20 @@ afterEvaluate { tasks.getByName("testDebugUnitTest").dependsOn("buildCargoDesktopDebug") tasks.getByName("testReleaseUnitTest").dependsOn("buildCargoDesktopRelease") + + tasks.register("buildForLocalDev") { + group = "publishing" + + doLast { + println("✅ Library is published in maven local with version:") + println(project.sargonVersion(true)) + } + + dependsOn( + "publishAndroidDebugPublicationToMavenLocal", + "publishDesktopDebugPublicationToMavenLocal" + ) + } } // Task that copies externally built artifacts into resources directory