手动设置目标

You can add targets when creating a project with the Project Wizard. If you need to add a target later, you can do this manually using target presets for supported platforms.

Learn more about additional settings for targets.

  1. kotlin {
  2. jvm() // Create a JVM target with the default name 'jvm'
  3. linuxX64() {
  4. /* Specify additional settings for the 'linux' target here */
  5. }
  6. }

Each target can have one or more compilations. In addition to default compilations for test and production purposes, you can create custom compilations.

区分一个平台的多个目标

You can have several targets for one platform in a multiplatform library. For example, these targets can provide the same API but use different libraries during runtime, such as testing frameworks and logging solutions. Dependencies on such a multiplatform library may fail to resolve because it isn’t clear which target to choose.

To solve this, mark the targets on both the library author and consumer sides with a custom attribute, which Gradle uses during dependency resolution.

For example, consider a testing library that supports both JUnit and TestNG in the two targets. The library author needs to add an attribute to both targets as follows:

  1. def testFrameworkAttribute = Attribute.of('com.example.testFramework', String)
  2. kotlin {
  3. jvm('junit') {
  4. attributes.attribute(testFrameworkAttribute, 'junit')
  5. }
  6. jvm('testng') {
  7. attributes.attribute(testFrameworkAttribute, 'testng')
  8. }
  9. }
  1. val testFrameworkAttribute = Attribute.of("com.example.testFramework", String::class.java)
  2. kotlin {
  3. jvm("junit") {
  4. attributes.attribute(testFrameworkAttribute, "junit")
  5. }
  6. jvm("testng") {
  7. attributes.attribute(testFrameworkAttribute, "testng")
  8. }
  9. }

The consumer has to add the attribute to a single target where the ambiguity arises.