klaxit / hidden-secrets-gradle-plugin

🔒 Deeply hide secrets on Android
MIT License
395 stars 40 forks source link

CPP files don't get compiled #24

Closed houmie closed 3 years ago

houmie commented 3 years ago

There is a problem with compiling the C code.

When adding this entry in grade build:

externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
        }
    }

And syncing gradle I get this error message:

Gradle project cmake.path is /Users/houmie/projects/t-android/app/src/main/cpp/CMakeLists.txt but that file doesn't exist
Gradle project cmake.path is /Users/houmie/projects/t-android/app/src/main/cpp/CMakeLists.txt but that file doesn't exist
Gradle project cmake.path is /Users/houmie/projects/t-android/app/src/main/cpp/CMakeLists.txt but that file doesn't exist
Gradle project cmake.path is /Users/houmie/projects/t-android/app/src/main/cpp/CMakeLists.txt but that file doesn't exist
Gradle project cmake.path is /Users/houmie/projects/t-android/app/src/main/cpp/CMakeLists.txt but that file doesn't exist
Gradle project cmake.path is /Users/houmie/projects/t-android/app/src/main/cpp/CMakeLists.txt but that file doesn't exist
Gradle project cmake.path is /Users/houmie/projects/t-android/app/src/main/cpp/CMakeLists.txt but that file doesn't exist
Gradle project cmake.path is /Users/houmie/projects/t-android/app/src/main/cpp/CMakeLists.txt but that file doesn't exist
/Users/houmie/projects/t-android/app/src/main/cpp/CMakeLists.txt : C/C++ debug|x86 : Failed to activate protocol version: "CMAKE_HOME_DIRECTORY" is set but incompatible with configured source directory value.
/Users/houmie/projects/t-android/app/src/main/cpp/CMakeLists.txt : C/C++ debug|x86 : Waiting for type "handshake".
executing external native build for cmake /Users/houmie/projects/t-android/app/src/main/cpp/CMakeLists.txt

I went then ahead and created the cpp directory and placed an empty text file called CMakeLists.txt. Then I could sync the gradle.

Then I ran:

./gradlew hideSecret -Pkey=Houmie -PkeyName=YourSecretKeyName 

It creates four files under cpp folder. secrets.cpp, sha256.cpp + headers. Secrets is also generated for Kotlin.

So far so good, but when I run it in the project:

val key = Secrets().getYourSecretKeyName(packageName)

It crashes:

java.lang.UnsatisfiedLinkError: dlopen failed: library "libsecrets.so" not found

It hasn't built it. What am I missing please? Thank you

houmie commented 3 years ago

Great news, I figured it out.

I copied the file from https://github.com/klaxit/hidden-secrets-gradle-plugin/blob/master/src/main/resources/cpp/CMakeLists.txt and placed it under myProject/src/main/resources/cpp

Now it works. Absolutely brilliant. But I think this step should be mentioned in the documentation. Unless this was meant to be done automatically, and I found a bug. I leave this open to see what others think.

Thank you for this amazing project.

ben-j69 commented 3 years ago

@houmie nice to see you found a work around. The creation of the cpp folder and the copy of the CMakeLists is automatic when you run ./gradlew hideSecret.

If you created the folder and the file before to run the command the copy was not done (because the file already exists) and the project could not compile.

If the documentation is not clear enough feel free to suggest modifications.

I will close this issue since everything works.

houmie commented 3 years ago

@ben-j69 Cheers. Yes, I think I know what has gone wrong. I inserted

android {

    ...

    // Enable NDK build
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
        }
    }
}

before the gradle was synced from previous step. This has led to CMakeLists.txt not exist. I have a habit to put everything in Gradle and then sync it once. The fault was on my side, I had to sync the gradle twice in step 1 and 3 separately. I think we are good.

Do you guys provide this project also for iOS? Such a useful feature to have for protecting API keys. Thanks

ben-j69 commented 3 years ago

@houmie not sure it is a sync issue, when you run ./gradlew hideSecret it should sync the project, before to add externalNativeBuild in the gradle file ?

We don't provide the same solution for iOS, but I recommend you Cocoapods Keys which work in the same way.

houmie commented 3 years ago

@ben-j69 I just tested it again. I deleted the cpp folder and ran ./gradlew hideSecret. And it syncs the files over. You were right.

The steps you mentioned is correct. Yesterday I had mistakenly added externalNativeBuild in gradle before running ./gradlew hideSecret. This caused the issue. So the documentation is correct, I didn't pay enough attention. :-)

Thank you for recommending Cocopods Keys, I will try it out.

ben-j69 commented 3 years ago

@houmie thanks for your feedback.