2015-09-08 9 views
7

Android Studio wird den Standard-APK-Namen als app- (release | debug) .apk generieren.
Wie apk Dateiname wie Paketname der App wie com.example-debug.apk generieren.Apk-Namen als Paketname in Android Studio generieren

+2

Link kann http://stackoverflow.com/questions/22126299/change-apk-name-with-gradle/22126638#22126638 helfen –

+0

Der obige Link ist nützlich –

Antwort

4

Versuchen Sie, diese build.gradle die in Ihrem Modul setzen

applicationVariants.all { variant -> 
    variant.outputs.each { output -> 
     def file = output.outputFile 
     def appId = android.defaultConfig.applicationId 
     def fileName = appId + "-" variant.buildType.name +".apk" 
     output.outputFile = new File(file.parent, fileName) 
    } 
} 
+0

Dank für seine Arbeits Update. – Bangaram

+0

Das spuckt nur ein APK für mich aus und spuckt für jede Build-Variante kein APK aus. – Subby

+0

Natürlich nicht, wenn Sie apk für eine andere Variante möchten, müssen Sie zuerst die Build-Variante ändern. Oder verwenden Sie einen Befehl, um alle Versionen zu erstellen. Dieser Code ändert nur den Namen der apk, nichts mehr. – slezadav

1

können Sie diese link sehen. oder Unlogische Option zum Umbenennen Ihrer release|debug.apk mit dem Namen, was Sie im Dateibrowser wollen.

kann dieser Code für Sie nützlich sein:

buildTypes { 
release { 
    minifyEnabled false 
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    applicationVariants.all { variant -> 
     variant.outputs.each { output -> 
      def formattedDate = new Date().format('yyyyMMddHHmmss') 
      def newName = output.outputFile.name 
      newName = newName.replace("app-", "$rootProject.ext.appName-") //"MyAppName" -> I set my app variables in the root project 
      newName = newName.replace("-release", "-release" + formattedDate) 
      //noinspection GroovyAssignabilityCheck 
      output.outputFile = new File(output.outputFile.parent, newName) 
     } 
    } 
} 
    debug { 
    } 
} 

Code :)

0

Erstellen Sie eine Datei mit dem Namen customname.gradle in der obersten Ebene des project.Put hinein diesen Code genießen.

android.applicationVariants.all { variant ->; 
def appName 
//Check if an applicationName property is supplied; if not use the name of the parent project. 
if (project.hasProperty("applicationName")) { 
    appName = applicationName 
} else { 
    appName = parent.name 
} 

variant.outputs.each { output ->; 
    def newApkName 
    //If there's no ZipAlign task it means that our artifact will be unaligned and we need to mark it as such. 
    if (output.zipAlign) { 
     newApkName = "${appName}-${output.baseName}-${variant.versionName}.apk" 
    } else { 
     newApkName = "${appName}-${output.baseName}-${variant.versionName}-unaligned.apk" 
    } 
    output.outputFile = new File(output.outputFile.parent, newApkName) 
}} 

Dann in Ihrem gradle App-Modul diesen Code hinzufügen

apply from: "../customname.gradle" 
15

Sie können es tun, ohne die anderen Aufgaben zu verwenden, die archivesBaseName Einstellung.

Zum Beispiel:

defaultConfig { 
     .... 
     project.ext.set("archivesBaseName", "MyName-" + defaultConfig.versionName); 

    } 

Ausgang:

MyName-1.0.12-release.apk 

In Ihrem Fall:

project.ext.set("archivesBaseName", "com.example"); 
+0

Sie können möglicherweise auf rootProject name zugreifen und es wie folgt verwenden: 'project.ext.set (" archivesBaseName ", rootProject.getName() + "-" + defaultConfig.versionName); ' –

+0

skaliert leider nicht, wenn Sie mit Varianten arbeiten, da archivesBaseName global für alle ist – Ewoks

0

Dies kann Ihnen helfen. Dieser Code erstellt einen App-Namen wie applicationId-release.apk oder applicationId-debug.apk, in dem ApplicationId Ihr Paketname sein kann.

buildTypes { 

    applicationVariants.all { variant -> 
     variant.outputs.each { output -> 
      def newName = output.outputFile.name 
      newName = newName.replace("app-", applicationId) 
      output.outputFile = new File(output.outputFile.parent, newName) 
     } 
    } 
}