简体   繁体   English

如何对本地 gradle 依赖项进行版本控制?

[英]How to version local gradle dependencies?

My Android project contains a library that is shipped as AAR file.我的 Android 项目包含一个作为 AAR 文件提供的库。

There are multiple options to include local AAR files.包含本地 AAR 文件有多种选择。 I can declare a file dependency: implementation files('libs/mylib.aar')我可以声明一个文件依赖项: implementation files('libs/mylib.aar')

Or I can put the AAR into another module and then use a project dependency: implementation project(':mylibmodule')或者我可以将 AAR 放入另一个模块,然后使用项目依赖项: implementation project(':mylibmodule')

However, I want to specify the exact version of my library: mylib:1.0.0但是,我想指定我的库的确切版本: mylib:1.0.0

Unfortunately, I do not know how to specify the version without using some remote repository.不幸的是,我不知道如何在不使用远程存储库的情况下指定版本。 Note that I do not want to upload the library to JitPack, MavenCentral or similar.请注意,我不想将库上传到 JitPack、MavenCentral 或类似的。 All I want is to specify the version of a local AAR file.我想要的只是指定本地 AAR 文件的版本。

Update更新

The AAR file is a Zip-File with the following content: AAR 文件是一个 Zip 文件,内容如下:

/proguard.txt      
/R.txt             
/AndroidManifest.xml  
/public.txt        
/classes.jar       
/res/values/values.xml  

Note that the AndroidManifest.xml contains the version of the library.请注意, AndroidManifest.xml包含库的版本。 However, I assume that gradle always expects a pom file for the versioning information.但是,我假设 gradle 总是需要一个 pom 文件来获取版本信息。

I realized that gradle allows to specify a local Maven repository at a specific path:我意识到 gradle 允许在特定路径指定本地 Maven 存储库:

repositories {
   maven {
       url uri("${projectDir}/mylibdir")
   }
}

To use a local Maven repository, I need to build my library as a Maven artifact.要使用本地 Maven 存储库,我需要将我的库构建为 Maven 工件。 To create a Maven artifact, it suffices to create a POM file in the right subfolder.要创建 Maven 工件,只需在右侧子文件夹中创建 POM 文件即可。 The AAR file remains unchanged since Maven does not care about the artifact format. AAR 文件保持不变,因为 Maven 不关心工件格式。 Creating a Maven artifact can be automated with the maven-publish plugin, eg:可以使用maven-publish插件自动创建 Maven 工件,例如:

apply plugin: 'maven-publish'

publishing {
    publications {
        myRelease(MavenPublication) {
            groupId 'com.foo'
            artifactId 'my-artifact'
            version '1.0.0'
            artifact("$buildDir/outputs/aar/my-artifact.aar")
        }
    }
    repositories {
        maven {
            url "$buildDir/repo"
        }
    }
}

However, since I do not really need to use Maven, the simpler choice is to add the version to the AAR file name.但是,由于我真的不需要使用 Maven,因此更简单的选择是将版本添加到 AAR 文件名中。 I did this with the following snippet in the build.gradle of my library:我在我的库的build.gradle中使用以下代码段执行此操作:

android.libraryVariants.all { variant ->
        variant.outputs.all {
            outputFileName = "${archivesBaseName}-${variant.name}-${defaultConfig.versionName}.aar"
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM