Android 库项目 transient 依赖松散 AAR 类型

标签 android maven android-gradle-plugin android-build

我正在开发一个包含两个 Android 库项目的项目。当我将这些库上传到我的 Maven 存储库 (Nexus) 时,生成的 pom 不包含 <type>aar</type>依赖项中的元素。

这是我的依赖树

* App1
 \
  * lib1
  |\
  | * lib2
  * Other libs

您可以在此图中看到我的 app取决于 lib1 , 这取决于 lib2 .这两个库都是 Android 库项目,因此是 AAR。

lib1/build.gradle

apply plugin: 'com.android.library'
apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    provided project(':lib2')

    compile 'com.squareup.picasso:picasso:2.3.3'
}

lib2/build.gradle

apply plugin: 'com.android.library'
apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion 19
        versionCode 1
        versionName '1.0'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:19.+'
    compile 'com.squareup.retrofit:retrofit:1.6.1'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
    compile 'com.squareup.okhttp:okhttp:2.0.0'
}

注意如何 Lib1包括 Lib2作为 project()依赖。

现在,当我使用 Chris Banes 出色的 gradle-mvn-push 插件部署它时,一切都按预期进行。我在生成的 pom.xml 中只注意到一个奇怪的地方。

<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>lib2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <scope>compile</scope>
    </dependency>
    ...
</dependencies>

请注意 mavenDeployer 插件遗漏了 <type>aar</type>生成 pom 时依赖项中的元素。

在我的应用程序项目(这是一个 Maven 项目)中,我列出了这个依赖项:

<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>lib1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>aar</type>
    </dependency>
    ...
</dependencies>

尝试使用 Maven 构建它时,出现以下错误。

Could not resolve dependencies for project com.example:app:apk:1.0.0-SNAPSHOT: The following artifacts could not be resolved: com.example:lib2:jar:0.0.1-SNAPSHOT

注意它是如何寻找库的 jar 版本的。我相信,因为没有生成 mavenDeployer 插件 <type>aar</type>在生成的pom的依赖列表中,maven默认是在寻找一个jar。

有谁知道如何使用 Gradle 构建包含 transient 依赖项的 Android 库项目?

最佳答案

只需使用任何文本编辑器将 aar 添加到 pom。之后,打开一个控制台进入项目的根目录(pom 所在的位置)并运行“mvn clean install”。将生成 aar Artifact 并将其部署到您的 Maven 存储库。

无论如何,使用 Maven 创建 aar 库要容易得多。只需使用任何 android 库原型(prototype)创建一个 Maven 项目(de.akquinet 的原型(prototype)非常好,但您必须使用 3.8.2 和 3.9 之间的插件版本,http://mvnrepository.com/artifact/de.akquinet.android.archetypes)。只需创建项目,添加库,然后运行命令“mvn clean install”将它们安装到本地 maven 目录中。之后,只需将它们用作普通的 aar 依赖项即可。

关于Android 库项目 transient 依赖松散 AAR 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26026060/

相关文章:

android - 没有单元 ID(AdMob 发布商 ID)的 Google adMob,仅用于测试

android - 仅使用 GCM 为 android 开发聊天应用程序放弃 XMPP 是否好?

android - picasso 磁盘缓存

java - Maven 缺少 Artifact org.geotools :gt-shapefile:jar:11-SNAPSHOT

android - 找不到 Gradle DSL 方法 : 'flavorGroups()'

android - Gradle 5.x - 将我的库发布到 Maven Local 时出错

java - 为团队设置 Maven

java - 如何编译两次?

Android Studio : generate signed apk in release mode , 但我仍然可以在调试列表中看到它

connection - Android Studio 0.8.6 Gradle项目刷新失败,连接重置