2013-08-23 17 views
9

Pracując na bibliotece Android z Gradle (v 1.7) jako narzędziem do budowania, użyłem wtyczki maven i skonfigurowałem zadanie uploadArchives do opublikowania wersji wydania i debugowania lib do lokalnego repozytorium maven.
Poniższy kod działa ok:Gradle i Android: konfiguracja pom z wieloma publikacjami artefaktów Maven

// [...] 
apply plugin: 'android-library' 

// [...] nothing unusual 

/* 
* Define name of the apk output file (build/apk/<outputFile>) 
*/ 
    android.libraryVariants.all 
    { 
     variant -> 
     def outputName = "MyModule-${android.defaultConfig.versionName}-${variant.baseName}.aar" 
     variant.outputFile = new File("$buildDir/libs", outputName) 
    } 


/* 
* Publish to maven local repo (older style maven plugin) 
* Used while android plugin is not fixed regarding maven-publish plugin 
* 
* type command "gradle uploadArchives" to publish the module into the 
* local .m2 repository 
*/ 
apply plugin: 'maven' 

android.libraryVariants.all 
{ 
    variant -> 
    // add final apk to the 'archives' configuration 
    project.artifacts 
    { 
     archives variant.outputFile 
    } 
} 

def localRepoPath = "file://" + new File(
    System.getProperty("user.home"), ".m2/repository").absolutePath 

uploadArchives 
{ 
    repositories.mavenDeployer 
    {  
     repository(url: localRepoPath) 

     addFilter('debug') { artifact, file -> 
      artifact.name.contains("debug") 
     } 
     addFilter('release') { artifact, file -> 
      artifact.name.contains("release") 
     } 

     pom('debug').groupId = 'com.company' 
     pom('release').groupId = 'com.company' 
     pom('debug').artifactId = 'id' 
     pom('release').artifactId = 'id' 
     pom('debug').version = android.defaultConfig.versionName + "d" 
     pom('release').version = android.defaultConfig.versionName 
     pom.packaging = 'aar' 
    } 
} 
uploadArchives.dependsOn(assemble) 

Jednak podczas próby byłaby konfigurację pom:

uploadArchives 
{ 
    repositories.mavenDeployer 
    {  
     repository(url: localRepoPath) 

     addFilter('debug') { artifact, file -> 
      artifact.name.contains("debug") 
     } 
     addFilter('release') { artifact, file -> 
      artifact.name.contains("release") 
     } 

     pom.groupId = 'com.company' 
     pom.artifactId = 'id' 
     pom('debug').version = android.defaultConfig.versionName + "d" 
     pom('release').version = android.defaultConfig.versionName 
     pom.packaging = 'aar' 
    } 
} 

artifactId jest rozwinięty jak nazwa pliku wyjściowego i GroupID jako nazwa katalogu głównego; w ten sposób dając złe ścieżki w repozytorium maven.

Chciałbym wiedzieć, dlaczego tak jest, i być może, jeśli istnieje czystszy sposób, aby osiągnąć to, czego potrzebuję.

+0

W moim projekcie miałem wymóg zmiany nazwy wyjścia '.aar'. Dlatego ustawiam 'archivesBaseName' w' build.gradle', który jest używany dla 'artifactId'. Może to pomaga. – JJD

+0

Wymyśliłeś to? –

+0

@YuchenZhong Niestety nie, ponieważ ta praca była częścią krótkoterminowego stażu. Nie wiem, kto zajął się projektem, a zatem, jak to się stało od tego czasu. – mklj

Odpowiedz

3

W celach informacyjnych w ten sposób przesyłamy wiele plików APK. Być może nie jest to dokładnie to, czego potrzebujesz, ponieważ przesyłamy wiele plików APK po splitach APK, podczas gdy próbujesz przesłać wiele plików APK z różnych typów kompilacji (debugowanie &). Ale teoretycznie powinny być takie same.

//declare some Variables for later use 
def projectName = "ProjectName" 
def versionString = "1.0.0" 
def baseVersionCode = 1 

// Values based on https://developer.android.com/ndk/guides/abis.html#sa 
ext.abiCodes = ['armeabi-v7a': 1, 
       'arm64-v8a' : 2, 
       'x86'  : 3, 
       'x86_64'  : 4] 

// collect artifacts, important for the `uploadArchives` to work 
artifacts { 
    if (new File('app/build/outputs/apk').exists()) { 
    new File('app/build/outputs/apk').eachFile() { file -> 
     if (file.toString().contains("productionRelease")) { 
     archives file: file 
     } 
    } 
    } 
} 

uploadArchives { 
    repositories { 
    mavenDeployer { 
     repository(url: "http://...") 

     project.ext.abiCodes.values().each{ abiVersionCode -> 
     def version = "${versionString}.${baseVersionCode + abiVersionCode}" 
     addFilter(version) { artifact, file -> artifact.name.contains(version) } 

     pom(version).artifactId = projectName.toLowerCase() 
     pom(version).groupId = 'com.yourcompanyname' 
     pom(version).version = version 
     } 
    } 
    } 
} 

android { 
    defaultPublishConfig "productionRelease" 

    buildTypes { 
     productionRelease { 
      minifyEnabled false 
      zipAlignEnabled true 
      //...more Config like proguard or signing 
     } 
    } 

    applicationVariants.all { variant -> 
    variant.outputs.each { output -> 
     def abiName = output.getFilter(com.android.build.OutputFile.ABI) 
     def abiVersionCode = project.ext.abiCodes.get(abiName)  
     output.versionCodeOverride = variant.versionCode + abiVersionCode 
     output.versionNameOverride = "$versionString (${output.versionCodeOverride})" 
     def apkName = "$projectName-${variant.name}-v${versionString}.${output.versionCodeOverride}.apk" 
     output.outputFile = new File(output.outputFile.parent, apkName) 
    } 
    } 
+0

Świetne +1. Dokładnie to, co musiałem podzielić na abis. Właśnie dodałem kilka brakujących elementów dla innych deweloperów. –

+0

Hej, @RafaelT Nie sądzę, że 'zipAlignEnabled true' jest potrzebna, ponieważ domyślnie jest ustawiona na true dla budowania produkcji. https://stackoverflow.com/a/27945728/1035008 –

+0

true, właśnie dodałem buildType 'productionRelease' ponieważ całkowicie go brakowało @Yuchen Zhong –

0

Musisz (ED), aby ustawić nazwę filtru dla groupId i artifactId taki sam jak masz na version

pom('debug').groupId = 'com.company' 
pom('release').groupId = 'com.company' 
pom('debug').artifactId = 'id' 
pom('release').artifactId = 'id' 
pom('debug').version = android.defaultConfig.versionName + "d" 
pom('release').version = android.defaultConfig.versionName 

Im zaskoczony uciec z nazwą wersji przyrostka, jak jej nie semver.

Powiązane problemy