Hello, during the development of an application for Android, the following problem arose:

there are java files that are designed to implement the logic for connecting to a device such as 1, they are in package Device1 (there are also subpackages inside this package), other java files are in package Device2 .

Each package ( Device1 , Device2 ) has java files with the same name, for example Func . The packages Device1 , Device2 should work independently of each other. Those. Device1 package files cannot reference Func from Device2 package and vice versa.

There is also the CommonConnection package, which implements the initial connection to the packages Device1 , Device2 . In this case, the CommonConnection package should have access to all other packages of both Device1 and Device2 .

Is it possible to somehow prohibit files from the Device1 package to refer to files from the Device2 package and vice versa, so that by mistake when entering auto-completion you do not enter the file from someone else's package, while the common CommonConnection package can access both the Device1 package files and Device2 files?

Thank you in advance for all the answers.


Created modules Device1, Device2, CommonConnection . In build.gradle for CommonConnection introduced dependencies for Device1, Device2 eventually got an error.

Error while adding dependencies


I summarize here the solution to my problem, maybe someone will come in handy in the future:

1) Create 3 modes for CommonConnection, Device1, Device2 . We need the files in CommonConnection to have access to Device1, Device2 . And Device1, Device2 were isolated from each other, i.e. the file from Device1 could not have access to Device2 . 2) After creating the modules, go to gradle for Device1 and make the following changes:

**apply plugin: 'com.android.application'** 

change to apply plugin: 'com.android.library'

And in defaultConfig, remove the line applicationId "com.bignerdranch.android.Device1"

The result should be

3) For Device2, you need to do the same. 4) In the gradle file for CommonConnection , add the line compile project (': Device1')

the result should be

5) Next, you need to explicitly indicate that you need to start the activation of the CommonConnection module initially . Click on Select Run / Debug Configuration and select the CommonConnection module. Next, once again click on Select Run / Debug Configuration -> EditConfigurations .. ** in the ** Launch Options menu in the Activity row click on ... and select activations for CommonConnection in particular MainActivityCommonConnection .

the result should be

6) Now you can run the project and check.

    1 answer 1

    Well, just do not make these classes public and they will only be visible within the package. And better carry these packages in different modules of the project.

    • Thank you very much for the answer. The fact is that I still have a package, let's call it "CommonConnection" . To implement the initial connection logic to a device of type 1 , or type 2 (respectively, either the packages Device1 or Device2 are called ). Is it possible to provide access to all Device1 , Device2 packages (modules) to the package or CommonConnection module, and prevent Device2 from accessing Device2 (and back)? The question in the main branch reformulated - foxis
    • one
      just add Device1 and Device2 depending on the CommonConnection module, and they don’t add everything in each other’s dependencies) - xkor
    • one
      Modules are connected in the same way as libraries in the dependencies section, just write compile project(':Device1') for example. If you have this module not in the root folder, but say in the modules subfolder, then there will be a compile project(':modules:Device1') - xkor
    • one
      You should have only one module - the application, the other modules should be libraries and just be dependent on the application module that it could use their functionality. Just change the use of the plugin com.android.application to com.android.library in the build.gradle scripts for Device1 and Device2 . Well, to start, select CommonConnection. - xkor
    • one
      So you should have only one main activity, unless you want to have several launch shortcuts for the application, check which activations are specified in the launch configuration for CommonConnection - xkor