在注释中使用@hide隐藏方法/类

时间:2014-07-17 08:51:34

标签: java android annotations documentation hide

我正在开发一个用于android的库(jar),并且遇到了我希望我的类或方法只能在我的库中访问,但不能在我的库之外访问的情况。使用no修饰符将使该类在该包中可访问,但我遇到的情况是,如果没有public修饰符,则不能使用此类,因为使用no修饰符将使其在我不想要的其他包中无法访问。例如, 我有一堂课说,

public class Globals {

    public static String thisDeviceAddress;
    public static String thisDeviceIP;
    public static String thisDeviceName = "";

}

这个课程无处不在。问题是我希望它可以在我正在开发的库中访问,但不能在库外部访问。我已经知道使用注释@hide将解决问题。例如:

/**
 * @hide
 */
class Globals {

    public static String thisDeviceAddress;
    public static String thisDeviceIP;
    public static String thisDeviceName = "";
}

我用Google搜索了很多但却无法找到实现@hide的方法。仅使用没有库的@hide并没有隐藏类。所以,请给我适当的指导。任何要使用的库?任何出路来解决这个问题?

2 个答案:

答案 0 :(得分:0)

无法隐藏公共类和方法。它是Java语言规范,你不能破坏它。

答案 1 :(得分:0)

要使其正常工作,您将需要更改您的分发模型以包括两个 jars

  1. 一个“仅编译时” API jar,仅对您的库具有公共/非隐藏的“存根”。
  2. 一个“运行时实现” jar,其中包含该库的所有必需代码(在构建该库时,默认情况下会获得当前的aar / jar)。

要使用@hide并生成API存根,必须使用Doclava(实现javadoc并具有生成存根选项的自定义@hide Doclet)。

您可以按照以下步骤生成API存根jar:

  1. 从Google获取自定义Doclava doclet,您可以从https://android.googlesource.com/platform/external/doclava/克隆并构建自己的doclet,也可以下载https://mvnrepository.com/artifact/com.google.doclava/doclava的预构建jar。

  2. 将Doclava jar放置在库根目录(src目录旁边)的目录中,例如/path/to/project/mylib/doclava/doclava.jar

  3. 如下更改您的build.gradle

dependencies {
    ...
    ...

    // Make implementation and compileOnly classes accessible to other tasks.
    configurations.getAt("implementation").setCanBeResolved(true)
    configurations.getAt("compileOnly").setCanBeResolved(true)
}

// A gradle task to generate the stubs using Doclava.
task generateStubSources(type: Javadoc) {
    source = [file('src/main/java')]
    outputs.dir "$buildDir/api/stub-sources" 
    title = null

    options {
        bootClasspath = project.files(
                project.android.getBootClasspath(),
                configurations.implementation,
                configurations.compileOnly
        ).getFiles().asList()

        doclet = 'com.google.doclava.Doclava'
        docletpath = fileTree(dir: 'doclava', include: '**/*.jar').asType(List)
        addFileOption 'stubs', file("$buildDir/api/stub-sources")
        addBooleanOption 'hide 111', true
        addBooleanOption 'hide 113', true
        addBooleanOption 'keepstubcomments', true
        addBooleanOption 'stubsourceonly', true
        addBooleanOption 'nodocs', true
        addBooleanOption 'android', true

        noTimestamp false
        jFlags '-Dignore.symbol.file'
    }
}

// A task to compile the stubs.
task compileStubs(type: JavaCompile, dependsOn: 'generateStubSources') {
    source = fileTree("$buildDir/api/stub-sources")
    destinationDir = file("$buildDir/api/stub-classes")
    classpath = files(source, generateStubSources.options.bootClasspath)
}

// A task to jar the compiled stubs.
task jarStubs(type: Jar) {
    from compileStubs
    destinationDirectory = file("$buildDir/api")
    archiveBaseName = 'api'
}

// The full task to generate the API stubs jar.
task generateAndJarStubs(dependsOn: ['generateStubSources', 'jarStubs'])

afterEvaluate {
    // Generate and jar the stubs after building the actual project.
    compileReleaseSources.finalizedBy(generateAndJarStubs)
    compileDebugSources.finalizedBy(generateAndJarStubs)
}

  1. 像往常一样构建您的项目。 API存根jar文件将显示在/path/to/project/mylib/build/api/api.jar下。您可以像平常一样api.jar依赖项来分发和使用此compileOnly文件。

...并且不要忘记在部署时使用/打包库的实际实现jar / aar!