【问题标题】:Is it possible to create another (Build APK(s)) file path in gradle?是否可以在 gradle 中创建另一个(构建 APK(s))文件路径?
【发布时间】:2021-10-29 15:19:14
【问题描述】:

我希望在项目路径中的不同文件位置生成第二个 debug.apk。是否可以在 gradle 中为 .apk 构建创建第二个文件路径位置?

当前构建路径为:

C:\..\app\build\outputs\apk\debug\debug.apk

我想在构建后创建第二个 apk 位置,例如:

C:\..\app\debug_apk\debug.apk

我目前正在更改 gradle 中输出文件的名称:

applicationVariants.all { variant ->
    variant.outputs.all { output ->
        def appVersionName = "company_name_${versionName}"
        switch (buildType.name) {
            case "debug": {
                outputFileName = "${appVersionName}_debug.apk"
                break
            }
            case "release": {
                outputFileName = "${appVersionName}_release.apk"
                break
            }
        }
    }
}

【问题讨论】:

    标签: android android-studio gradle


    【解决方案1】:

    试试这个解决方案:


    我可以通过在gradle file 中运行copy 脚本来做到这一点:

        android.applicationVariants.all { variant ->
         variant.outputs.all {
    
         copy {
          from file("${project.buildDir}/outputs/apk/" + variant.name + "/release/${outputFileName}")
          into file("${project.buildDir}/outputs/apk/")
         }
    
        // i don't want apk on this location so after successful copy i am deleting it
        delete file("${project.buildDir}/outputs/apk/" + variant.name) 
             }
        }
    

    【讨论】:

      【解决方案2】:
      apply plugin: 'android'
      
      def outputPathName = "D:\build.apk"
      def secondDir= "D:\backup\build.apk"
      
      android {
          compileSdkVersion 19
          buildToolsVersion "19.0.3"
      
          defaultConfig {
              minSdkVersion 8
              targetSdkVersion 19
              versionCode 1
              versionName "1.0"
          }
          buildTypes {
              release {
                  runProguard false
                  proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
              }
          }
      
          def publish = project.tasks.create("publishAll")
          android.applicationVariants.all { variant ->
          variant.outputFile = file(outputPathName)
          def task = project.tasks.create("publish${variant.name}Apk", Copy)
          task.from(variant.outputFile)
          task.into(secondDir)
      
          task.dependsOn variant.assemble
          publish.dependsOn task
        }
      
      }
      
      dependencies {
          compile 'com.android.support:appcompat-v7:19.+'
          compile fileTree(dir: 'libs', include: ['*.jar'])
      }
      
      task cleanExtra(type: Delete) {
          delete outputPathName
      }
      
      clean.dependsOn(cleanExtra)
      

      此代码是此resource 和此answer 的组合

      【讨论】:

        【解决方案3】:

        这是一个应用模块构建脚本,它提供以下内容:

        • 创建 copy*Apk 任务,收集每个构建变体的 APK。
        • 创建 copyApks 任务,收集所有构建变体的 APK。
        • (可选)使assemble* 任务也收集其特定的 APK。
        build.gradle.kts
        // Create an umbrella task that will collect APKs from all build variants.
        val copyApks = tasks.register("copyApks")
        
        android {
            applicationVariants.all {
                val variantName = name
        
                // Create variant-specific task that collects the APK.
                val copyApk = tasks.register<Copy>("copy${variantName.capitalize()}Apk") {
        
                    // Copy output files from the task that produces the APK...
                    from(packageApplicationProvider)
        
                    // ...into a directory relative to module source root.
                    into(file("${variantName}_apk"))
        
                    // Filter out any metadata files, only include APKs.
                    include { it.name.endsWith(".apk") }
        
                    // Change the output file name.
                    // Only works if there's a single APK for each variant.
                    // This will not work with APK splits enabled.
                    rename { "${variantName}.apk" }
                }
        
                // Register the variant-specific task under the umbrella task.
                copyApks.dependsOn(copyApk)
        
                // (Optional) collect variant-specific APKs when assemble task is invoked.
                assembleProvider.dependsOn(copyApk)
            }
        }
        
        build.gradle

        如果您使用的是 Groovy,这些行会有所不同:

        def copyApks = tasks.register("copyApks")
        // ...
        def variantName = name
        // ...
        def copyApk = tasks.register("copy${variantName.capitalize()}Apk", Copy) { // ...
        

        用法

        ./gradlew copyApks
        ./gradlew app:copyDebugApk
        ./gradlew app:copyReleaseApk
        ./gradlew app:assemble
        

        结果

        区别

        • 原始 APK 路径未硬编码。相反,它们源自 Android Gradle 插件使用的任何内容。它自动支持product flavors
        • 采集任务依赖原打包任务,参与Gradleup-to-date checks。如果源/APK 未更改,则不会重新收集它们。
        • clean 任务不会删除收集的 APK。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-06-30
          • 2018-07-23
          • 2022-01-26
          • 2020-07-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多