user1300214
user1300214

Reputation:

Can't resolve module classes in Android Studio

I have two separate Android projects A and B. I wish to use B in A, but I don't want it copied into A. I just want it to use.

So I add this to project A's settings.gradle:

include ':ProjectA', ':ProjectB'

project(':ProjectB').projectDir = new File('/dir/dir/ProjectB/')

Syncing gradle works fine, but when I clean and rebuild, Project A can't resolve any symbols in Project B.

So I Alt-Enter on an unresolved symbols and click "add dependency on module Project B," which then adds the following to Project A's gradle.build file:

compile project(path: ':ProjectB')

But clean+build don't work either.

I've tried changing to

compile project(path: ':ProjectB', configuration: 'default')

and

implementation project(path: ':ProjectB')

but they don't work either. Any ideas?

Using gradle 3.1.2, Android 3.1.2. Ubuntu 16.04 LTS.

Output

ProjectA/build.gradle   
Unable to resolve dependency for ':ProjectA@debug/compileClasspath': Could not resolve project :ProjectB.   
Unable to resolve dependency for ':ProjectA@debugAndroidTest/compileClasspath': Could not resolve project :ProjectB.    
Unable to resolve dependency for ':ProjectA@debugUnitTest/compileClasspath': Could not resolve project :ProjectB.   
Unable to resolve dependency for ':ProjectA@release/compileClasspath': Could not resolve project :ProjectB. 
Unable to resolve dependency for ':ProjectA@releaseUnitTest/compileClasspath': Could not resolve project :ProjectB. 

Upvotes: 2

Views: 1255

Answers (1)

user1300214
user1300214

Reputation:

What another nightmare. The only way I could work this out was as follows, which I believe is the correct way.

Create an Android Library (not Java Library or you can't use the Android functions), then load in your library classes/files, and set up your gradle with the appropriate implementation statements (i.e. dependencies). But you need to be careful here because Android Studio (AS) creates both "app" and "library" modules within the same Project. So make sure you put your library classes in the correct module, otherwise it won't generate an AAR file (as opposed to JAR) file for you to include externally elsewhere. Once you build the project you'll find the AAR file in the build/outputs/aar directory.

Ok, now you can create your separate application, and use the AAR file as a dependencey. Once you've created your new main project, go to Project Structure and click the + in the op left to add a new Module. Select Import JAR/AAR from the menu and direct the selector to your AAR file in the build folder mentioned above. Click OK to get back to the main window. Then Select the main app module, and then click the Dependencies tab and click + to add a Module. Select the AAR module from the list, then click OK to exit.

The system should now recognise your classes and have no undefined symbols. This was worked perfectly and I like it very much as it uses a nice compact AAR file.

Upvotes: 2

Related Questions