最新版的Android Studio如何集成Arouter?gradle配置如何修改?

注释处打开就会报错

plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.kotlin.android)
    alias(libs.plugins.kotlin.compose)
}

android {
    namespace = "com.example.demo2"
    compileSdk = 35

    defaultConfig {
        applicationId = "com.example.demo2"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

//        javaCompileOptions.annotationProcessorOptions.arguments=projet.getName()
//        annotationProcessorOptions {
//            arguments = [AROUTER_MODULE_NAME: project.getName()]
//        }


    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }
    kotlinOptions {
        jvmTarget = "11"
    }
    buildFeatures {
        compose = true
    }
}

dependencies {

//    implementation (libs.arouter.api)
//    annotationProcessor(libs.alibaba.arouter.compiler)

    implementation (libs.utilcodex)
//    com.blankj:utilcodex:1.31.1
    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.activity.compose)
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.ui)
    implementation(libs.androidx.ui.graphics)
    implementation(libs.androidx.ui.tooling.preview)
    implementation(libs.androidx.material3)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(platform(libs.androidx.compose.bom))
    androidTestImplementation(libs.androidx.ui.test.junit4)
    debugImplementation(libs.androidx.ui.tooling)
    debugImplementation(libs.androidx.ui.test.manifest)
}
阅读 567
1 个回答

我帮你修改了一下,以下是完整代码

plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.kotlin.android)
    alias(libs.plugins.kotlin.compose)
}

android {
    namespace = "com.example.demo2"
    compileSdk = 35

    defaultConfig {
        applicationId = "com.example.demo2"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

        javaCompileOptions {
            annotationProcessorOptions {
                arguments["AROUTER_MODULE_NAME"] = project.name
            }
        }
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }
    kotlinOptions {
        jvmTarget = "11"
    }
    buildFeatures {
        compose = true
    }
}

dependencies {
    implementation(libs.arouter.api)
    annotationProcessor(libs.arouter.compiler)
    implementation(libs.utilcodex)
    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.activity.compose)
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.ui)
    implementation(libs.androidx.ui.graphics)
    implementation(libs.androidx.ui.tooling.preview)
    implementation(libs.androidx.material3)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(platform(libs.androidx.compose.bom))
    androidTestImplementation(libs.androidx.ui.test.junit4)
    debugImplementation(libs.androidx.ui.tooling)
    debugImplementation(libs.androidx.ui.test.manifest)
}

具体修复的地方如下:

1.取消注释依赖项:
将 implementation(libs.arouter.api) 和 annotationProcessor(libs.arouter.compiler) 取消注释,以确保 ARouter 的依赖项被正确添加。
2.注解处理器选项:
在 defaultConfig 块中,添加了 javaCompileOptions 和 annotationProcessorOptions,并设置了 arguments["AROUTER_MODULE_NAME"] = project.name。这确保了注解处理器能够正确识别模块名称。
修复的代码片段

defaultConfig {
    // 其他配置...
    javaCompileOptions {
        annotationProcessorOptions {
            arguments["AROUTER_MODULE_NAME"] = project.name
        }
    }
}

dependencies {
    implementation(libs.arouter.api)
    annotationProcessor(libs.arouter.compiler)
    // 其他依赖项...
}
推荐问题
宣传栏