2013-08-23 3 views
9

Gradle (v 1.7)을 빌드 도구로 사용하여 Android 라이브러리에서 작업하면서, 저는 Maven 플러그인을 사용하여 uploadArchives 작업을 구성하여 릴리스 및 디버그 버전을 모두 게시했습니다. lib를 로컬 Maven 저장소에 저장합니다.
아래 코드는 확인 작동합니다Gradle and Android : 다중 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) 

을하지만, 치어 구성 리팩토링 할 때 :로 출력 파일의 이름으로 확장

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를하고 을의 groupId을 루트 디렉토리의 이름. 따라서 메이븐 레포에 나쁜 경로를 제공합니다.

왜 그런지 알고 싶습니다. 그리고 내가 원하는 것을 얻기위한 더 깨끗한 방법이 있다면 어떨까요?

+0

내 프로젝트에서 나는'.aar' 출력의 이름을 바꾸라는 요구 사항이있었습니다. 그러므로'artifactId'에 사용되는'build.gradle'에'archivesBaseName'을 설정했습니다. 아마 이것은 도움이 될 것입니다. – JJD

+0

이것을 알아 냈습니까? –

+0

@YuchenZhong 불행히도이 작품은 단기 인턴쉽의 일부분이므로 아니요. 나는 누가 프로젝트를 담당했는지, 그리고 이후로 어떻게 처리되었는지를 모른다. – mklj

답변

3

참고로 여러 APK를 업로드하는 방법입니다. 다른 빌드 유형 (디버그 & 출시)에서 여러 개의 APK를 업로드하려고 시도하는 동안 APK가 분할 된 후 여러 APK를 업로드하기 때문에 사용자가 필요로하는 것과 정확하게 일치하지 않을 수 있습니다. 그러나 이론적으로는 동일해야합니다.

//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

큰 +1. 정확히 내가 abis에 나눌 필요가 있었던. 그냥 동료 개발자에게 누락 된 부분을 추가했습니다. –

+0

안녕하세요, @RafaelT 'zipAlignEnabled true'는 프로덕션 빌드에 기본적으로 true로 설정되어 있으므로 필요하지 않다고 생각합니다. https://stackoverflow.com/a/27945728/1035008 –

+0

사실, buildType'productionRelease'를 추가했습니다. 왜냐하면 @Yuchen Zhong이 완전히 없어 졌기 때문입니다. –

0

당신은 groupIdartifactId 당신이 임 당신이하지, 멀리 버전 이름 접미사 얻을 놀라게 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 

에 대한이 같은 대한 필터 이름을 설정 (ED)가 필요합니다 semver.