Jon*_*Jon 8 android gradle classnotfoundexception android-library android-gradle-plugin
我有一个自定义视图库,可以自己编译和运行(通过在库项目中为测试目的而创建的另一个活动).然而,当我构建库,然后将aar导入另一个项目(打开模块设置 - >新模块 - >现有的aar ..)我得到一个运行时ClassNotFoundException - 异常是关于库的唯一gradle依赖项使用.为什么会这样?
库gradle文件:
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.googlecode.libphonenumber:libphonenumber:7.2.1'
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.i18n.phonenumbers.PhoneNumberUtil" on path: DexPathList[[zip file..
Run Code Online (Sandbox Code Playgroud)
aar 依赖项与 maven/ivy 依赖项不同,因为 pom 或 xml 文件中没有与其捆绑的传递依赖项。当您添加 aar 依赖项时,gradle 无法知道要获取哪些传递依赖项。
Android 世界中的一种常见做法似乎是,使用 aar.properties 文件向您的应用程序显式添加传递依赖项。这可能会变得很麻烦,并且有点违背了依赖管理系统的意义。
有几种解决方法:
有一个第 3 方 gradle 插件,允许您将 aar 文件以及有效的 pom 文件发布到本地 Maven 存储库。
您可以使用标准的maven-publish 插件将 aar 发布到 Maven 存储库,但您必须自己组装 pom 依赖项。例如:
publications {
maven(MavenPublication) {
groupId 'com.example' //You can either define these here or get them from project conf elsewhere
artifactId 'example'
version '0.0.1-SNAPSHOT'
artifact "$buildDir/outputs/aar/app-release.aar" //aar artifact you want to publish
//generate pom nodes for dependencies
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
configurations.compile.allDependencies.each { dependency ->
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', dependency.group)
dependencyNode.appendNode('artifactId', dependency.name)
dependencyNode.appendNode('version', dependency.version)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,一旦 aar+pom 在 Maven 存储库中可用,您就可以在您的应用程序中使用它,如下所示:
compile ('com.example:example:0.0.1-SNAPSHOT@aar'){transitive=true}
Run Code Online (Sandbox Code Playgroud)
(我不完全确定如果您将依赖项添加为 ,传递性如何工作compile project(:mylib)。我将很快更新此案例的答案)
| 归档时间: |
|
| 查看次数: |
1905 次 |
| 最近记录: |