Android Studio 编译项目时,出现类似以下报错,依赖库中没有命名空间
A problem occurred configuring project ':app_group_directory'.
[ ] > Could not create an instance of type com.android.build.api.variant.impl.LibraryVariantBuilderImpl.
[ ] > Namespace not specified. Specify a namespace in the module's build file: E:\.pub-cache\hosted\pub.flutter-io.cn\app_group_directory-2.0.0\android\build.gradle. See https://d.android.com/r/tools/upgrade-assistant/set-namespace for information about setting the namespace.
可以在主工程,build.gradle中添加以下代码,再编译即可
// 配置JVM工具链以解决兼容性问题
subprojects {
afterEvaluate { project ->
if (project.plugins.hasPlugin('com.android.library') || project.plugins.hasPlugin('com.android.application')) {
project.android {
compileSdk 35
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
}
project.tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
kotlinOptions {
jvmTarget = '17'
}
}
}
}
}
subprojects {
afterEvaluate { project ->
if (project.plugins.hasPlugin('com.android.library') || project.plugins.hasPlugin('com.android.application')) {
println "project: ${project.name} Namespace get: ${project.android.namespace}"
def packageName = project.android.namespace ?: project.android.defaultConfig.applicationId ?: project.android.sourceSets.main.manifest.srcFile.text.find(/package="([^"]*)"/) ?: project.group
project.android.namespace = packageName
println "Namespace set to: ${packageName} for project: ${project.name}"
def manifestFile = project.android.sourceSets.main.manifest.srcFile
if (manifestFile.exists()) {
def manifestText = manifestFile.text
if (manifestText.contains('package=')) {
manifestText = manifestText.replaceAll(/package="[^"]*"/, "")
manifestFile.text = manifestText
println "Package attribute removed in AndroidManifest.xml for project: ${project.name}"
} else {
println "No package attribute found in AndroidManifest.xml for project: ${project.name}"
}
} else {
println "AndroidManifest.xml not found for project: ${project.name}"
}
}
}
}
在有的项目中,group会带有package字段,修复如下
subprojects {
afterEvaluate { project ->
if (project.plugins.hasPlugin('com.android.library') || project.plugins.hasPlugin('com.android.application')) {
println "project: ${project.name} Namespace get: ${project.android.namespace}"
def packageName = project.android.namespace ?: project.android.defaultConfig.applicationId ?: project.android.sourceSets.main.manifest.srcFile.text.find(/package="([^"]*)"/) ?: project.group
println "packageName: ${packageName}"
project.android.namespace = packageName.replaceAll('_', '').replace('package="', '').replace('"', '')
println "Namespace set to: ${packageName} for project: ${project.name}"
def manifestFile = project.android.sourceSets.main.manifest.srcFile
if (manifestFile.exists()) {
def manifestText = manifestFile.text
if (manifestText.contains('package=')) {
manifestText = manifestText.replaceAll(/package="[^"]*"/, "")
manifestFile.text = manifestText
println "Package attribute removed in AndroidManifest.xml for project: ${project.name}"
} else {
println "No package attribute found in AndroidManifest.xml for project: ${project.name}"
}
} else {
println "AndroidManifest.xml not found for project: ${project.name}"
}
}
}
}
3534

被折叠的 条评论
为什么被折叠?



