Imported module in Android Studio can't find imported class

56,876

Solution 1

First of all, you must import your library project by following that path:

File --> New --> Import Module

After you have imported the library project successfully, you must check your build.gradle file inside your project's folder if the following line is present at the "dependencies" section:

implementation project(':NameOfTheLibProject')

Then your project must be built successfully.

Solution 2

I had the same problem. I just did: Invalidate / Restart ..

Solution 3

I found that my issue was the Android Plugin Version under Project Structure --> Project was different to the version my plugins all used. Once I aligned them to the same version, I could see all my classes from my imported module.

Took me hours :(

Solution 4

I too had trouble importing module as it was not appearing in list of modules. And way it worked for me is manually entering it in settings.gradle this way:

include ':app', 'module_name'

And in build.gradle

compile project(':module_name')

Solution 5

In my case, I did add in app gradle:

// Nearly deprecated.
compile project(':NameOfTheLibProject')
// Or
implementation project(':NameOfTheLibProject')

but it only works when I change

compileSdkVersion 
minSdkVersion 
targetSdkVersion 

in app and the other modules are the same.

Share:
56,876
user1971
Author by

user1971

Updated on July 09, 2022

Comments

  • user1971
    user1971 almost 2 years

    I recently downloaded the ViewPagerIndicator library and imported it into android studio. After adding it to my project I get a rendering error "The following classes could not be found:" and points to com.viewpagerindicator.IconPageIndicator.

    The steps I took were Files->Import Module->'library name', Project Structure -> Dependencies -> + the imported module. Then to my layout xml file I added the <com.viewpagerindicator.IconPageIndicator />, after that I got the missing class problem.

    It compiles just fine and I went through all of the build.gradle and settings.gradle files and compared them to what they should be online.

    MyApp->build.gradle has compile project(':library') under dependencies settings.gradle has include ':library' with no build errors.

  • Quintin Balsdon
    Quintin Balsdon over 8 years
    You need to check that the Android Plugin Version is the same for both the Project and the imported module.
  • Vadiraj Purohit
    Vadiraj Purohit about 8 years
    +1 to your answer This looks to be a limitation on the android modules. What if we want to use main project with gradle plugin com.android.tools.build:gradle:2.1.0 and library module with com.android.tools.build:gradle-experimental:0.7.0? Library module's classes are not visible to main project in this case :-(
  • Lakedaemon
    Lakedaemon almost 7 years
    This saved my day too : my main project (android-gradle-plugin 2.4.0-alpha7') was reusing an external module (from another project whose android-gradle-plugin was 2.3.3). By aligning all plugins, some code (visible in the ide but invisible fro gradle) suddenly became visible for gradle.
  • Dan
    Dan about 6 years
    This sounds like the issue I'm having but when I go to "Project Structure > Project" the "Android Plugin Version" field is blank. Only the "Gradle Version" is populated. If I type something in that field it seems to get reset. Any ideas? This is driving me up the wall.
  • xarlymg89
    xarlymg89 about 6 years
    I had a very similar issue. My library was added fine and I could see the classes were reachable from my project. However, if a new class was created in the library, even a rebuild / clean + build didn't make the new classes available. I changed the minSdkVersion from my library build.gradle and voilà (just because I remembered I recently updated the one from my project)!
  • Nguyen Tan Dat
    Nguyen Tan Dat about 6 years
    Glad that it helps you. Cheer!
  • xarlymg89
    xarlymg89 about 6 years
    Although this works, I know we should be missing something. Shouldn't be that difficult to effectively recompile an Android Library. Either that, or there's an annoying bug.
  • Bheid
    Bheid about 6 years
    also check settings.gradle and include library like: include ':app', ':mylib'
  • Yawar
    Yawar over 5 years
    stuck at this for almost an hour. And this solved my issue. Logged in to so just to upvote this.
  • Admin
    Admin over 5 years
    compile is now deprecated. use implementation or api instead.
  • varun
    varun about 5 years
    Further, for your understanding, notice how the build.gradle file of your newly imported module has a line at the top that reads apply plugin: 'com.android.library', whereas your app's build.gradle file has the line apply plugin 'com.android.application'. Just a surface level insight as to what the IDE thanklessly does for your convenience!!! :P