Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling with the source of another module in Android Studio

In Android Studio I have an Android project which contains an module called go setup with Gradle to be an Android library. It contains a single Java package called com.nwoods.go. For the sake of this Stack Overflow submission, it contains a class called FooBar

Inside the same Android project, I have an application module called ugurdemo1 which has a MainActivity class in the package com.nwoods.ugurdemo1.

Android Studio I have modified the project structure for the module ugurdemo1 to depend on the library go at compile time. In ugurdemo1's MainActivity, I'd like to reference and use the class FooBar from go. To do this, I prefaced the class with a typical Java import statement:

   import com.nwoods.go.FooBar;
   ... 
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FooBar myFooBarObject = null; 

Android Studio still can not resolve the symbol for FooBar after doing a clean rebuild of my sources. Similar questions on Stack Overflow were resolved by editing the compile time settings in the Dependency dialog but again that does not work. If it is of any help, I am currently running Android Studio IO Preview (0.3.6)

like image 289
deadboy Avatar asked Nov 22 '13 21:11

deadboy


People also ask

Can we import module from source in Android Studio?

Select the source directory of the Module you want to import and click Finish. Open Project Structure Dialog (You can open the PSD by selecting File > Project Structure) and from the left panel click on Dependencies. Select the module from the Module(Middle) section In which you want to add module dependency.

How do I import a module into gradle?

In Android Studio, Right-clicking on the folder to add as library. Editing the dependencies in the build. gradle file, adding: compile fileTree(dir: 'libs', include: '*. jar')}

What is AAR file in Android?

AAR files can contain Android resources and a manifest file, which allows you to bundle in shared resources like layouts and drawables in addition to Java classes and methods. AAR files can contain C/C++ libraries for use by the app module's C/C++ code.

What is multi module project in Android?

A project with multiple Gradle modules is known as a multi-module project. In a multi-module project that ships as a single APK with no feature modules, it's common to have an app module that can depend on most modules of your project and a base or core module that the rest of the modules usually depend on.


1 Answers

It sounds like you've done the right thing, but to confirm, here's a screenshot of a Project Structure dependencies panel that lets you set module dependencies in Gradle:

Screenshot of Android Studio Project Structure dialog

This module already has dependencies on two libraries retrieved via Maven-style imports, and it also as a dependency on the Foo module. To add a new dependency, you click on the + button at the bottom and choose a dependency type (shown).

You can also edit your Gradle build files by hand. In my example, there's a build.gradle file in the "two" module's directory. It has a dependencies block that looks like this:

dependencies {
    compile 'com.android.support:support-v4:+'
    compile 'com.android.support:appcompat-v7:+'
    compile project(':Foo')
}

You can add further compile project lines to add new module dependencies; use a colon-delimited syntax to provide the position of the module relative to the project's root directory.

If you edit the build.gradle file by hand, click on the "Sync Project with Gradle Files" button in the toolbar when you're done to make Android Studio re-read the file and pick up your changes.

like image 78
Scott Barta Avatar answered Sep 28 '22 15:09

Scott Barta