问题:
我正在尝试创建一个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中?目前,它只包括类文件。这是我目前的成绩。发布版本:
// 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)
}
}
}
}
我能够解决我的问题。我在这里找到了解决方案。
我最初的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
}
}
}