Android studio gradle 自定义任务与 shell 脚本在构建时不起作用

标签 android android-studio android-gradle-plugin build.gradle sh

我已经加密了一个文件(API key 、密码等),添加到代码库中。我需要添加一个自定义任务(任务encIt)来解密文件(将调用decrypt.sh来解密)。此任务添加在build.gradle文件中。

我已经通过从终端调用“gradle encIt”对此进行了测试,它工作正常(按预期解密文件),但我需要每次构建 gradle 时都发生这种情况;怎么做?

这是任务:

     task encIt(type:Exec) {
       println("WORKING")
       commandLine "./decrypt.sh"
       println("WORKING")
     }

这是脚本文件:

   #!/usr/bin/env sh
   echo "inside working"
   openssl enc -aes-256-cbc -d  -in ../encrypted.key   -out  key.txt                     -pass pass:Abcdk5551
  echo "inside working"

我已经测试过了

场景 1:gradle encIt

 result : WORKING
          inside working
          inside working
          WORKING
   The file is decrypted

场景 1:gradle/gradlew(无参数)

 result : WORKING
          WORKING
   The file is not getting decrypted

请帮忙!

使用gradle版本5.4.1 MacBook Pro Android Studio 3.4.1

最佳答案

编辑app/build.gradle,添加以下代码

task encIt(type:Exec) {
    println("WORKING")
    commandLine "./decrypt.sh"
    println("WORKING")
}

afterEvaluate {
    assembleDebug.dependsOn encIt
    assembleRelease.dependsOn encIt
}

它的工作对象:

  1. 点击 Android Studio 上的run按钮
  2. 运行./gradlew build
  3. 运行./gradlew assemble
  4. 运行./gradlew assembleDebug
  5. 运行./gradlew assembleRelease

关于Android studio gradle 自定义任务与 shell 脚本在构建时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56494841/

相关文章:

Android Studio 找不到我的资源

java - 使用一些声音文件启动 Intent

android - android studio 3.0 缺少 GPU 监视器

android - eclipse android 图形布局编辑器的错误?

Android - 我们应该将额外的业务逻辑放在架构中的什么位置?

android - 单击按钮后如何重复相同的文本?

android - 在 Android 上,保存 api url 等设置的最佳位置

android - 如何显示内部存储中的位图?

android - 能够更改通过Gradle(Android Studio)导入的库的代码吗?

android - Gradle buildTypes 不兼容的类型