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

refactor: build gradle #161

Merged
merged 5 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Loading