183

Gradleの自動生成されたAPKファイル名に特定のバージョン番号を設定しようとしています。

gradlemyapp-release.apkが生成されるようになりましたが、myapp-release-1.0.apk.

乱雑に見えるオプションの名前を変更してみました。これを行う簡単な方法はありますか?

buildTypes {
    release {
       signingConfig signingConfigs.release
       applicationVariants.each { variant ->
       def file = variant.outputFile
       variant.outputFile = new File(file.parent, file.name.replace(".apk", "-" +    defaultConfig.versionName + ".apk"))
    }
}

上記のコードを試してみましたが、うまくいきませんでした。助言がありますか?(グラドル1.6を使用)

4

16 に答える 16

244

バージョン名を 1 か所だけ変更する必要があります。コードもシンプル。

以下の例では、選択したビルド バリアントに応じて、 MyCompany -MyAppName-1.4.8-debug.apkまたは MyCompany-MyAppName-1.4.8- release.apkという名前の apk ファイルを作成します。

このソリューションは、APK とApp Bundle (.aab ファイル)の両方で機能することに注意してください。

関連項目: Android プロジェクトの gradle でプロガード マッピング ファイル名を変更する方法

#最近の Gradle プラグインの解決策

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"
    defaultConfig {
        applicationId "com.company.app"
        minSdkVersion 13
        targetSdkVersion 21
        versionCode 14       // increment with every release
        versionName '1.4.8'   // change with every release
        setProperty("archivesBaseName", "MyCompany-MyAppName-$versionName")
    }
}

上記のソリューションは、次の Android Gradle プラグイン バージョンでテストされています。

  • 3.6.4 (2020 年 8 月)
  • 3.5.2 (2019 年 11 月)
  • 3.3.0 (2019 年 1 月)
  • 3.1.0 (2018 年 3 月)
  • 3.0.1 (2017 年 11 月)
  • 3.0.0 (2017 年 10 月)
  • 2.3.2 (2017 年 5 月)
  • 2.3.1 (2017 年 4 月)
  • 2.3.0 (2017 年 2 月)
  • 2.2.3 (2016 年 12 月)
  • 2.2.2
  • 2.2.0 (2016 年 9 月)
  • 2.1.3 (2016 年 8 月)
  • 2.1.2
  • 2.0.0 (2016 年 4 月)
  • 1.5.0 (2015/11/12)
  • 1.4.0-beta6 (2015/10/05)
  • 1.3.1 (2015/08/11)

新しいバージョンが出たら、この記事を更新します。

#Solution Tested only on versions 1.1.3-1.3.0 次のソリューションは、次の Android Gradle プラグイン バージョンでテストされています。

  • 1.3.0 (2015/07/30) - 動かない、1.3.1で修正予定のバグ
  • 1.2.3 (2015/07/21)
  • 1.2.2 (2015/04/28)
  • 1.2.1 (2015/04/27)
  • 1.2.0 (2015/04/26)
  • 1.2.0-beta1 (2015/03/25)
  • 1.1.3 (2015/03/06)

アプリ gradle ファイル:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"
    defaultConfig {
        applicationId "com.company.app"
        minSdkVersion 13
        targetSdkVersion 21
        versionCode 14       // increment with every release
        versionName '1.4.8'   // change with every release
        archivesBaseName = "MyCompany-MyAppName-$versionName"
    }
}
于 2015-03-11T17:02:52.233 に答える
49

(Android Studio 3.0およびGradle 4で動作するように編集)

より複雑な apk ファイル名の名前変更オプションを探していたので、他の人に役立つことを期待してこれを書きました。apk の名前を次のデータで変更します。

  • 風味
  • ビルドタイプ
  • バージョン
  • 日にち

Gradleクラスで少し調査し、他の回答から少しコピー/貼り付けする必要がありました。私はグラドル 3.1.3を使用しています。

build.gradle で:

android {

    ...

    buildTypes {
        release {
            minifyEnabled true
            ...
        }
        debug {
            minifyEnabled false
        }
    }

    productFlavors {
        prod {
            applicationId "com.feraguiba.myproject"
            versionCode 3
            versionName "1.2.0"
        }
        dev {
            applicationId "com.feraguiba.myproject.dev"
            versionCode 15
            versionName "1.3.6"
        }
    }

    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def project = "myProject"
            def SEP = "_"
            def flavor = variant.productFlavors[0].name
            def buildType = variant.variantData.variantConfiguration.buildType.name
            def version = variant.versionName
            def date = new Date();
            def formattedDate = date.format('ddMMyy_HHmm')

            def newApkName = project + SEP + flavor + SEP + buildType + SEP + version + SEP + formattedDate + ".apk"

            outputFileName = new File(newApkName)
        }
    }
}

今日 (2016 年 10 月 13 日) 10:47 にコンパイルすると、選択したフレーバーとビルド タイプに応じて、次のファイル名が得られます。

  • 開発デバッグ: myProject_ dev_debug_1.3.6 _131016_1047.apk
  • 開発リリース: myProject_ dev_release_1.3.6 _131016_1047.apk
  • 製品デバッグ: myProject_ prod_debug_1.2.0 _131016_1047.apk
  • 製品リリース: myProject_ prod_release_1.2.0 _131016_1047.apk

