Skip to content

Commit

Permalink
refactor: build gradle (#161)
Browse files Browse the repository at this point in the history
* chore: split big sdk/build.gradle to several applied gradle sripts

* chore: upload all outputs and reports on benchmark failure

* ci: move timeout restoriction to step level and reduce it to 20

* debug: enable artifact upload for success bench

* fix: ignore HCaptchaWebViewHelperTest.benchmarkWebViewLoad benchmark
  • Loading branch information
CAMOBAP authored Jul 12, 2024
1 parent 124caaa commit 6cd1223
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 65 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ jobs:
# ubuntu-latest fails with JNI ERROR (app bug): weak global reference table overflow (max=51200)
# macos-latest i.e. macos-14 https://github.com/ReactiveCircus/android-emulator-runner/issues/324
runs-on: macos-13
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
Expand All @@ -146,19 +145,20 @@ jobs:
cache-read-only: false
- name: Run tests
uses: ./.github/actions/android-emulator-run
timeout-minutes: 20
with:
api-level: 29
fresh-avd: true
script: |
adb uninstall com.hcaptcha.sdk.bench.test || true
./gradlew benchmark:connectedReleaseAndroidTest
- if: failure()
- if: always()
uses: actions/upload-artifact@v4
with:
name: androidTest-benchmark-results
path: |
benchmark/build/outputs/androidTest-results
benchmark/build/reports/androidTests
benchmark/build/outputs/
benchmark/build/reports/
- name: Diff benchmark result
id: diff-benchmark
uses: ./.github/actions/android-benchmark-diff
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.CountDownLatch;

@Ignore("https://github.com/hCaptcha/hcaptcha-android-sdk/issues/101")
@RunWith(AndroidJUnit4.class)
public class HCaptchaWebViewHelperTest {
@Rule
Expand Down
31 changes: 31 additions & 0 deletions gradle/shared/html-java-gen.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
android.libraryVariants.all { variant ->
def packageName = android.namespace
def variantName = variant.name.capitalize()
def outputDir = file("${project.buildDir}/generated/source/hcaptcha/${variant.name}/${packageName.replaceAll('\\.', '/')}")
def generateTask = project.task("generate${variantName}JavaClassFromStaticHtml") {
group 'Generate'
description "Generate HTML java class"

doFirst {
def outputJavaClass = file("$outputDir/HCaptchaHtml.java")
def template = file("$projectDir/src/main/html/HCaptchaHtml.java.tml").text
def html = file("$projectDir/src/main/html/hcaptcha.html")
.readLines()
.stream()
.map({l -> "\"${l.replaceAll('"', '\\\\"')}\\n\""})
.collect(java.util.stream.Collectors.joining("\n${' ' * 16}+ "))

def engine = new groovy.text.SimpleTemplateEngine()
def src = engine.createTemplate(template).make([
"htmlContent": html,
"packageName": packageName
])

outputDir.mkdirs()
outputJavaClass.write(src.toString())
}
}

// preBuild.dependsOn generateTask
variant.registerJavaGeneratingTask(generateTask, outputDir)
}
28 changes: 28 additions & 0 deletions gradle/shared/size-check.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
android.libraryVariants.all { variant ->
def variantName = variant.name.capitalize()
project.task("report${variantName}AarSize") {
group 'Help'
description "Report ${variant.name} AAR size"
dependsOn variant.packageLibraryProvider

doFirst {
var aarPath = variant.packageLibraryProvider.get().archiveFile.get().getAsFile()
long aarSizeKb = aarPath.length() / 1024
println("File ${aarPath} is ${aarSizeKb}Kbyte")
}
}

project.tasks.findByName("check").dependsOn(project.task("check${variantName}AarSize") {
group 'Verification'
description "Checks ${variant.name} AAR size doesn't exceed ${project.ext}Kb"
dependsOn variant.packageLibraryProvider

doFirst {
var aarFile = variant.packageLibraryProvider.get().archiveFile.get().getAsFile()
long aarSizeKb = aarFile.length() / 1024
if (aarSizeKb > maxAarSizeKb) {
throw new GradleException("${aarPath} size exceeded! ${aarSizeKb}Kbyte > ${MAX_AAR_SIZE_KB}Kbyte")
}
}
})
}
68 changes: 7 additions & 61 deletions sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ plugins {
id "org.sonarqube" version "3.4.0.2513"
}

ext {
maxAarSizeKb = 200
}

android {
compileSdk 34
namespace 'com.hcaptcha.sdk'
Expand Down Expand Up @@ -111,64 +115,6 @@ project.afterEvaluate {
}
}

long MAX_AAR_SIZE_KB = 200

android.libraryVariants.all { variant ->
def variantName = variant.name.capitalize()
project.task("report${variantName}AarSize") {
group 'Help'
description "Report ${variant.name} AAR size"
dependsOn variant.packageLibraryProvider

doFirst {
var aarPath = variant.packageLibraryProvider.get().archiveFile.get().getAsFile()
long aarSizeKb = aarPath.length() / 1024
println("File ${aarPath} is ${aarSizeKb}Kbyte")
}
}

project.tasks.findByName("check").dependsOn(project.task("check${variantName}AarSize") {
group 'Verification'
description "Checks ${variant.name} AAR size doesn't exceed ${MAX_AAR_SIZE_KB}Kb"
dependsOn variant.packageLibraryProvider

doFirst {
var aarFile = variant.packageLibraryProvider.get().archiveFile.get().getAsFile()
long aarSizeKb = aarFile.length() / 1024
if (aarSizeKb > MAX_AAR_SIZE_KB) {
throw new GradleException("${aarPath} size exceeded! ${aarSizeKb}Kbyte > ${MAX_AAR_SIZE_KB}Kbyte")
}
}
})

def packageName = "com.hcaptcha.sdk"
def outputDir = file("${project.buildDir}/generated/source/hcaptcha/${variant.name}/${packageName.replaceAll('\\.', '/')}")
def generateTask = project.task("generate${variantName}JavaClassFromStaticHtml") {
group 'Generate'
description "Generate HTML java class"

doFirst {
def outputJavaClass = file("$outputDir/HCaptchaHtml.java")
def template = file("$projectDir/src/main/html/HCaptchaHtml.java.tml").text
def html = file("$projectDir/src/main/html/hcaptcha.html")
.readLines()
.stream()
.map({l -> "\"${l.replaceAll('"', '\\\\"')}\\n\""})
.collect(java.util.stream.Collectors.joining("\n${' ' * 16}+ "))

def engine = new groovy.text.SimpleTemplateEngine()
def src = engine.createTemplate(template).make([
"htmlContent": html,
"packageName": packageName
])

outputDir.mkdirs()
outputJavaClass.write(src.toString())
}
}

// preBuild.dependsOn generateTask
variant.registerJavaGeneratingTask(generateTask, outputDir)
}

apply from: "$rootProject.projectDir/gradle/shared/code-quality.gradle"
apply from: "$rootProject.projectDir/gradle/shared/code-quality.gradle"
apply from: "$rootProject.projectDir/gradle/shared/size-check.gradle"
apply from: "$rootProject.projectDir/gradle/shared/html-java-gen.gradle"

0 comments on commit 6cd1223

Please sign in to comment.