Artifactory使用资源

时间:2015-05-30 19:40:26

标签: android maven gradle android-library artifactory

问题:

我正在尝试使用以下命令创建一个可以包含在应用程序中的Android库:

// Application's build.gradle
compile 'com.mycompany:mylibrary:1.0.0'

就我而言,我正在使用 Artifactory ,我已经完成了上述工作。问题是当我尝试运行应用程序时我缺少资源。问题似乎是我的库的代码依赖于未发布到 Artifactory

的jar中的资源
// Code in library, throws exception because <resource> is not included in the jar
getString(R.string.<resource>)

问题:

在发布到 Artifactory 时,如何在jar中包含资源?目前,它只包含类文件。这是我目前用于发布的gradle.build:

// Android Library - build.gradle
{
    // ... Other build.gradle settings go here

    //==========================================================
    //  ARTIFACTORY SETTINGS
    //==========================================================

    buildscript {
        repositories {
            maven {
                url "${artifactory_contextUrl}/plugins-release"
                credentials {
                    username = "${artifactory_user}"
                    password = "${artifactory_password}"
                }
            }
        }
        dependencies {
            classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.0.3"
        }
    }

    apply plugin: "com.jfrog.artifactory"
    apply plugin: 'maven-publish'

    artifactory {
        contextUrl = "${artifactory_contextUrl}"
        publish {
            repository {
                repoKey = 'libs-release-local'
                username = "${artifactory_user}"
                password = "${artifactory_password}"
                maven = true
            }
            defaults {
                publications ('mavenJava')
            }
        }
        resolve {
            repository {
                repoKey = 'libs-release'
                username = "${artifactory_user}"
                password = "${artifactory_password}"
                maven = true
            }
        }
    }

    task sourceJar(type: Jar) {
        from android.sourceSets.main.java.srcDirs
    }

    publishing {
        publications {
            mavenJava(MavenPublication) {
                groupId 'com.mycompany'
                artifactId 'mylibrary'
                version '1.0.0'
                artifact(sourceJar)
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我能够解决我的问题。我找到了解决方案herehere

我原来的gradle.build脚本适合发布jar文件;但是,在构建用于其他项目的Android库时,您通常需要 .aar 文件,而不是.jar 文件。

为了让Artifactory发布 .aar 文件,我在下面复制了我的build.gradle。

注意:您必须使用com.android.library插件,否则最终会使用 .apk 而不是 .aar

apply plugin: 'com.android.library'

android {
    //... Android specific parameters
}

dependencies {
    //... Android specific dependencies
}

//==========================================================
//  ARTIFACTORY SETTINGS
//==========================================================

buildscript {
    repositories {
        maven {
            url "${artifactory_contextUrl}/plugins-release"
            credentials {
                username = "${artifactory_user}"
                password = "${artifactory_password}"
            }
        }
    }
    dependencies {
        classpath 'com.github.dcendents:android-maven-plugin:1.2'
        classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:2.2.3'
    }
}

apply plugin: 'artifactory'
apply plugin: 'com.github.dcendents.android-maven'

version = "1.0.0"
group = "com.mycompany"

configurations {
    published
}

task sourceJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
}

artifactoryPublish {
    dependsOn sourceJar
}

artifacts {
    published sourceJar
}

artifactory {
    contextUrl = "${artifactory_contextUrl}"
    publish {
        repository {
            repoKey = 'libs-release-local'
            username = "${artifactory_user}"
            password = "${artifactory_password}"
            maven = true
        }
        defaults {
            publishConfigs('archives', 'published')
            properties = ['build.status': 'integration']
            publishPom = true
            publishIvy = false
        }
    }
}