![](/img/trans.png)
[英]How to upload an android aar library to maven central with other aar dependencies?
[英]How to add a maven manifest to an AAR which specifies dependencies
我(終於)能夠使用本指南將我的 Android 庫發布到 AWS S3 maven 存儲庫。 它作為 AAR 而不是 JAR 文件發布,這意味着即使生成的 POM 文件列出了其所有內部依賴項,在將庫添加到另一個項目時它們也會被忽略。 似乎是一個很常見的問題。 我從中了解到, 我只需要在 maven manifest 中列出依賴項,但是這是如何完成的呢?
我真的是這方面的新手,所以越簡單越好......謝謝!
注意:我也發現了這個問題,所以我在依賴實現行的末尾添加了“{transitive=true}”並且它起作用了。
所以現在要成功包含我的庫,依賴項必須編碼為:
implementation (group: 'com.mygroup', name: 'my_library', version: '1.3', ext: 'aar', classifier: 'release') { transitive=true changing=true }
(我包括“改變=真”來強制它每次重新下載庫。)
如果我不包含“transitive=true”,我會收到以下錯誤:
02-04 20:11:48.462 10225-10225/com.mydomain.app.testapplication4 E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
02-04 20:14:07.080 10225-10225/com.mydomain.app.testapplication4 E/dalvikvm: Could not find class 'android.app.NotificationChannel', referenced from method com.mydomain.library.mydomain$1.onReceive
02-04 20:14:07.290 10225-10225/com.mydomain.app.testapplication4 E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: com.google.firebase.iid.FirebaseInstanceId
at com.mydomain.library.MyActivity.onCreate(MyActivity.java:88)
at android.app.Activity.performCreate(Activity.java:4701)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1051)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1924)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4477)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
at dalvik.system.NativeStart.main(Native Method)
這是我用來發布庫的最新腳本(注意它只發布發布版本,但依賴行仍然需要“發布”分類器):
apply plugin: 'maven-publish'
group = 'com.mydomain'
version = '1.3'
// Add sources as an artifact
task sourceJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier "source"
}
// Loop over all variants
android.libraryVariants.all { variant ->
if (variant.buildType.name == "release") { // only release build
variant.outputs.all { output ->
// This creates a publication for each variant
publishing.publications.create(variant.name, MavenPublication) {
// The sources artifact from earlier
artifact sourceJar
// Variant dependent artifact, e.g. release, debug
artifact source: output.outputFile, classifier: output.name
// Go through all the dependencies for each variant and add them to the POM
// file as dependencies
pom.withXml {
final dependenciesNode = asNode().appendNode('dependencies')
ext.addDependency = { Dependency dep, String scope ->
if (dep.group == null || dep.version == null || dep.name == null || dep.name == "unspecified")
return // ignore invalid dependencies
final dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', dep.group)
dependencyNode.appendNode('artifactId', dep.name)
dependencyNode.appendNode('version', dep.version)
dependencyNode.appendNode('scope', scope)
if (!dep.transitive) {
// If this dependency is transitive, we should force exclude all its dependencies them from the POM
final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
exclusionNode.appendNode('groupId', '*')
exclusionNode.appendNode('artifactId', '*')
} else if (!dep.properties.excludeRules.empty) {
// Otherwise add specified exclude rules
final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
dep.properties.excludeRules.each { ExcludeRule rule ->
exclusionNode.appendNode('groupId', rule.group ?: '*')
exclusionNode.appendNode('artifactId', rule.module ?: '*')
}
}
}
// List all "compile" dependencies (for old Gradle)
configurations.compile.getDependencies().each { dep -> addDependency(dep, "compile") }
// List all "api" dependencies (for new Gradle) as "compile" dependencies
configurations.api.getDependencies().each { dep -> addDependency(dep, "compile") }
// List all "implementation" dependencies (for new Gradle) as "runtime" dependencies
configurations.implementation.getDependencies().each { dep -> addDependency(dep, "runtime") }
}
}
}
}
}
// Ensure that the publish task depends on assembly
tasks.all { task ->
if (task instanceof AbstractPublishToMaven) {
task.dependsOn assemble
}
}
// Configure the destination repository with
// S3 URL and access credentials
publishing {
// Properties properties = new Properties()
// properties.load(file('maven.properties').newDataInputStream())
repositories {
maven {
url "s3://androidsdk.mydomain.com.s3.amazonaws.com"
credentials(AwsCredentials) {
accessKey "myaccesskey"
secretKey "mysecretkey"
}
}
}
}
以下是庫的 gradle.build 文件中的依賴項:
dependencies {
api fileTree(include: ['*.jar'], dir: 'libs')
api 'com.android.support:appcompat-v7:27.1.1'
api 'com.android.support.constraint:constraint-layout:1.1.3'
api 'com.android.support:design:27.1.1'
api 'io.reactivex.rxjava2:rxandroid:2.1.0'
api 'io.reactivex.rxjava2:rxjava:2.2.4'
api 'com.github.instacart.truetime-android:library-extension-rx:3.3'
api 'com.google.dagger:dagger-android:2.15'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.15'
annotationProcessor 'com.google.dagger:dagger-compiler:2.15'
api 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
api 'com.google.firebase:firebase-core:16.0.7'
api 'com.google.firebase:firebase-messaging:17.3.4'
}
我在想下面生成的 POM 文件可能與庫的依賴項不匹配,但它們確實如此。 POM 文件中的“范圍”標簽應該不需要“transitive=true”,對吧?
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mydomain</groupId>
<artifactId>my_library</artifactId>
<version>1.3</version>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>com.android.support</groupId>
<artifactId>appcompat-v7</artifactId>
<version>27.1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.android.support.constraint</groupId>
<artifactId>constraint-layout</artifactId>
<version>1.1.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.android.support</groupId>
<artifactId>design</artifactId>
<version>27.1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxandroid</artifactId>
<version>2.1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<version>2.2.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.instacart.truetime-android</groupId>
<artifactId>library-extension-rx</artifactId>
<version>3.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.dagger</groupId>
<artifactId>dagger-android</artifactId>
<version>2.15</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.nostra13.universalimageloader</groupId>
<artifactId>universal-image-loader</artifactId>
<version>1.9.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-core</artifactId>
<version>16.0.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-messaging</artifactId>
<version>17.3.4</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
您應該能夠通過更改POM XML生成來解決此問題,以便<packaging>
部分設置為aar
而不是pom
:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.humanvideoboard</groupId>
<artifactId>hvb_library</artifactId>
<version>1.0</version>
<packaging>aar</packaging>
但是,現在我正在重新檢查它,它與transitive
依賴關系一起工作的原因是在構建庫時或在構建使用庫的東西時使用implementation
vs api
。 如果您的庫將其依賴項中的類型作為其公共API的一部分公開,則需要在庫構建中正確處理這些構建依賴項。 有關更多信息,請參閱此頁面 。
我遇到過類似的問題,但我的問題是在發布塊中我使用了“庫”函數而不是“maven”函數
在我的庫 build.gradle 我有這樣的塊:
afterEvaluation{
publishing {
publications {
maven(MavenPublication) { // here I had library(MvenPublication) which caused the packaing to be pom instead of aar
artifacts = ["build/outputs/aar/library-release.aar"]
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.