首页 > 解决方案 > 无法使用显式ApiWarning() 显示警告或错误

问题描述

我打算对我用 kotlin 制作的库使用explicitApiWarning。问题是我在 build.gradle 文件中设置了以下脚本。不幸的是,它没有显示任何警告或错误。是不是应该把下面的标签放在特定的位置(比如在android{}内)?还是需要一些特定的设置?根据文件,这看起来很简单,但我无法让它工作。除此之外,我想知道是否可以同时设置explicitApiWarning 和explicitApi?一些提示或示例会有所帮助。谢谢你。

kotlin {
  explicitApiWarning()
}

https://kotlinlang.org/docs/reference/whatsnew14.html#explicit-api-mode-for-library-authors

    apply plugin: 'com.android.library'
    apply plugin: 'kotlin-android'

        
        android {
            compileSdkVersion 29
            buildToolsVersion "29.0.3"
        
            defaultConfig {
                minSdkVersion 24
                targetSdkVersion 29
                versionName "1.0"
        
                testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            }
        
      buildTypes {
        release {
          minifyEnabled false
        }
      }
    
      lintOptions {
        abortOnError true
      }
  }
        kotlin {
                explicitApiWarning()
        }

标签: androidkotlingradlebuild.gradle

解决方案


对于 android 库模块,设置显式 api 如下:

android {
  kotlinOptions {
    freeCompilerArgs += '-Xexplicit-api=warning'
  }
}

推荐阅读