Search code examples
androidbuild.gradleandroid-library

How to Include Another Library within a Library


I've made a Library that implements androidx.annotation but when I use that library in a app the app can't resolve annotation.

build.gradle (Module : library)

dependencies {
    implementation 'androidx.annotation:annotation:1.0.0'
}

build.gradle (Module : app)

dependencies {
    implementation project(':library')
}

MainActivity.java (app)

import androidx.annotation.NonNull;

public class MainActivity extends AppCompatActivity {
    // ...

   @NonNull
   public String getString() {
       // ...
   }
}

Whenever I try to Run App, it says no class found for androidx.annotation.NonNull. I want to Include androidx.annotation with my library. How can I do that?


Solution

  • The reason why it is not working might be the way you have added the dependency. Implementation means that the dependency will only be available and accessible to the library module which explains why you're facing this.

    Use api instead. It means that the dependency will also be available and accessible to the module that depends on this module.

    Try this:

    build.gradle (Module : library)

    dependencies {
        api 'androidx.annotation:annotation:1.0.0'
    }