Android出现错误:未指定命名空间

huangapple go评论75阅读模式
英文:

Android getting error: Namespace not specified

问题

我决定使用最新版本的Gradle,在更新AndroidManifest.xmlbuild.gradle之后,我遇到了这个错误,而且无法解决它。

错误信息:

未指定命名空间。请在模块的 build.gradle 文件中指定一个命名空间,如下所示:

android {
    namespace 'com.example.namespace'
}

如果在源 AndroidManifest.xml 中指定了 package 属性,则可以使用 AGP 升级助手自动将其迁移到 build.gradle 文件中的命名空间值;请参阅 https://developer.android.com/studio/build/agp-upgrade-assistant 了解更多信息。

application build.gradle

dependencies {
    classpath 'com.android.tools.build:gradle:8.0.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}

gradle.wrapper.properties

distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application
        android:label="xxxxx"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

build.gradle

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
}

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

android {
    namespace "com.example.xxxxx"
    compileSdkVersion flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        applicationId "com.example.xxxxx"
        minSdkVersion flutter.minSdkVersion
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {}

希望这个翻译能够帮助您解决问题。如果需要更多帮助,请随时提问。

英文:

I decided to use latest version of Gradle and after updating AndroidManifest.xml and build.gradle I get this error and I couldn't solve it

Error:

Namespace not specified. Please specify a namespace in the module&#39;s build.gradle file like so:

android {
    namespace &#39;com.example.namespace&#39;
}

If the package attribute is specified in the source AndroidManifest.xml, it can be migrated automatically to the namespace value in the build.gradle file using the AGP Upgrade Assistant; please refer to https://developer.android.com/studio/build/agp-upgrade-assistant for more information.

application build.gradle

dependencies {
    classpath &#39;com.android.tools.build:gradle:8.0.1&#39;
    classpath &quot;org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version&quot;
}

gradle.wrapper.properties:

distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip

AndroidManifest.xml

&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
    &lt;application
        android:label=&quot;xxxxx&quot;
        android:name=&quot;${applicationName}&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;&gt;
        &lt;activity
            android:name=&quot;.MainActivity&quot;
            android:exported=&quot;true&quot;
            android:launchMode=&quot;singleTop&quot;
            android:theme=&quot;@style/LaunchTheme&quot;
            android:configChanges=&quot;orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode&quot;
            android:hardwareAccelerated=&quot;true&quot;
            android:windowSoftInputMode=&quot;adjustResize&quot;&gt;
            
            &lt;meta-data
              android:name=&quot;io.flutter.embedding.android.NormalTheme&quot;
              android:resource=&quot;@style/NormalTheme&quot;
              /&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot;/&gt;
                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot;/&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
        
        &lt;meta-data
            android:name=&quot;flutterEmbedding&quot;
            android:value=&quot;2&quot; /&gt;
    &lt;/application&gt;
&lt;/manifest&gt;

and build.gradle

plugins {
    id &quot;com.android.application&quot;
    id &quot;kotlin-android&quot;
    id &quot;dev.flutter.flutter-gradle-plugin&quot;
}

def localProperties = new Properties()
def localPropertiesFile = rootProject.file(&#39;local.properties&#39;)
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader(&#39;UTF-8&#39;) { reader -&gt;
        localProperties.load(reader)
    }
}

def flutterVersionCode = localProperties.getProperty(&#39;flutter.versionCode&#39;)
if (flutterVersionCode == null) {
    flutterVersionCode = &#39;1&#39;
}

def flutterVersionName = localProperties.getProperty(&#39;flutter.versionName&#39;)
if (flutterVersionName == null) {
    flutterVersionName = &#39;1.0&#39;
}

android {
    namespace &quot;com.example.xxxxx&quot;
    compileSdkVersion flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = &#39;1.8&#39;
    }

    sourceSets {
        main.java.srcDirs += &#39;src/main/kotlin&#39;
    }

    defaultConfig {
        applicationId &quot;com.example.xxxxx&quot;
        minSdkVersion flutter.minSdkVersion
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source &#39;../..&#39;
}

dependencies {}

答案1

得分: 5

我相信namespace问题是因为Gradle 8.x目前仅支持Java JDK 17,而不支持版本18到20。

Gradle用户指南提到了这些不兼容性:
https://docs.gradle.org/current/userguide/compatibility.html#java

我目前使用的是Java 20,但在AndroidManifest.xmlbuild.gradle中修改命名空间对我并没有解决问题。但是通过降级到Gradle 7.4.2,我成功地使Flutter和Android Studio能够构建。

所以我的建议是降级Gradle版本,或者降级Java版本。

英文:

I believe the namespace issue occurs because Gradle 8.x is currently supported by Java JDK 17, and not by versions 18 through 20.

The Gradle user guide hints at the incompatibilities:
https://docs.gradle.org/current/userguide/compatibility.html#java

I'm currently on Java 20, and motifying the AndroidManifest.xml and build.gradle with the namespace did not fix the issue for me either, but by downgrading to Gradle 7.4.2, I was able to get Flutter and Android Studio to build.

So my advice is to downgrade your version of Gradle, or downgrade your version of Java.

答案2

得分: 4

我已将以下内容添加到app/build.gradle文件中:

android{
    namespace 'com.example.application'
    ....
}

请确保将其添加到位于应用程序文件夹中而不是项目文件夹中的build.gradle文件中。

英文:

I have added the following lines to app/build.gradle

android{
    namespace &#39;com.example.application
    ....
}

be sure to add it to the build.gradle file found in the app folder no in the project folder.

答案3

得分: 0

在我的Flutter项目中,在升级到Giraffe后,我遇到了相同的错误。基本上从Giraffe开始,gradle只查看构建文件中的namespace属性,而不是清单中的package属性。

因此,我已经迁移了我的应用级build.gradle文件和manifest。但是它显示某些包缺少命名空间。

所以我在工作室中单独打开了android文件夹,尝试了AGP升级,发现一些插件gradle文件缺少了命名空间,一旦我将命名空间属性添加到它们所有的构建文件中,一切都正常工作了。

英文:

In my Flutter project, I got the same error after the studio upgrade to Giraffe.
Basically from Giraffe onwards gradle only looks at the build file
namespace property than the manifest package property.

So I have migrated my app-level build.gradle file and manifest. But it was showing some package was missing the namespace.

So I opened the android folder separately in the studio and tried the AGP upgrade and found out some plugin gradle files were missing the namespace, once I added the namespace property to all of their build files everything worked.

答案4

得分: -5

Instead of using a namespace within your build.gradle file, use the applicationId as shown below,

defaultConfig {
applicationId "com.myApp.example"
minSdkVersion 16
targetSdkVersion 30
versionCode 33
versionName "0.3.4"

If this doesn't work for you, please make a new project setup and attempt to build your app using the new project setup.

file >> new project >> project folder path

英文:

Instead of using a namespace within your build.gradle file, use the applicationId as shown below,

 defaultConfig {
        applicationId &quot;com.myApp.example&quot;
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 33
        versionName &quot;0.3.4&quot;

If this doesn't work for you, please make a new project setup and attempt to build your app using the new project setup.

file &gt;&gt; new project &gt;&gt; project folder path

huangapple
  • 本文由 发表于 2023年5月21日 23:36:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定