问题
Problem:
I am trying to create an Android Library which can be included in an application using:
// Application's build.gradle
compile 'com.mycompany:mylibrary:1.0.0'
In my case, I am using Artifactory and I have gotten the above to work fine. The problem is when I try to run the application I a missing resources. The problem seems to be that my library has code that depends on resources which are not getting included in the jar published to Artifactory
// Code in library, throws exception because <resource> is not included in the jar
getString(R.string.<resource>)
Question:
How can I include the resources in the jar when publishing to Artifactory? Currently, it only includes the class files. Here is my current gradle.build for publishing:
// 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:
I was able to fix my issue. I found my solution here and here.
My original gradle.build
script was fine for publishing jar files; however, when building an android library for use with other projects you typically want a .aar file, not a .jar file.
In order to have Artifactory publish a .aar file I've copied my build.gradle below.
NOTE: you must use the com.android.library
plugin otherwise you'll end up with a .apk instead of a .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
}
}
}
来源:https://stackoverflow.com/questions/30550588/artifactory-publish-android-library-with-resources