2

I have an Android Project, and I want to create a sub-library (just another Android Project) as library for this one.

Here is my Structure:

Project/ settings.gradle build.gradle /ProjectA /ProjectA/build.gradle /ProjectA/libs /ProjectA/libs/ProjectB /ProjectA/libs/ProjectB/build.gradle 

ProjectA is my main project. I do as following. In setting.gradle. I add:

include ':ProjectA' include ':ProjectA:libs:ProjectB' 

after that. I copy all information of ProjectA/build.gradle into ProjectB/build.gradle content of file is:

buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.6.+' } } apply plugin: 'android' repositories { mavenCentral() } android { compileSdkVersion 18 buildToolsVersion "18.1.1" defaultConfig { minSdkVersion 9 targetSdkVersion 18 } buildTypes { debug { packageNameSuffix ".debug" } } } 

After that, I change ProjectA/build.gradle. Add this line: compile projects(:libs:projectB) to dependencies. After that I receive error :

Project with path ':libs:ProjectB' could not be found in project ':ProjectA' 

Please help me how to configure for multiply project in Android Studio.

Thanks :)

2 Answers 2

1

Either of these will work:

compile project(':ProjectA:libs:ProjectB') 

or

compile project('libs:ProjectB') 

The leading colon is like a leading slash on a filesystem path: it makes it an absolute path, starting with the project root. Without a leading colon, it's relative to the module's root.

Having said all that, your project structure is complex. Does ProjectB really need to be contained in ProjectA? Would this work better instead?

Project/ settings.gradle build.gradle /ProjectA /ProjectA/build.gradle /ProjectB /ProjectB/build.gradle 
Sign up to request clarification or add additional context in comments.

Comments

0

I have solution for my problem. Sadly, I see no full tutorial for my problem. And by many mistakes, I can try it by hand.I have try a way : here is my short tutorial. Hope help someone here.

Short Description : I will add a SlidingMenu library to my project. This library is special because :

  1. it's an Eclipse-type project (means : has a slide difference compare to android project create by Android Studio or Intellij IDEA), so you must have a step config by hand. If this's a normal library create by Android Studio, you can remove mapping by hand step in below Gradle file.

  2. It uses v4 support library. Often, your app uses this library too. So, if you compile both same two libries in same project. You will meet error. So, there is a way : using Android Repository to synchronized. You will not meet exception that two same libraries in one project.

You create a libs folder inside your app. After that, copy whole project library to this folder. For example, I'm using SlidingMenu. After that, you create gradle.build with following content. ( I have added some comments, for easier to follow)

// SlidingMenu is an Eclipse project. so its structure is different. and must config by hand. // script to decide which gradle version will use for maven buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.6.+' } } // because this is a android library. we change : apply plugin: 'android' to apply plugin: 'android-library' // Base Project includes "Support Library v4 jar to its project // 1. If Not Compile it : this library project cannot compile because missing libraries // 2. Cannot delete jar file in libs folder. Because its code depend on this libs. Delete jar file turn out that cannot see class file // 3. My Solution : Use Gradle Repository. By use Gradle Repository, this library will be synchronized between two projects. // So, just one instance could be used for both project. dependencies { compile 'com.android.support:support-v4:19.0.0' } android { compileSdkVersion 18 buildToolsVersion "18.1.1" // Mapping phrase : use if this library is Eclipse-base project // modify link because this project is from Eclipse Project // not from normal gradle Project. So project structure is different. Cannot make default // Config by hand, get Gradle knows which part for each type of directory sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] resources.srcDirs = ['src'] aidl.srcDirs = ['src'] renderscript.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] } // Move the tests to tests/java, tests/res, etc... instrumentTest.setRoot('tests') debug.setRoot('build-types/debug') release.setRoot('build-types/release') } } 

Here is main gradle file :

buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.6.+' } } // this is main project : so we use apply plugin : 'android' apply plugin: 'android' repositories { mavenCentral() } android { compileSdkVersion 18 buildToolsVersion "18.1.1" defaultConfig { minSdkVersion 9 targetSdkVersion 18 } } dependencies { compile 'com.android.support:appcompat-v7:19.0.0' compile 'com.android.support:support-v4:18.0.0' // compile project SlidingMenu compile project(':Hop Am Chuan:libs:SlidingMenu') // add this line just for demonstration. you can use or remove it. // compile files('libs/google-gson-2.2.4/gson-2.2.4.jar') } 

Hope this help :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.