我正在开发一组共享本地库的 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) 相同,而不是“未指定”。
如何正确依赖我的共享本地库?
我所做的我认为接近解决方案:
我尝试更改库 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
}
(深受这个项目的启发)
然后在我的依赖项子句中:
if (useRemote()) {
api "com.my_company.project:common:${project.ext.libraryVersion}"
} else {
api project(path: ':common')
}
这似乎在内部有效,但当我在外部使用它时,我仍然收到以下错误:
Failed to resolve: package_repository:common:unspecified