88661

Gradle: compileOnly and runtimeOnly

<h3>Question</h3>

I'd read the documentation but I'm not able to understand how to create a working example to understand better their differences.

And ofc I've created a playground project to check what happens when I use one or another.

app.gradle

dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$rootProject.kotlin_version" implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.core:core-ktx:1.2.0' compileOnly project(":compileonlylibrary") runtimeOnly project(":runtimeonlylibrary") }

MainActivity.kt

class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) FooCompile() // this crash in runtime BarRuntime() // this doesn't compiles obviously } } // FooCompile belongs to compileonlylibrary // BarRuntime belongs to runtimeonlylibrary

And that's it, I'm stuck here, I'm not able to create a proper example in order to improve my knowledge of Gradle configurations.

Could someone give me a hand? I can provide more details if needed.


<h3>Answer1:</h3>

CompileOnly dependencies are available while compiling but not when running them.

This is equivalent to the provided scope in maven.

It means that everyone who wants to execute it needs to supply a library with all classes of the CompileOnly library.

For example, you could create a library that uses the SLF4J API and you could set it to CompileOnly.

Anyone using the library needs to (explicitely) import some version of the SLF4J API in order to use it.

RuntimeOnly libraries are the opposite, they are available at runtime but not at compile-time.

For example, you don't need the concrete SLF4J logger(e.g. logback) at compile time (as you use the SLF4J classes in order to access it) but you need it at runtime as you want to use it.

Let's look at the following example:

You have a library that uses the SLF4J:

compileOnly org.slf4j:slf4j-api:1.7.30

and you could have a project using the library:

implementation project(":yourlibrary") implementation org.slf4j:slf4j-api:2.0.0-alpha1 runtimeOnly ch.qos.logback:logback:0.5

SLF4J detects the concrete logger at runtime, it does not need to know the logging library (like logback) at runtime. This is why you can use runtimeOnly for the concrete logger.

<hr />

Note that compileOnly is broadly used with Jakarta EE as lots of dependencies are provided by the JEE container/application server as shown in the blog the OP found.

来源:https://stackoverflow.com/questions/61696863/gradle-compileonly-and-runtimeonly

Recommend

  • AWS Code Commit Simple Notification Service: is there a way to get better formatted notification ema
  • Group list by month
  • Implicit inheritance working in Java
  • How to correctly get Unicode text input from QPlainTextEdit? [duplicate]
  • What's wrong with following assembly code?
  • Drools 6 - Accessing the LHS of an ObjectInsertedEvent
  • seaborn heatmap get array of color codes values
  • Find object in Firebase by one value
  • R independent columns in matrix
  • Following query giving DB CPU utilization impact, can I reduce data in table will help me to reduce
  • Creating msqrd or snapchat filters using javascript
  • call child function from parent in reactjs
  • How multiple executors are managed on the worker nodes with a Spark standalone cluster?
  • Problems with connecting bluetooth SPP in android?
  • How to get spesific error instead of Internal Service Error
  • Animated line plot with parallel processing
  • Oracle outer join with filter condition on the second table
  • How to get current TFS license type?
  • How to add a share button to the text selection bar for an EditText?
  • How do I handle a WebSocket close from the client in Yaws?
  • Get All the instance subclass of trait using Google Guice
  • Select multiple fields with single group by in django
  • How can I progmatically click a link in a Winform Web Browser control?
  • Having a custom repository with DBAL connection in Symfony 2 / Doctrine 2?
  • AJAX calls - where does logic go?
  • hadoop jar command points to local filesystem
  • Interactive labeling of images in jupyter notebook
  • Why processBuilder in java hangs after 5 mins?
  • How do I link when building with llvm libraries?
  • List using with references, changes behavior when used as a member
  • Display variable as html in Google App Script?
  • Unsupported ciphersuite TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
  • PHP: Need to close STDIN in order to read STDOUT?
  • xpath assertion failure with dynamic xpath
  • Floating parent div grows to hypothetical width of floating child div
  • Time Complexity of Fibonacci Algorithm [duplicate]