【问题标题】:Common local dependency in Android libraryAndroid库中常见的本地依赖
【发布时间】:2022-01-02 00:08:18
【问题描述】:

我正在开发一组共享一个公共库的 android 库。

基本上我有一个库 A 和一个库 B,它们都依赖于一个公共库 C

   A   B
   ^   ^
   |   |
    \./
     C

库 A、B、C 都在同一个 Android 项目中。

最初库 A 和 B 都可以有以下它们的 build.gradle 文件:

dependencies {
    api project(path: ':common')
    ...
}

但这仅在“内部”构建库 A 和 B 时有效。如果我将它们部署为包并在外部项目中使用它们,我会收到此错误:

Could not find my_package_directory:library_common:unspecified.
     Searched in the following locations:
       - https://repo.maven.apache.org/maven2/my_package_directory/common/unspecified/common-unspecified.pom
       ...
     Required by:
         project :app > com.my_company.project:library_a:1.0.0

我想这是有道理的,因为库 C 的发布版本号与 A 和 B (1.0.0) 相同,而不是“未指定”。

如何正确依赖我的公共库?

【问题讨论】:

    标签: android build.gradle aar


    【解决方案1】:

    我认为接近解决方案的做法:

    我尝试将库 A 和 B 更改为依赖于特定版本的库公共(最新),但在处理我的库时我仍然想使用公共库的本地版本。

    为了实现这一点,我编写了一些代码来确定我的工作目录是否包含任何更改,或者我的 Git 存储库的头部是否不是标记版本。

    import org.eclipse.jgit.lib.ObjectId
    import org.eclipse.jgit.lib.Repository
    import org.eclipse.jgit.storage.file.FileRepositoryBuilder
    import org.eclipse.jgit.errors.RepositoryNotFoundException
    
    import org.eclipse.jgit.revwalk.RevCommit
    import org.eclipse.jgit.revwalk.RevObject
    import org.eclipse.jgit.revwalk.RevTag
    import org.eclipse.jgit.revwalk.RevWalk
    import org.eclipse.jgit.lib.Constants
    import org.eclipse.jgit.api.Git
    
    boolean useRemote() {
        Repository repo
        try {
            repo = new FileRepositoryBuilder()
                    .readEnvironment()
                    .findGitDir(project.projectDir)
                    .build()
        } catch (IllegalArgumentException | RepositoryNotFoundException ignore) {
            return false
        }
    
        def git = Git.wrap(repo)
        def head = repo.findRef(Constants.HEAD).getTarget()
    
        // No commits?
        if (!head.getObjectId()) return false
    
        // Check to see if uncommitted files exist
        if (git.status().call().hasUncommittedChanges() || !git.status().call().getUntracked().isEmpty())
            return false
    
        RevWalk walk = new RevWalk(repo)
        List<ObjectId> tags = git.tagList().call().findResults { ref ->
            RevObject obj = walk.parseAny(ref.getObjectId())
            ObjectId id = null
            if (obj instanceof RevTag) {
                // Annotated tag
                id = obj.getObject().getId()
            } else if (obj instanceof RevCommit) {
                // Lightweight tag
                id = obj.getId()
            }
            id
        }
        walk.close()
    
        // Check to see if head is tagged
        if (!tags.findAll { (it == head.getObjectId()) })
            return false
        return true
    }
    

    (非常受this project的启发)

    然后在我的依赖子句中:

    if (useRemote()) {
        api "com.my_company.project:common:${project.ext.libraryVersion}"
    } else {
        api project(path: ':common')
    }
    

    这似乎在内部工作,但是当我在外部使用它时,我仍然收到以下错误:

    Failed to resolve: package_repository:common:unspecified
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      • 1970-01-01
      • 1970-01-01
      • 2018-02-20
      • 2023-03-04
      • 2015-05-07
      • 2021-09-18
      相关资源
      最近更新 更多