5

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.

Umang
  • 966
  • 2
  • 7
  • 17
  • i am attempting to do the same thing. I noticed that SUBCLASSES and TESTS works correctly when specifying these as RestrictTo values. However, LIBRARY and LIBRARY_GROUP seems not to work. – Jon Nov 05 '18 at 00:20
  • @Umang have you had any luck with this? – Mr.Noob Nov 08 '18 at 12:59
  • Insightful: https://twitter.com/alexjlockwood/status/945384107424567296?lang=en – Jared Rummler Nov 09 '18 at 07:50
  • @Mr.Noob No, Haven't been able find a solution yet. – Umang Nov 09 '18 at 17:42
  • @JaredRummler I had seen the post before posting the questions, but that does not help. No lint error when calling the method as well. – Umang Nov 09 '18 at 17:44
  • @Umang I wonder if it's something to do with how the group id is defined? – Mr.Noob Nov 12 '18 at 07:26
  • Under which package is your `TaskProcessor` defined? Did you try to use a completely different package? – aveuiller Nov 12 '18 at 09:24
  • @Mr.Noob I have defined the group as follows **group="com.umang"** – Umang Nov 12 '18 at 10:35
  • @Antoine I have used different package names for both the projects. Library Project's package name = **com.umang.asyncprocessor** Caller App's package name = **com.myapplication** – Umang Nov 12 '18 at 10:37

1 Answers1

2

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.

Sergei Bubenshchikov
  • 5,275
  • 3
  • 33
  • 60
  • Do you mean my library should be published? Currently it is a single android studio project with 2 modules. So artifact-id and group-id does not come into picture. I wanted to test it out before releasing hence trying out this way. – Umang Nov 12 '18 at 10:54
  • @Umang I think, it doesn't matter library is published or not – Sergei Bubenshchikov Nov 12 '18 at 11:10
  • In that case I am not really sure what you mean. I will push the code to github and share it. Would be great if you can have a look and help. Thanks – Umang Nov 12 '18 at 11:53
  • I have added the github url in the question please check. – Umang Nov 13 '18 at 17:37
  • @Umang I've create pull request to your repository and added notes about it to my answer, please check – Sergei Bubenshchikov Nov 13 '18 at 20:10
  • The lint error shows up now, but still able to run the application with the error. If you look at some of the APIs in the support library(for example stop() in androidx.work.Worker) it shows a compilation error. Any way to achieve that?? – Umang Nov 14 '18 at 06:29
  • @Umang I've checked `androidx.work.ListenableWorker.stop()` and as expected it's works the same way. How do you compile the project? – Sergei Bubenshchikov Nov 14 '18 at 18:05
  • Sorry my bad it works the same way. Thanks a ton for helping out. – Umang Nov 15 '18 at 01:56
  • @Umang thanks for posting this question. Does this throw a compilation error for you now or does it only show a lint error? Thanks – Mr.Noob Nov 16 '18 at 12:51