Setting JAR priority in Android Application plugin

I have a user defined JAR(used as compileOnly in dependencies) which has some duplicate name same as android.jar.
I want to give my jar classpath priority over the android.jar.

Please guide the options i can explore to achieve this.

Sounds like something you definitely should not do.

But if you are sure you know what you are doing, you might need to use a compiler argument provider to construct your own classpath argument with your intended order.

Thank you for the reply.

To provide context, earlier i was using approach mentioned in this discussion.

Prepend my jar using -xbootclasspath. But prepending option is removed after JAVA_1_8.

So facing class priority issue after my source and target compatibility is upgraded to JAVA17 from JAVA1-8.

Could you please share more insight and examples on argument provider. TIA

Something like

tasks.compileJava {
    options.compilerArgumentProviders.add {
        listOf("-cp", "the classpath in the order you need")
    }
}

Besides that you can probably just replace /p by /a as you are not replacing a JVM stdlib classs it shouldn’t matter whether you prepend or append to the bootstrap classpath and appending is still there.

Hi I tried but was not successful.

I create a Demo project.
add framework.jar to app/libs and add compileOnly

dependencies {
    compileOnly files('libs/framework.jar')
}

I tried modifying top level build.gradle

//def fwkJar = file('app/libs/framework.jar')
allprojects {
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile).tap {
            configureEach {
                options.compilerArgumentProviders.add(new CommandLineArgumentProvider() {
                    @Override
                    Iterable<String> asArguments() {
                        return ["-cp", "app/libs/framework.jar"]
                        // or use absolute path
                        // return ["-cp", "$fwkJar "]
                    }
                })
            }
        }
    }
}

or app/build.gradle

gradle.projectsEvaluated {
    tasks.withType(JavaCompile).tap {
        configureEach {
            options.compilerArgumentProviders.add(new CommandLineArgumentProvider() {
                @Override
                Iterable<String> asArguments() {
                    return ["-cp", "libs/framework.jar"]
                }
            })
        }
    }
}

but it didn’t work.Could you let me know if there’s anything wrong?
Thank you very much

Besides that you should not use allprojects or projectsEvaluated, and you probably should use the version with absolute path, it should be fine. You can easily check with --info or --debug (don’t remember which is sufficient) to see the actual arguments used.

Of course that way you will only have the framework.jar on the classpath and nothing else, so like you showed it, it will only work if you have not a single other dependency. You need to provide the full classpath.

As long as you are aware that his is extremely dirty and very bad for build performance, reliability, and correctness.
At best this snippet makes your build unnecessary slow, at worst you make it incorrect.