Umang
Umang

Reputation: 1070

RestrictTo not restricting usage of restricted method

I have annotated one of my library project's method as restricted @RestrictTo(Scope.LIBRARY) even tried @RestrictTo(Scope.LIBRARY_GROUP) but this not prevent the APIs from being used in the other modules of the project. I have even tried setting group=xxx and group=yyy in both modules.

enter image description here

Restricting API call

enter image description here

No error/warning shown by the Android Studio.

enter image description here Event lint is enabled for Restricted APIs.

I have even tried running lint on the caller module using ./gradlew lint

Please find the implementation on Github

library module - async-task-processor

tried setting different groups - module example

tried using a completely different package name - module myapplication

Not sure what is wrong here please help.

Upvotes: 5

Views: 2810

Answers (1)

Sergei Bubenshchikov
Sergei Bubenshchikov

Reputation: 5371

From point of view Scope.LIBRARY_GROUP annotation all you modules is parts of one library if they have equal groupId

Restrict usage to code within the same group of libraries. This corresponds to the gradle group ID.

To restrict API by Scope.LIBRARY you also need to use different artifactId

Restrict usage to code within the same library (e.g. the same gradle group ID and artifact ID).


It's necessary to add library as external dependency. You need to build and deploy your library artifact:

// follow answer https://stackoverflow.com/a/28361663/3926506 to build 
artifact
group = 'com.umang.asyncprocessor'
version = '1.0'

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "file://[path to you repository]")
            // repository(url: "file://C:/Users/Sergey/.m2/repository")
        }
    }
}

You can deploy artifact for example to local maven repository (don't forget to add mavenLocal() to project build script).

And then add dependency from compiled library in app build.gradle file:

implementation 'com.umang.asyncprocessor:async-task-processor:1.0'

not from project module:

// it doesn't work!
implementation project(path: ':async-task-processor')

I've create pull request to your repository with appropriate configuration.

Upvotes: 2

Related Questions