Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link module from external project in Android Studio

I'm migrating all my project from Eclipse to Android Studio (1.0.2, just download it yesterday) but having issue with external module.

In Eclipse, I have workspace like this

enter image description here

All the activities, fragments, models and classes are in 'Core' project. The Core project required some libraries to work (Such as Google Play Service, Facebook or Twitter) While App 1, App 2, App 3 and so on are just while label apps. Those app doesn't contain anything except icons, configuration files, loading images etc.

I manage to import "Core" app and all it dependencies to Android Studio as a new project. When I build the core, I got 0 errors and 0 warnings

enter image description here

Then, I create new project call "Test" and link "Core" project to it by following selected answer from this question

How to share a single library source across multiple projects

setting.gradle of Test Project

include ':Test'
include '..:..:AppyCore:Core'

build.gradle of Test Project

dependencies {
    compile 'com.android.support:support-v4:+'
    compile project('..:..:AppyCore:Core')
}

But, when I rebuild project, I got this error

Error:(41, 0) Project with path ':SlidingMenu' could not be found in project '..:..:AppyCore:Core'

When I double click the error message, IDE show me build.gradle of the Core project and highlight the dependency part of the file as following

enter image description here

Seems like when I try to build the "Test" project, it can't locate all the dependencies of "Core" project. Do you know how to fix this?

Note

  • I believe that the "Core" project setup is already correct because I did create "Test 2" app and import "Core" project into the same root directory, same project. It was working without any issue. But this is not an option because I actually have about 20+ white label app. It will be super difficult to update one by one if each of those app has it own "Core".
  • I think the directory that I include is correct, otherwise, I'll instead get this error

    "Error: Configuration with name 'default' not found."

(I tried by intentionally put wrong directory and got this same error)

like image 368
Tar_Tw45 Avatar asked Feb 19 '15 05:02

Tar_Tw45


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.

What is multi module project 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.

How import AAR to gradle?

Add your AAR or JAR as a dependency In the Declared Dependencies tab, click and select Jar Dependency in the dropdown. In the Add Jar/Aar Dependency dialog, first enter the path to your . aar or . jar file, then select the configuration to which the dependency applies.


1 Answers

You are trying to make a new project for each app version, and link them to a common codebase. In this scenario, it's much better to use the "variant" feature of Gradle. With it, each app is a "variant" of the core app, with has its own package name, its own resource files, and even its own src files.

To do so you need to:

  1. modify build.gradle for your core app:

    android {
      productFlavors {
        // default BuildConfig variables
        all {
        }
        app_1 {
            applicationId 'com.yourcompany.app_1' //package name
            def variantName='app_1' //name of the variant
        }
        app_2 {
            applicationId 'com.yourcompany.app_2' //package name
            def variantName='app_2' //name of the variant
        }
      }
    }
    
  2. Create app_1, app_2... app_x folders in /src, each with its own AndroidManifest.xml, /src and /res folders.

  3. Change the current variant in Android Studio: enter image description here

You can find more info on building variant on Android Tools Project Site.

I can personally vouch for this approach, as I've used this such that a single Android Studio project supports the building of dozens variant, each with its own unique package name, resource files, and custom code that significantly modifies some aspect of the base app, all the while sharing a common base code.

like image 170
Kai Avatar answered Oct 05 '22 18:10

Kai