Android.mk遇到user tag detected on new module - user tags are only supported on legacy modules

本文记录了一次在编译Android程序时遇到的错误,并详细描述了解决过程。问题出现在未正确设置LOCAL_MODULE_TAGS变量和变量赋值顺序不当。通过调整变量顺序解决了编译问题。

今天阳历生日,明天光棍节。继续光着吧。

前几天,编译Android程序时,出现了类似下面的一个错误。


build/core/base_rules.mk:74: *** Module name: xxxtest
build/core/base_rules.mk:75: *** Makefile location: packages/xxx/xxxtest
build/core/base_rules.mk:76: * 
build/core/base_rules.mk:77: * Each module must use a LOCAL_MODULE_TAGS in its
build/core/base_rules.mk:78: * Android.mk. Possible tags declared by a module:
build/core/base_rules.mk:79: * 
build/core/base_rules.mk:80: *     optional, debug, eng, tests, samples
build/core/base_rules.mk:81: * 
build/core/base_rules.mk:82: * If the module is expected to be in all builds
build/core/base_rules.mk:83: * of a product, then it should use the
build/core/base_rules.mk:84: * "optional" tag: 
build/core/base_rules.mk:85: * 
build/core/base_rules.mk:86: *    Add "LOCAL_MODULE_TAGS := optional" in the
build/core/base_rules.mk:87: *    Android.mk for the affected module, and add
build/core/base_rules.mk:88: *    the LOCAL_MODULE value for that component
build/core/base_rules.mk:89: *    into the PRODUCT_PACKAGES section of product
build/core/base_rules.mk:90: *    makefile(s) where it's necessary, if
build/core/base_rules.mk:91: *    appropriate.
build/core/base_rules.mk:92: * 
build/core/base_rules.mk:93: * If the component should be in EVERY build of ALL
build/core/base_rules.mk:94: * products, then add its LOCAL_MODULE value to the
build/core/base_rules.mk:95: * PRODUCT_PACKAGES section of
build/core/base_rules.mk:96: * build/target/product/core.mk
build/core/base_rules.mk:97: * 
build/core/base_rules.mk:98: *** user tag detected on new module - user tags are only supported on legacy modules.  Stop.

看这段解释,也上网搜答案,都说要把写上LOCAL_MODULE_TAGS := optional这么一行。但是明明我写了啊?看来不是这个问题。

后来才发现,原来是我Android.mk里,变量赋值的顺序的问题。 

本来在LOCAL_MODULE := xxxtest和include $(BUILD_EXECUTABLE)之间,我还给了好几个变量赋了值,有一个还是前面赋了一段,在这两者之间又赋了一段。我试着将LOCAL_MODULE := test_exe和include $(BUILD_EXECUTABLE)放到最后。问题突然就解决了。

因为太忙,也没去看看到底是什么问题。就去忙别的了。

如果谁遇到这问题,看是不是这个问题,咱们再探讨。


