我可以看到这是几个月前添加到AGP的。但他们没有提供更多关于此问题的信息。
所以:
1.这是什么问题?有更多信息吗?我找不到任何错误报告。
2.我该如何解决这个问题?这是否意味着我不能构建一个依赖于其他本地.aar文件的.aar文件?如果这个本地aar文件改为托管在Maven Central或另一个远程repo上,会有什么区别?为什么会有区别?
最近我也遇到了同样的问题,解决方法是将库从libs/
目录中删除,然后使用File -> New -> New Module -> Import .JAR/.AAR Package
导入它,最后在库模块的build.gradle
文件中引用它:
dependencies {
implementation project(":imported_aar_module")
}
如果您正在使用更新版本的Android Studio(4.0.0+),则该选项不可用。相反,您必须手动执行它。
build.gradle
文件中:configurations.maybeCreate("default")
artifacts.add("default", file('[nameOfTheAar].aar'))
aar
文件放入这个新文件夹。与 build.gradle
文件放在同一级目录下。settings.gradle
文件中:include(":pathToTheCreatedDirectory")
aar
的库中:implementation project(":pathToTheCreatedDirectory", configuration = "default")
implementation project(":pathToTheCreatedDirectory", configuration = "default")
引起错误 Could not set unknown property 'configuration'...
。____________________ implementation project(":pathToTheCreatedDirectory")
可行。 - Eugene Babich我想在这个问题上呼吁一下@StefMa的评论,它非常简单,为我解决了这个问题,但是它淹没在许多其他评论中,很容易被忽视。
这个主题上的“正确”答案不再适用,因为在那个答案中提到的在Android Studio中导入AARs已经不可能了。但是,StefMa的评论中提到的链接到这个GitHub帖子的解决方案可以,并且它完美地工作。
长话短说-将您的AAR放入单独的模块中。
不需要费力地创建lib目录,只需按照以下说明操作:
在项目的根目录中创建一个新目录。下面的图像显示了其中两个- spotify-app-remote
和spotify-auth
,但一个就足够了。在里面,把你的AAR放进去,并创建一个新的build.gradle
文件。
在build.gradle
文件中添加以下内容,将aar文件名替换为您的AAR文件名-
configurations.maybeCreate("default")
artifacts.add("default", file('spotify-app-remote-release-0.7.1.aar'))
请将以下内容添加到您的settings.gradle
文件中,将创建的目录名称替换为相应的名称。include ':spotify-app-remote'
将你的新模块包含在你想要使用AAR的模块中。例如,如果你想在app
模块中使用它,在app的build.gradle
中打开并添加:
api project(':spotify-app-remote')
在您的dependencies { }
块中,显然需要将spotify-app-remote替换为您的模块的名称。
artifacts.add
子句将多个AAR添加到单个library module
中? - Tash Pemhiwaaar
文件)的Android库时,如果将aar文件作为项目的依赖项包含进去,您将会收到以下错误消息:
如上所述,当您构建一个Android库项目时,它所依赖的任何aar都不会被打包。如果您在AGP(Android Gradle Plugin)4之前以这种方式构建,则可能会注意到您必须将aar依赖项包含在使用您的库的项目中。在构建AAR时,不支持直接本地.aar文件依赖项。由于任何本地.aar文件依赖项的类和Android资源都不会打包在生成的AAR中,因此生成的AAR将无法正常工作。即使是之前版本的Android Gradle插件也会在这种情况下生成错误的AAR(尽管没有抛出此错误)。
compileOnly
来编译您的Android库项目。有关何时使用compileOnly
的更多信息,请参见this。app
build.gradle
文件中:compileOnly files('libs/some-library.aar')
implementation 'mylibrarygroup:mylibraryartifact:version-x.y.z@aar'
根据我的经验,在Gradle插件版本为4.2.2+且Gradle版本为7.1+时,如@Luis的答案中所示,'compileOnly'可以正常工作。
compileOnly files('libs/your_library_name.aar')
当Gradle版本较低时,它无法正常工作。
我也有同样的问题,即想将一个库依赖项封装到一个模块中。然而,这个库依赖项有一堆aars,为每个aars创建单独的模块只会让事情更加混乱,在新版studio中甚至找不到这个选项。
为了解决这个问题,我将aars发布到了本地仓库,然后再开始构建过程。
因此,我的封装模块的build.gradle文件看起来像这样:
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'maven-publish'
}
//..
parent.allprojects { // for some reason simply repositories didn't work
repositories {
mavenLocal()
}
}
//...
publishing {
publications {
barOne(MavenPublication) {
groupId 'foo-aar-dependency'
artifactId 'bar1'
version '1.0'
artifact("$libsDirName/bar1.aar")
}
barTwo(MavenPublication) {
groupId 'foo-aar-dependency'
artifactId 'bar2'
version '1.0'
artifact("$libsDirName/bar2.aar")
}
barThree(MavenPublication) {
groupId 'foo-aar-dependency'
artifactId 'bar3'
version '1.0'
artifact("$libsDirName/bar3.aar")
}
// and so on...
}
}
// add the publication before the build even starts
// used ./gradlew mymodule:assemble --dry-run to find where to put it
afterEvaluate {
tasks.clean.dependsOn("publishToMavenLocal")
tasks.preBuild.dependsOn("publishToMavenLocal")
}
dependencies {
implementation "foo-aar-dependency:bar1:1.0"
implementation "foo-aar-dependency:bar2:1.0"
implementation "foo-aar-dependency:bar3:1.0"
// and so on
// also I had to make sure to add the aar's transitive dependencies as implementation below
}
注意:第一次同步时,依赖项未找到,但只要调用任何 clean/assemble,依赖项就会先被发布,以满足所需的运行。
注意2:大部分内容可以移动到一个单独的文件中,以避免混乱你的 build.gradle。
注意3:如果你真的想将你的模块作为库发布,这个解决方案不适合你。
注意4:如果你运行 clean 然后再运行下一个任务,这也适用于 CI。
当使用这段代码时,会出现相同的错误。
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation fileTree(include: ['*.aar'], dir: 'libs')
请用以下代码替换您的代码。
打开顶层的 'build.gradle' 文件并添加。
repositories {
flatDir {
dirs('/src/main/libs')
}
}
接着,在你项目的build.gradle文件中添加以下内容。
api(name:'aar_module_name', ext:'aar')
我遇到了类似的问题:
任务:在另一个SDK中添加.aar
SDK
解决方案:
We have to create new Android Library Module inside our library (right click on our library name -> module -> Android library )
Delete all files inside it
Insert our .arr
inside this module
Create build.gradle
file inside module and put there:
configurations.maybeCreate("default")
artifacts.add("default", file('your_arr_name.aar'))
Add to your library build.gradle
inside dependencies block next:
implementation project(':your_library:your_arr_module')
Now rebuild project and everything should work fine
settings.gradle
被附加了 include ':your_library:your_arr_module'
,如果您要链接React Native库,则包含它非常重要。[我在react-native-spotify-remote
库上遇到了问题,在发布Gradle构建时失败,所以我不得不对其进行分支,并应用此修复] - Danielimplementation fileTree(dir: 'libs', include: ['*.jar','*.aar']) //Load all aars and jars from libs folder
我的补丁将该行更改为:
implementation(name: 'the-name-of-the-aar', ext: 'aar')
flatDir { dirs "$rootDir/../node_modules/the-third-party-dependency/android/src/main/libs" }
AAR文件存放位置
已测试通过reactnative >= 0.69.x
对于那些想要将其作为常规依赖项(或Gradle版本目录中的一项)使用的人:
spotifyAppRemote
的文件夹,与app
文件夹在同一级别下spotifyAppRemote
文件夹的根目录中添加所需的.aar
文件spotifyAppRemote
文件夹的根目录中创建一个名为settings.gradle.kts
的文件。此文件将为空,只需存在于复合构建中。参见:文档spotifyAppRemote
文件夹的根目录中创建一个名为build.gradle.kts
的文件:plugins {
base //allows IDE clean to trigger clean on this module too
}
configurations.maybeCreate("default")
artifacts.add("default", file("spotify-app-remote-release-0.7.2.aar"))
//Change group to whatever you want. Here I'm using the package from the aar that I'm importing from
group = "com.spotify.android"
version = "0.7.2"
settings.gradle.kts
的根目录下(!!即项目根目录,而不是上面创建的空文件夹)。/* Optional - automatically sync gradle files for included build */
rootDir.run {
listOf(
"gradle.properties",
"gradlew.bat",
"gradlew",
"gradle/wrapper/gradle-wrapper.jar",
"gradle/wrapper/gradle-wrapper.properties"
).map { path ->
resolve(path)
.copyTo(
target = rootDir.resolve("spotifyAppRemote").resolve(path),
overwrite = true
)
}
}
settings.gradle.kts
文件中。与上面添加片段的地方相同:rootProject.name = "Your project name"
include(":app")
includeBuild("spotifyAppRemote")
dependencies {
// group:moduleName:version
implementation("com.spotify.android:spotifyAppRemote:0.7.2")
}