Skip to content

Commit

Permalink
Android Local Development (#226)
Browse files Browse the repository at this point in the history
* Provide ability to publish package to maven local for local development

* Update readme

* Add suffix explanation
  • Loading branch information
micbakos-rdx authored Sep 30, 2024
1 parent 9ac6768 commit fcdc0a8
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release-android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
5 changes: 1 addition & 4 deletions .github/workflows/release-desktop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 }}
3 changes: 0 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
98 changes: 64 additions & 34 deletions jvm/sargon-android/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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<Jar>("desktopJar") {
from("${buildDir}/generated/src/resources")
dependsOn("copyExternalArtifacts")
buildTypes.forEach {
val buildTypeVariant = it.name.capitalized()
tasks.register<Jar>("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")
}
}
}
}

Expand Down Expand Up @@ -169,37 +181,41 @@ dependencies {

publishing {
publications {
// Publishing the android library we just need to build the library from the release component
register<MavenPublication>("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<MavenPublication>("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<MavenPublication>("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<MavenPublication>("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")
}
}
}
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fcdc0a8

Please sign in to comment.