From b9090b5ef3d238cdbd050ed518b2ec90952a616b Mon Sep 17 00:00:00 2001 From: Jay Ohms Date: Wed, 28 Feb 2024 16:28:12 -0500 Subject: [PATCH 1/2] Convert build files to Kotlin DSL --- README.md | 2 +- build.gradle => build.gradle.kts | 11 +- docs/INSTALLATION.md | 19 +-- strada/build.gradle | 192 ----------------------------- strada/build.gradle.kts | 190 ++++++++++++++++++++++++++++ strada/proguard-consumer-rules.pro | 15 +++ strada/proguard-rules.pro | 2 +- 7 files changed, 223 insertions(+), 208 deletions(-) rename build.gradle => build.gradle.kts (51%) delete mode 100644 strada/build.gradle create mode 100644 strada/build.gradle.kts create mode 100644 strada/proguard-consumer-rules.pro diff --git a/README.md b/README.md index 00545a1..86db871 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ ## Requirements -1. Android SDK 26+ is required as the `minSdkVersion` in your build.gradle. +1. Android SDK 26+ is required as the `minSdk` in your `build.gradle.kts` file. 1. This library is written entirely in [Kotlin](https://kotlinlang.org/), and your app should use Kotlin as well. Compatibility with Java is not provided or supported. 1. This library supports [Turbo Native](https://turbo.hotwired.dev/handbook/native) hybrid apps. 1. Your web app must be running [strada-web](https://github.com/hotwired/strada-web). The `window.Strada` object is automatically exposed on the loaded WebView page, which enables `strada-android` to work. diff --git a/build.gradle b/build.gradle.kts similarity index 51% rename from build.gradle rename to build.gradle.kts index 8f318ac..4c9622d 100644 --- a/build.gradle +++ b/build.gradle.kts @@ -1,16 +1,15 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlinVersion = '1.9.10' - repositories { google() mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:8.2.2' - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" + classpath("com.android.tools.build:gradle:8.2.2") + classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.10") + classpath("org.jetbrains.kotlin:kotlin-serialization:1.9.10") } } @@ -21,6 +20,6 @@ allprojects { } } -task clean(type: Delete) { - delete rootProject.buildDir +tasks.register("clean").configure { + delete(rootProject.buildDir) } diff --git a/docs/INSTALLATION.md b/docs/INSTALLATION.md index 8a3604e..946d2cc 100644 --- a/docs/INSTALLATION.md +++ b/docs/INSTALLATION.md @@ -1,11 +1,11 @@ # Installation ## Gradle -Add the dependency from Maven Central to your app module's (not top-level) `build.gradle` file: +Add the dependency from Maven Central to your app module's (not top-level) `build.gradle.kts` file: -```groovy +```kotlin dependencies { - implementation 'dev.hotwire:strada:' + implementation("dev.hotwire:strada:") } ``` @@ -17,10 +17,13 @@ See the [latest version](https://search.maven.org/artifact/dev.hotwire/strada) a ## Required `minSdkVersion` Android SDK 26 (or greater) is required as the `minSdkVersion` in your app module's `build.gradle` file: -```groovy +```kotlin +compileSdk = 34 + defaultConfig { - minSdkVersion 26 - ... + minSdk = 26 + targetSdk = 34 + // ... } ``` @@ -40,7 +43,7 @@ export GITHUB_ACCESS_TOKEN='' ## Gradle Add the GitHub Packages maven repository and the dependency to your app module's `build.gradle` file: -```groovy +```kotlin repositories { maven { name = "GitHubPackages" @@ -54,6 +57,6 @@ repositories { } dependencies { - implementation 'dev.hotwire:strada:' + implementation("dev.hotwire:strada:") } ``` diff --git a/strada/build.gradle b/strada/build.gradle deleted file mode 100644 index 04bd502..0000000 --- a/strada/build.gradle +++ /dev/null @@ -1,192 +0,0 @@ -apply plugin: 'com.android.library' -apply plugin: 'kotlin-android' -apply plugin: 'kotlinx-serialization' -apply plugin: 'maven-publish' -apply plugin: 'signing' - -ext { - libVersionName = version - libraryName = 'Strada Android' - libraryDescription = 'Create fully native Android controls, driven by your web app' - - publishedGroupId = 'dev.hotwire' - publishedArtifactId = 'strada' - - siteUrl = 'https://github.com/hotwired/strada-android' - gitUrl = 'https://github.com/hotwired/strada-android.git' - - licenseType = 'MIT License' - licenseUrl = 'https://github.com/hotwired/strada-android/blob/main/LICENSE' - - developerId = 'basecamp' - developerEmail = 'androidteam@basecamp.com' - - isSonatypeRelease = project.hasProperty('sonatype') -} - -buildscript { - repositories { - google() - mavenCentral() - } - - dependencies { - classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlinVersion" - } -} - -repositories { - google() - mavenCentral() -} - -android { - compileSdk = 34 - - defaultConfig { - minSdkVersion 26 - targetSdkVersion 34 - testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" - } - - compileOptions { - sourceCompatibility JavaVersion.VERSION_17 - targetCompatibility JavaVersion.VERSION_17 - } - - kotlinOptions { - jvmTarget = "17" - } - - buildTypes { - release { - minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' - } - } - - sourceSets { - main.java.srcDirs += 'src/main/kotlin' - test.java.srcDirs += 'src/test/kotlin' - } - - testOptions { - unitTests.includeAndroidResources = true - unitTests.returnDefaultValues = true - - kotlinOptions { - freeCompilerArgs += [ - '-Xopt-in=kotlinx.coroutines.ExperimentalCoroutinesApi' - ] - } - } - - namespace 'dev.hotwire.strada' -} - -dependencies { - implementation fileTree(dir: 'libs', include: ['*.jar']) - - implementation 'androidx.core:core-ktx:1.12.0' - implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0' - implementation 'androidx.lifecycle:lifecycle-common:2.7.0' - - testImplementation 'junit:junit:4.13.2' - testImplementation 'androidx.test:core:1.5.0' - testImplementation 'org.assertj:assertj-core:3.24.2' - testImplementation 'androidx.lifecycle:lifecycle-runtime-testing:2.7.0' - testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3" - testImplementation 'org.robolectric:robolectric:4.11.1' - testImplementation 'org.mockito:mockito-core:5.2.0' - testImplementation 'com.nhaarman:mockito-kotlin:1.6.0' -} - -// Use the sources jar when publishing -task androidSourcesJar(type: Jar) { - archiveClassifier.set("sources") - from android.sourceSets.main.java.srcDirs -} - -// Only sign Sonatype release artifacts -tasks.withType(Sign) { - onlyIf { isSonatypeRelease } -} - -// Sign Sonatype published release artifacts -if (isSonatypeRelease) { - signing { - def keyId = System.getenv('GPG_KEY_ID') - def secretKey = System.getenv("GPG_SECRET_KEY") - def password = System.getenv("GPG_PASSWORD") - - useInMemoryPgpKeys(keyId, secretKey, password) - - required { gradle.taskGraph.hasTask("publish") } - sign publishing.publications - } -} - -// Publish to GitHub Packages via ./gradlew -Pversion= clean build publish -// https://github.com/orgs/hotwired/packages?repo_name=strada-android -afterEvaluate { - publishing { - publications { - release(MavenPublication) { - pom { - name = libraryName - description = libraryDescription - url = siteUrl - licenses { - license { - name = licenseType - url = licenseUrl - } - } - developers { - developer { - id = developerId - name = developerId - email = developerEmail - } - } - scm { - url = gitUrl - } - } - - // Applies the component for the release build variant - from components.release - - // Add sources as separate jar - artifact androidSourcesJar - - // Publication attributes - groupId = publishedGroupId - artifactId = publishedArtifactId - version = libVersionName - } - } - repositories { - if (isSonatypeRelease) { - maven { - url = uri('https://s01.oss.sonatype.org/content/repositories/releases/') - - credentials { - username = System.getenv('SONATYPE_USER') - password = System.getenv('SONATYPE_PASSWORD') - } - } - } else { - maven { - name = 'GitHubPackages' - url = uri('https://maven.pkg.github.com/hotwired/strada-android') - - credentials { - username = System.getenv('GITHUB_ACTOR') - password = System.getenv('GITHUB_TOKEN') - } - } - } - } - } -} diff --git a/strada/build.gradle.kts b/strada/build.gradle.kts new file mode 100644 index 0000000..5e3bb37 --- /dev/null +++ b/strada/build.gradle.kts @@ -0,0 +1,190 @@ +plugins { + id("com.android.library") + id("kotlin-android") + id("kotlinx-serialization") + id("maven-publish") + id("signing") +} + +val libVersionName by extra(version as String) +val libraryName by extra("Strada Android") +val libraryDescription by extra("Create fully native Android controls, driven by your web app") + +val publishedGroupId by extra("dev.hotwire") +val publishedArtifactId by extra("strada") + +val siteUrl by extra("https://github.com/hotwired/strada-android") +val gitUrl by extra("https://github.com/hotwired/strada-android.git") + +val licenseType by extra("MIT License") +val licenseUrl by extra("https://github.com/hotwired/strada-android/blob/main/LICENSE") + +val developerId by extra("basecamp") +val developerEmail by extra("androidteam@basecamp.com") + +val isSonatypeRelease by extra(project.hasProperty("sonatype")) + +buildscript { + repositories { + google() + mavenCentral() + } + + dependencies { + classpath("org.jetbrains.kotlin:kotlin-serialization:1.9.10") + } +} + +repositories { + google() + mavenCentral() +} + +android { + compileSdk = 34 + testOptions.unitTests.isIncludeAndroidResources = true + testOptions.unitTests.isReturnDefaultValues = true + + defaultConfig { + minSdk = 26 + targetSdk = 34 + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + + // Define ProGuard rules for this android library project. These rules will be applied when + // a consumer of this library sets 'minifyEnabled true'. + consumerProguardFiles("proguard-consumer-rules.pro") + } + + kotlinOptions { + jvmTarget = JavaVersion.VERSION_17.toString() + } + + compileOptions { + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 + } + + buildTypes { + getByName("release") { + isMinifyEnabled = false + setProguardFiles(listOf(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")) + } + } + + sourceSets { + named("main") { java { srcDirs("src/main/kotlin") } } + named("test") { java { srcDirs("src/test/kotlin") } } + } + + namespace = "dev.hotwire.strada" + + publishing { + singleVariant("release") { + withSourcesJar() + } + } +} + +dependencies { + implementation(fileTree(mapOf("include" to listOf("*.jar"), "dir" to "libs"))) + + implementation("androidx.core:core-ktx:1.12.0") + implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0") + implementation("androidx.lifecycle:lifecycle-common:2.7.0") + + testImplementation("junit:junit:4.13.2") + testImplementation("androidx.test:core:1.5.0") + testImplementation("org.assertj:assertj-core:3.24.2") + testImplementation("androidx.lifecycle:lifecycle-runtime-testing:2.7.0") + testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3") + testImplementation("org.robolectric:robolectric:4.11.1") + testImplementation("org.mockito:mockito-core:5.2.0") + testImplementation("com.nhaarman:mockito-kotlin:1.6.0") +} + +tasks { + // Only sign Sonatype release artifacts + withType().configureEach { + onlyIf { isSonatypeRelease } + } +} + +// Sign Sonatype published release artifacts +if (isSonatypeRelease) { + signing { + val keyId = System.getenv("GPG_KEY_ID") + val secretKey = System.getenv("GPG_SECRET_KEY") + val password = System.getenv("GPG_PASSWORD") + + useInMemoryPgpKeys(keyId, secretKey, password) + + setRequired({ gradle.taskGraph.hasTask("publish") }) + sign(publishing.publications) + } +} + +// Publish to GitHub Packages via: +// ./gradlew -Pversion= clean build publish +// https://github.com/orgs/hotwired/packages?repo_name=strada-android +// Publish to Maven Central via: +// ./gradlew -Psonatype -Pversion= clean build publish +// https://search.maven.org/artifact/dev.hotwire/strada +publishing { + publications { + register("release") { + groupId = publishedGroupId + artifactId = publishedArtifactId + version = libVersionName + + pom { + name.set(libraryName) + description.set(libraryDescription) + url.set(siteUrl) + + licenses { + license { + name.set(licenseType) + url.set(licenseUrl) + } + } + developers { + developer { + id.set(developerId) + name.set(developerId) + email.set(developerEmail) + } + } + scm { + url.set(gitUrl) + } + } + + // Applies the component for the release build variant + afterEvaluate { + from(components["release"]) + } + } + } + repositories { + if (isSonatypeRelease) { + maven { + url = uri("https://s01.oss.sonatype.org/content/repositories/releases/") + + credentials { + username = System.getenv("SONATYPE_USER") + password = System.getenv("SONATYPE_PASSWORD") + } + } + } else { + maven { + name = "GitHubPackages" + url = uri("https://maven.pkg.github.com/hotwired/strada-android") + + credentials { + username = System.getenv("GITHUB_ACTOR") + password = System.getenv("GITHUB_TOKEN") + } + } + } + } +} diff --git a/strada/proguard-consumer-rules.pro b/strada/proguard-consumer-rules.pro new file mode 100644 index 0000000..c612716 --- /dev/null +++ b/strada/proguard-consumer-rules.pro @@ -0,0 +1,15 @@ +# The Android Gradle plugin allows to define ProGuard rules which get embedded in the AAR. +# These ProGuard rules are automatically applied when a consumer app sets minifyEnabled to true. +# The custom rule file must be defined using the 'consumerProguardFiles' property in your +# build.gradle file. + +# Maintain JavascriptInterfaces attached to webviews +-keepclassmembers class * { + @android.webkit.JavascriptInterface ; +} + +-keepclassmembers class dev.hotwire.strada.Bridge { + public *; +} + +-keep class dev.hotwire.strada.** { *; } diff --git a/strada/proguard-rules.pro b/strada/proguard-rules.pro index 1230e9f..13e0b05 100644 --- a/strada/proguard-rules.pro +++ b/strada/proguard-rules.pro @@ -1,6 +1,6 @@ # Add project specific ProGuard rules here. # You can control the set of applied configuration files using the -# proguardFiles setting in build.gradle. +# proguardFiles setting in build.gradle.kts. # # For more details, see # http://developer.android.com/guide/developing/tools/proguard.html From c9f37dfba74685d1c14826cbff256a301b87597e Mon Sep 17 00:00:00 2001 From: Jay Ohms Date: Thu, 29 Feb 2024 10:55:37 -0500 Subject: [PATCH 2/2] Build cleanup --- strada/build.gradle.kts | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/strada/build.gradle.kts b/strada/build.gradle.kts index 5e3bb37..cc6c89c 100644 --- a/strada/build.gradle.kts +++ b/strada/build.gradle.kts @@ -24,17 +24,6 @@ val developerEmail by extra("androidteam@basecamp.com") val isSonatypeRelease by extra(project.hasProperty("sonatype")) -buildscript { - repositories { - google() - mavenCentral() - } - - dependencies { - classpath("org.jetbrains.kotlin:kotlin-serialization:1.9.10") - } -} - repositories { google() mavenCentral()