Android Module with Java Module
已回答
I'm trying to create a new project that consists of a Java library and an Android application. The Android application is dependent on the Java library and needs to compile the Java library before compiling the Android app. What I tried doing was creating an Empty Project, added a new Java module, then tried adding a new Android module but the IDE would give errors and the Android module doesn't show up. How can I create a project that consists of my Java library and my Android application?
请先登录再写评论。
Hello,
What error do you get during adding Android module?
When I create the empty project, add a Java module then try to add the Android module it gives me there error: "The project '...' is not a Gradle-based project". I was able to get beyond that (I don't remember how) but the Android module wouldn't show up in the module list and I had the error "Plugin with id 'com.android.application' not found". Even though the Android module wasn't listed the source code had been added to my project.
IDEA doesn't support mixing Gradle and not-Gradle modules in one project. So you need to create project with two modules. One is Java and another one is Android.
I was experimenting some more and I most of the way there except I'm having an issue building now.
What I did so far:
But now when I try to access the test class in the Android module I get an "cannot find symbol class MyClass" (MyClass is the name of my test class). What am I missing to make this work? If you need to see the project structure or want to see the source code let me know and I'll be happy to post whatever will help get this answered.
It would be great if you could share sampe project.
Here's a link to the files: https://drive.google.com/file/d/1fUqdNhvPPFan75E-pOS0xnZ-01nqaL0f/view?usp=sharing
Thanks for example! How did you "In the "app" module I added a Module Dependency on the Java module" ? I don't see any dependency on Java Module in provided example.
I did it through File -> Project Structure -> Modules, selected "App" and went to the dependencies tab. When I reloaded the project just now I noticed the dependency wasn't there. It looks like the dependency isn't being saved properly, every time I reload the project that dependency disappears. Even after adding it manually the project still says it can't find the symbol "MyClass".
You need to change your "AndroidApplication/settings.gradle" file to:
```
include ':app'
include ':CommonLibrary'
project(':CommonLibrary').projectDir = new File(settingsDir, '../CommonLibrary')
```
So you "CommonLibrary" project will be added as a dependency. Please take a look at the related article: https://looksok.wordpress.com/2014/07/12/compile-gradle-project-with-another-project-as-a-dependency/
Thanks, that article really helps. Unfortunately I'm still running into issues. Following that article I get an error saying I have a missing dependency "Error:Unable to find module with Gradle path ':CommonLibrary' (needed by module 'app'.)". After some Googling I found an article saying to delete the settings.gradle in the CommonLibrary project. Doing that allows the Android app to compile but then the library generates an error on build "Project 'CommonLibrary' not found in root project 'CommonLibrary'.".
Please try to use the same project I changed on my machine. unpack it and open "AndroidApplication" project in IDEA. It should automatically add "CommonLibrary" project and configure dependencies between them.
https://drive.google.com/file/d/1wnpn_eZPk08f9Hqy1KLXyep1Jm_DGiVg/view?usp=sharing
That's looking a lot better what had to be changed to make it work?
One thing I noticed is that the "app" configuration won't run as it doesn't have a module defined and when I edit the configuration there are no modules in the dropdown.
As I said earlier I opened "AndroidApplication" and changed settings.gradle to add external dependency.
Missing "app" module looks like some cache collision. Please try to comment and then uncomment two lines in settings.gradle:
```
include ':CommonLibrary'
project(':CommonLibrary').projectDir = new File(settingsDir, '../CommonLibrary')
```
Thanks, after some messing around I see what you did to get it working. That solution could work, however, ideally I would prefer if the CommonLibrary would build at the same time as the Android project as opposed to having to build the JAR then build the Android project. I've posted a question on Stack Overflow with what I'm trying to achieve with hopefully a better description of my setup/goals. The question is here.
Gradle build task should aslo build dependencies.