Skip to content

Commit

Permalink
Fix unsatisfied link when package name has underscores
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin AIMONE authored and Benjamin AIMONE committed Apr 12, 2022
1 parent 61cdb0a commit 2e5e5ff
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 21 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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`
Expand All @@ -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")
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ rootProject.name = "HiddenSecretsPlugin"

gradle.allprojects {
group = "com.klaxit.hiddensecrets"
version = "0.1.4"
version = "0.1.5"
}
2 changes: 1 addition & 1 deletion src/main/kotlin/com/klaxit/hiddensecrets/CodeGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ open class HiddenSecretsPlugin : Plugin<Project> {
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
Expand Down
19 changes: 8 additions & 11 deletions src/main/kotlin/com/klaxit/hiddensecrets/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = 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(".", "_")
}

/**
Expand Down
10 changes: 8 additions & 2 deletions src/test/kotlin/UtilsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

Expand Down

0 comments on commit 2e5e5ff

Please sign in to comment.