我想开发一个“独立”库,该库可以在其他库中重用。
以下是说明我要构建的用例的图像:
说明
我已经使用在线依赖项(例如OkHttp,Gson等)开发了 CommonLib 。这部分是通过Android Studio项目生成的aar文件完成的(效果很好,谢谢;))
现在,我想以最透明的方式在其他库中使用此 CommonLib 。例如,我不想在 Lib 1 项目或 My App 项目中指定okHttp依赖项。
CommonLib
简而言之,该库是由Kotlin开发的,并且负责我想轻松重用的中央过程。我向您展示了我的gradle文件的 dependencies 块:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion"
implementation "com.google.code.gson:gson:$gsonVersion"
implementation "com.android.support:support-annotations:$supportLibVersion"
implementation "com.squareup.okhttp3:okhttp:$okhttpVersion"
implementation "com.squareup.okhttp3:logging-interceptor:$okhttpVersion"
testImplementation "org.robolectric:robolectric:$robolectricVersion"
testImplementation "junit:junit:$junitVersion"
}
解决方案1(我认为是最好的):私人回购
我已经在 CommonLib 项目中配置了Maven插件,以将我的aar文件发布到私有存储库中(我使用的是Nexus,但这不是问题)。这是我的gradle文件:
apply plugin: 'maven'
uploadArchives {
repositories {
mavenDeployer {
repository(url: "$url") {
authentication(userName: "$user", password: "$password")
}
pom.groupId = "com.test.common"
pom.artifactId = "CommonLib"
pom.version = VERSION_NAME + "." + VERSION_CODE
}
}
}
上传过程运行良好,我可以在我的仓库中找到 CommonLib 工件和关联的 pom 文件,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.common</groupId>
<artifactId>CommonLib</artifactId>
<version>0.3.1</version>
<packaging>aar</packaging>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-android-extensions-runtime</artifactId>
<version>1.3.31</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>1.3.31</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.android.support</groupId>
<artifactId>appcompat-v7</artifactId>
<version>28.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.12.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>3.11.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
我的问题是:当我在 Lib 1 项目中添加此工件依赖项时,由于NoClassDefFoundError
和I'm not the only one,编译过程失败了。
更准确地说,我为您提供 Lib 1 项目的配置。
allprojects {
repositories {
google()
jcenter()
maven {
url "$url"
}
}
}
和
dependencies {
[...]
implementation 'com.test.common:CommonLib:0.3.1'
}
构建错误消息为
未解决的参考:程序包名称
packagename 是 CommonLib
中存在的软件包解决方案2(还不错):Android Studio子模块
现在,在 Lib 1 项目中,我将 CommonLib 项目添加为Android Studio模块。导入可以,构建/编译时间可以。但是(遗憾的是,有一个“ but”),当我生成最终的aar文件时, CommonLib 中的方法和对象未嵌入,因此我无法使用它:(
解决方案3(最丑):复制/粘贴代码
最后,我将 CommonLib 的源代码复制/粘贴到 Lib 1 项目中。哈哈 !有用 ! [在这里插入悲伤/羞耻/泪水笑脸]
最佳解决方案
好吧,我不知道,我需要你的帮助,伙计们...我该怎么办?