Home > Net >  Android Studio - Setting up a Project hierarchy that checks source files in real time (Ant style)
Android Studio - Setting up a Project hierarchy that checks source files in real time (Ant style)

Time:10-27

I've got two Android applications (not libraries) on Android Studio:

  • App A can be built and run on its own;
  • App B can be built and run on its own, but needs App A as a dependency.

I've currently set up the hierarchy as instructed in this question (and similar ones), but the result is that the current state of App A was copied inside the App B project folder as a module, so any changes I make inside the actual source files in the project folder of App A are never visible from App B.

What I'd like to achieve is for App B to reference the source files in the App A project folder, and possibly take notice of the changes I make to App A source files in real time without having to manually rebuild/resync anything (i.e. if I delete a referenced variable in App A and save, Android Studio should immediately show the error in App B, etc...).

Basically how an extremely basic Ant project hierarchy you can set up with a couple clicks in Java IDEs would work.

How would I accomplish that in Android Studio?

CodePudding user response:

I'm provisionally answering my own question, even though what I've got now feels more like a workaround than a good practice, but until someone more experienced chimes in here it is.

App B - settings.gradle:

[...]
include ':App_A'
project(':App_A').projectDir = new File('..\\App_A\\app')
[...]

App B - build.gradle:

dependencies {
[...]
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation fileTree(dir: '..\\App_A', include: ['*.jar'])
implementation project(path: ':App_A')
[...]
}

This is assuming both projects have the same root directory, as in:

C:\Android_Projects\App_A
C:\Android_Projects\App_B

Otherwise, you can input an absolute path in place of ".." in both gradle files.

With that done, your project view of App B in Android Studio will also show a tree structure of the project directory of App A, which is convenient for editing source files of App A while working on App B. But you can also open the App A project in a different window of Android Studio, and all changes made there will immediately be reflected in the App B window as well.

  • Related