From 2e5e5ff7f02a443522cd363b4644588b68ba7cb0 Mon Sep 17 00:00:00 2001 From: Benjamin AIMONE Date: Tue, 12 Apr 2022 17:44:29 +0200 Subject: [PATCH] Fix unsatisfied link when package name has underscores --- CHANGELOG.md | 6 ++++++ README.md | 2 +- build.gradle.kts | 6 +++--- gradle/wrapper/gradle-wrapper.properties | 2 +- settings.gradle.kts | 2 +- .../com/klaxit/hiddensecrets/CodeGenerator.kt | 2 +- .../hiddensecrets/HiddenSecretsPlugin.kt | 2 +- .../kotlin/com/klaxit/hiddensecrets/Utils.kt | 19 ++++++++----------- src/test/kotlin/UtilsTest.kt | 10 ++++++++-- 9 files changed, 30 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7b9cda..eb4362d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.1.5 +### Fixes +* Fix `UnsatisfiedLinkError` when package name has underscores. Reported in https://github.com/klaxit/hidden-secrets-gradle-plugin/issues/52 +* Add unit tests for package name conversion +### Improvements +* Update libs and gradle # 0.1.4 ### Improvements * Update libs and gradle diff --git a/README.md b/README.md index e2f6ce5..6fe4486 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ The parameter `keyName` is optional, by default the key name is randomly generat The parameter `package` is optional, by default the `applicationId` of your project will be used. # 3 - Get your secret key in your app -Enable C++ files compilation by adding this lines in the app level `build.gradle` : +Enable C++ files compilation by adding this lines in the Module level `build.gradle` : ```gradle android { diff --git a/build.gradle.kts b/build.gradle.kts index e90be13..3015c96 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,5 @@ plugins { - id("com.gradle.plugin-publish") version "0.18.0" + id("com.gradle.plugin-publish") version "0.21.0" id("io.gitlab.arturbosch.detekt") version "1.19.0" `kotlin-dsl` `maven-publish` @@ -14,8 +14,8 @@ repositories { dependencies { implementation("com.android.tools.build:gradle:4.2.2") - testImplementation("io.kotest:kotest-runner-junit5-jvm:4.6.4") - testImplementation("io.kotest:kotest-assertions-core-jvm:4.6.4") + testImplementation("io.kotest:kotest-runner-junit5-jvm:5.2.2") + testImplementation("io.kotest:kotest-assertions-core-jvm:5.2.2") testImplementation("junit:junit:4.13.2") } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index d2880ba..2e6e589 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/settings.gradle.kts b/settings.gradle.kts index 0108cbb..f8e245a 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -2,5 +2,5 @@ rootProject.name = "HiddenSecretsPlugin" gradle.allprojects { group = "com.klaxit.hiddensecrets" - version = "0.1.4" + version = "0.1.5" } diff --git a/src/main/kotlin/com/klaxit/hiddensecrets/CodeGenerator.kt b/src/main/kotlin/com/klaxit/hiddensecrets/CodeGenerator.kt index 7e741ba..1ab7ff8 100644 --- a/src/main/kotlin/com/klaxit/hiddensecrets/CodeGenerator.kt +++ b/src/main/kotlin/com/klaxit/hiddensecrets/CodeGenerator.kt @@ -12,7 +12,7 @@ object CodeGenerator { return "\nextern \"C\"\n" + "JNIEXPORT jstring JNICALL\n" + - "Java_" + Utils.getSnakeCasePackageName(packageName) + "_Secrets_get$keyName(\n" + + "Java_" + Utils.getCppPackageName(packageName) + "_Secrets_get$keyName(\n" + " JNIEnv* pEnv,\n" + " jobject pThis,\n" + " jstring packageName) {\n" + diff --git a/src/main/kotlin/com/klaxit/hiddensecrets/HiddenSecretsPlugin.kt b/src/main/kotlin/com/klaxit/hiddensecrets/HiddenSecretsPlugin.kt index fc5d7c8..3a099f5 100644 --- a/src/main/kotlin/com/klaxit/hiddensecrets/HiddenSecretsPlugin.kt +++ b/src/main/kotlin/com/klaxit/hiddensecrets/HiddenSecretsPlugin.kt @@ -284,7 +284,7 @@ open class HiddenSecretsPlugin : Plugin { if (text.contains(KEY_PLACEHOLDER)) { //Edit placeholder key //Replace package name - text = text.replace(PACKAGE_PLACEHOLDER, Utils.getSnakeCasePackageName(kotlinPackage)) + text = text.replace(PACKAGE_PLACEHOLDER, Utils.getCppPackageName(kotlinPackage)) //Replace key name text = text.replace("YOUR_KEY_NAME_GOES_HERE", keyName) //Replace demo key diff --git a/src/main/kotlin/com/klaxit/hiddensecrets/Utils.kt b/src/main/kotlin/com/klaxit/hiddensecrets/Utils.kt index 31d3d47..36ab3ab 100644 --- a/src/main/kotlin/com/klaxit/hiddensecrets/Utils.kt +++ b/src/main/kotlin/com/klaxit/hiddensecrets/Utils.kt @@ -11,18 +11,15 @@ object Utils { /** * Transform package name com.klaxit.hidden to com_klaxit_hidden to ingrate in C++ code + * Java package name needs to escape some characters to call the NDK + * From https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/design.html#resolving_native_method_names */ - fun getSnakeCasePackageName(packageName: String): String { - val packageComponents = packageName.split(".") - var packageStr = "" - val iterator: Iterator = packageComponents.iterator() - while (iterator.hasNext()) { - packageStr += iterator.next() - if (iterator.hasNext()) { - packageStr += "_" - } - } - return packageStr + fun getCppPackageName(packageName: String): String { + return packageName + .replace("_", "_1") + .replace(";", "_2") + .replace("[", "_3") + .replace(".", "_") } /** diff --git a/src/test/kotlin/UtilsTest.kt b/src/test/kotlin/UtilsTest.kt index 9c97dd6..8f4b2e1 100644 --- a/src/test/kotlin/UtilsTest.kt +++ b/src/test/kotlin/UtilsTest.kt @@ -10,9 +10,15 @@ class UtilsTest : WordSpec({ val packageName = "com.klaxit.test" - "Using getUnderScoredPackageName()" should { + "Using getCppPackageName()" should { "transform package separator" { - Utils.getSnakeCasePackageName(packageName) shouldBe "com_klaxit_test" + Utils.getCppPackageName(packageName) shouldBe "com_klaxit_test" + } + "transform package with underscore" { + Utils.getCppPackageName("com.klaxit.test_with_underscore") shouldBe "com_klaxit_test_1with_1underscore" + } + "transform package with escaping characters" { + Utils.getCppPackageName("com[test.klaxit;test.test_with_underscore") shouldBe "com_3test_klaxit_2test_test_1with_1underscore" } }