Configuration `:app:debugRuntimeClasspath` contains AndroidX dependencies, but the `android.useAndroidX` property is not enabled, which may cause runtime issues. Set `android.useAndroidX=true` in the `gradle.properties` file and retry. The following AndroidX dependencies are detected: :app:debugRuntimeClasspath -> androidx.core:core-ktx:1.12.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.annotation:annotation:1.6.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.annotation:annotation:1.6.0 -> androidx.annotation:annotation-jvm:1.6.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.core:core:1.12.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.annotation:annotation-experimental:1.3.0 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.appcompat:appcompat-resources:1.6.1 -> androidx.collection:collection:1.1.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.core:core:1.12.0 -> androidx.concurrent:concurrent-futures:1.1.0 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.appcompat:appcompat-resources:1.6.1 -> androidx.vectordrawable:vectordrawable-animated:1.1.0 -> androidx.interpolator:interpolator:1.0.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.lifecycle:lifecycle-runtime:2.6.1 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.savedstate:savedstate:1.2.1 -> androidx.arch.core:core-common:2.2.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.fragment:fragment:1.3.6 -> androidx.lifecycle:lifecycle-livedata-core:2.6.1 -> androidx.arch.core:core-runtime:2.2.0 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.savedstate:savedstate:1.2.1 -> androidx.lifecycle:lifecycle-common:2.6.1 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.fragment:fragment:1.3.6 -> androidx.loader:loader:1.0.0 -> androidx.lifecycle:lifecycle-livedata:2.6.1 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.fragment:fragment:1.3.6 -> androidx.lifecycle:lifecycle-livedata-core:2.6.1 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.emoji2:emoji2:1.2.0 -> androidx.lifecycle:lifecycle-process:2.6.1 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.emoji2:emoji2:1.2.0 -> androidx.startup:startup-runtime:1.1.1 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.activity:activity:1.8.0 -> androidx.tracing:tracing:1.0.0 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.lifecycle:lifecycle-viewmodel:2.6.1 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.fragment:fragment:1.3.6 -> androidx.lifecycle:lifecycle-viewmodel-savedstate:2.6.1 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.savedstate:savedstate:1.2.1 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.activity:activity:1.8.0 -> androidx.profileinstaller:profileinstaller:1.3.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.core:core:1.12.0 -> androidx.versionedparcelable:versionedparcelable:1.1.1 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.activity:activity:1.8.0 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.appcompat:appcompat-resources:1.6.1 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.vectordrawable:vectordrawable:1.1.0 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.appcompat:appcompat-resources:1.6.1 -> androidx.vectordrawable:vectordrawable-animated:1.1.0 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.cursoradapter:cursoradapter:1.0.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.drawerlayout:drawerlayout:1.1.1 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.drawerlayout:drawerlayout:1.1.1 -> androidx.customview:customview:1.1.0 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.emoji2:emoji2:1.2.0 :app:debugRuntimeClasspath -> androidx.appcompat:appcompat:1.6.1 -> androidx.emoji2:emoji2-views-helper:1.2.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.fragment:fragment:1.3.6 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.fragment:fragment:1.3.6 -> androidx.viewpager:viewpager:1.0.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.fragment:fragment:1.3.6 -> androidx.loader:loader:1.0.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.resourceinspection:resourceinspection-annotation:1.0.1 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.cardview:cardview:1.0.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.coordinatorlayout:coordinatorlayout:1.1.0 :app:debugRuntimeClasspath -> androidx.constraintlayout:constraintlayout:2.1.4 :app:debugRuntimeClasspath -> androidx.constraintlayout:constraintlayout:2.1.4 -> androidx.constraintlayout:constraintlayout-core:1.0.4 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.dynamicanimation:dynamicanimation:1.0.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.dynamicanimation:dynamicanimation:1.0.0 -> androidx.legacy:legacy-support-core-utils:1.0.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.dynamicanimation:dynamicanimation:1.0.0 -> androidx.legacy:legacy-support-core-utils:1.0.0 -> androidx.documentfile:documentfile:1.0.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.dynamicanimation:dynamicanimation:1.0.0 -> androidx.legacy:legacy-support-core-utils:1.0.0 -> androidx.localbroadcastmanager:localbroadcastmanager:1.0.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.dynamicanimation:dynamicanimation:1.0.0 -> androidx.legacy:legacy-support-core-utils:1.0.0 -> androidx.print:print:1.0.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.recyclerview:recyclerview:1.1.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.transition:transition:1.2.0 :app:debugRuntimeClasspath -> com.google.android.material:material:1.11.0 -> androidx.viewpager2:viewpager2:1.0.0 :app:debugRuntimeClasspath -> androidx.media:media:1.7.0 Enable AndroidX in project's Gradle properties More information about migrating to AndroidX... Affected Modules: app Ask Gemini
最新发布
12-10
<think>我们遇到了一个Gradle构建错误,提示项目使用了AndroidX依赖,但没有启用`android.useAndroidX`属性。错误信息中列出了许多传递依赖的AndroidX库。解决方案是在`gradle.properties`文件中设置`android.useAndroidX=true`。 ### 解决方案步骤: 1. **在项目根目录下的`gradle.properties`文件中添加AndroidX配置** 如果文件不存在,则创建该文件。添加以下两行: ```properties android.useAndroidX=true android.enableJetifier=true ``` - `android.useAndroidX=true`:启用AndroidX支持。 - `android.enableJetifier=true`:自动将第三方库中的支持库引用转换为AndroidX等价库(如果需要)。 2. **确保所有模块的`build.gradle`文件中使用AndroidX兼容的依赖项** 例如,使用`androidx.appcompat:appcompat`代替`com.android.support:appcompat-v7`。 3. **同步项目** 在Android Studio中,点击"Sync Project with Gradle Files"按钮,或者从命令行运行`./gradlew clean build`。 ### 示例代码: 在`gradle.properties`文件中添加: ```properties # gradle.properties android.useAndroidX=true android.enableJetifier=true ``` ### 解释: - **android.useAndroidX**:当设置为`true`时,Android插件会使用对应的AndroidX库,而不是旧的Support库。 - **android.enableJetifier**:当设置为`true`时,Gradle插件会自动重写二进制文件,将旧的支持库引用转换为AndroidX。这对于那些尚未迁移到AndroidX的第三方库特别有用。 ### 注意事项: - 如果项目中有模块仍然在使用旧的支持库(如`com.android.support`),则需要将这些依赖替换为对应的AndroidX库。否则,即使启用了Jetifier,也可能出现冲突。 - 在迁移到AndroidX后,代码中导入的包名也需要从`android.support`改为`androidx`。Android Studio提供了重构工具(Refactor > Migrate to AndroidX)来帮助完成这一过程。 §§相关问题§§: 1. 如何将现有的Android项目从Support Library迁移到AndroidX? 2. 什么是Jetifier?它是如何工作的? 3. 在迁移到AndroidX时,常见的兼容性问题有哪些?如何解决? 4. 如何检查项目中的第三方库是否已经支持AndroidX? </think> # 解决 AndroidX 依赖未启用问题 这个错误表明你的项目使用了 AndroidX 依赖,但没有正确启用 AndroidX 支持。以下是完整的解决方案: ## 解决方案步骤 ### 1. 在 `gradle.properties` 中启用 AndroidX 在**项目根目录**的 `gradle.properties` 文件中添加以下配置: ```properties # gradle.properties # 强制使用 AndroidX 库 android.useAndroidX=true # 自动迁移旧的支持库到 AndroidX android.enableJetifier=true # 解决可能的构建缓存问题 org.gradle.parallel=true org.gradle.caching=true ``` ### 2. 更新模块级 `build.gradle.kts` 文件 确保应用模块的 `build.gradle.kts` 使用正确的 AndroidX 依赖: ```kotlin // app/build.gradle.kts plugins { id("com.android.application") id("org.jetbrains.kotlin.android") } android { compileSdk = 34 // 确保是最新版本 defaultConfig { applicationId = "com.example.myapp" minSdk = 24 targetSdk = 34 versionCode = 1 versionName = "1.0" } buildTypes { getByName("release") { isMinifyEnabled = true proguardFiles( getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" ) } } compileOptions { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 } kotlinOptions { jvmTarget = "17" } } dependencies { // 核心 AndroidX 依赖 implementation("androidx.core:core-ktx:1.13.0") // 使用最新版本 // Material Design 组件 implementation("com.google.android.material:material:1.12.0") // 使用最新版本 // AppCompat 支持 implementation("androidx.appcompat:appcompat:1.7.0") // ConstraintLayout implementation("androidx.constraintlayout:constraintlayout:2.1.4") // 其他可能的依赖 implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.0") // 移除所有旧的支持库依赖 // implementation("com.android.support:appcompat-v7:28.0.0") // 删除此类依赖 } ``` ### 3. 清理并重建项目 执行以下命令清理构建缓存: ```bash ./gradlew clean ./gradlew --stop # 停止所有 Gradle 守护进程 ``` 然后重新构建项目: ```bash ./gradlew build ``` ## 额外注意事项 ### 如果问题仍然存在 1. **检查所有模块的依赖项**: ```bash ./gradlew :app:dependencies --configuration debugRuntimeClasspath ``` 这会显示所有依赖项及其传递依赖,确保没有旧的支持库残留 2. **强制使用 AndroidX 依赖版本**: 在应用模块的 `build.gradle.kts` 中添加: ```kotlin configurations.all { resolutionStrategy { force("androidx.core:core-ktx:1.13.0") force("com.google.android.material:material:1.12.0") // 为其他有问题的依赖添加强制版本 } } ``` ### 迁移旧代码 如果项目中仍有旧的支持库代码: 1.Android Studio 中使用迁移工具: **Refactor > Migrate to AndroidX** 2. 手动更新 XML 布局文件: ```xml <!-- 替换所有 android.support 命名空间 --> <!-- 旧: --> <android.support.constraint.ConstraintLayout> <!-- 新: --> <androidx.constraintlayout.widget.ConstraintLayout> ``` ## 完整项目结构示例 ``` my-project/ ├── gradle.properties # 包含 AndroidX 配置 ├── settings.gradle.kts ├── build.gradle.kts # 项目级构建文件 └── app/ ├── build.gradle.kts # 应用级构建文件 ├── src/ └── AndroidManifest.xml ```
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值