注: アラインされていないバージョンの apk 名は、デフォルトの名前のままです。

于 2016-10-13T09:14:48.357 に答える
19

build.gradle要約すると、 (私のように)パッケージをインポートする方法がわからない人のために、次buildTypesを使用してください。

buildTypes {
      release {
        signingConfig signingConfigs.release
        applicationVariants.all { variant ->
            def file = variant.outputFile
            def manifestParser = new com.android.builder.core.DefaultManifestParser()
            variant.outputFile = new File(file.parent, file.name.replace(".apk", "-" + manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile) + ".apk")) 
        }
    }       
}

=====編集=====

ファイルにversionCodeandversionNameを次のように設定した場合:build.gradle

defaultConfig {
    minSdkVersion 15
    targetSdkVersion 19
    versionCode 1
    versionName "1.0.0"
}

次のように設定する必要があります。

buildTypes {   
        release {
            signingConfig signingConfigs.releaseConfig
            applicationVariants.all { variant ->
                def file = variant.outputFile
                variant.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
            }
        }
}


====== Android Studio 1.0で編集 ======

Android Studio 1.0 を使用している場合、次のようなエラーが表示されます。

Error:(78, 0) Could not find property 'outputFile' on com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated@67e7625f.

build.Typesこの部分を次のように変更する必要があります。

buildTypes {
        release {
            signingConfig signingConfigs.releaseConfig
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    output.outputFile = new File(output.outputFile.parent, output.outputFile.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
                }
            }
        }
    }
于 2014-07-09T09:18:01.973 に答える
7

別の方法として、次を使用することもできます。

String APK_NAME = "appname"
int VERSION_CODE = 1
String VERSION_NAME = "1.0.0"

project.archivesBaseName = APK_NAME + "-" + VERSION_NAME;

    android {
      compileSdkVersion 21
      buildToolsVersion "21.1.1"

      defaultConfig {
        applicationId "com.myapp"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode VERSION_CODE
        versionName VERSION_NAME
      }

       .... // Rest of your config
}

これにより、すべての apk 出力に「appname-1.0.0」が設定されます。

于 2014-12-12T17:06:53.567 に答える
3

完全に、またはいくつかの変更後に正しい答えがたくさんあります。しかし、タスクにフックしてスクリプトを使用して VersionName と VersionCode を動的に生成していたため、それらすべてに問題があったため、とにかく追加しpreBuildます。

同様のアプローチを使用している場合、これは機能するコードです。

project.android.applicationVariants.all { variant ->
    variant.preBuild.doLast {
    variant.outputs.each { output ->
        output.outputFile = new File(
                output.outputFile.parent,
                output.outputFile.name.replace(".apk", "-${variant.versionName}@${variant.versionCode}.apk"))
        }
    }
}

説明: 最初のアクションでバージョン コードと名前をオーバーライドしpreBuildているため、このタスクの最後にファイルの名前変更を追加する必要があります。したがって、この場合、gradle が行うことは次のとおりです。

バージョン コード/名前を挿入 -> preBuild アクションを実行 -> apk の名前を置換

于 2015-09-01T09:31:54.120 に答える
1

私の場合、このエラーをこの方法で解決します

デバッグ バージョンにサフィックスを追加します。この場合、「-DEBUG」テキストをデバッグ デプロイに追加します。

 buildTypes {
        release {

            signingConfig signingConfigs.release
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'


        }
        debug {

            defaultConfig {
                debuggable true

                versionNameSuffix "-DEBUG"
            }
        }
    }
于 2016-02-05T12:10:44.900 に答える
0

最新の gradle バージョンでは、次のスニペットを使用できます。

最初にアプリケーション マニフェストの場所を設定します

 sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
        {
    }

そして後でbuild.gradleで

import com.android.builder.core.DefaultManifestParser

def getVersionName(manifestFile) {
    def manifestParser = new DefaultManifestParser();
    return manifestParser.getVersionName(manifestFile);
}

def manifestFile = file(android.sourceSets.main.manifest.srcFile);
def version = getVersionName(manifestFile)

buildTypes {
    release {
       signingConfig signingConfigs.release
       applicationVariants.each { variant ->
       def file = variant.outputFile
       variant.outputFile = new File(file.parent, file.name.replace(".apk", "-" +    versionName + ".apk"))
    }
}

ビルド タイプごとに異なるマニフェストがある場合は調整します。しかし、私は単一のものを持っているので、私にとっては完璧に機能します。

于 2015-02-22T03:35:12.613 に答える
0

ここで答えたように、バージョン名とバージョンコードを出力ファイルに追加する場合は、次のようにします。

applicationVariants.all { variant ->
        variant.outputs.all {
            def versionName = variant.versionName
            def versionCode = variant.versionCode
            def variantName = variant.name
            outputFileName = "${rootProject.name}" + '_' + variantName + '_' + versionName + '_' + versionCode + '.apk'
        }
    }
于 2020-11-05T05:39:12.897 に答える