Android Studio:用于库项目的Config Gradle文件

时间:2013-12-14 01:16:10

标签: intellij-idea gradle android-studio

我有一个Android项目,我想创建一个子库(只是另一个Android项目)作为这个库的库。

这是我的结构:

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

ProjectA是我的主要项目。我做如下。在setting.gradle。我补充说:

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

之后。我将ProjectA/build.gradle的所有信息复制到ProjectB/build.gradle文件内容中:

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"
        }
    }
}

之后,我改变ProjectA/build.gradle。将此行:compile projects(:libs:projectB)添加到依赖项。之后我收到错误:

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

请帮我介绍如何在Android Studio中配置多个项目。

谢谢:)

2 个答案:

答案 0 :(得分:1)

其中任何一个都可行:

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

compile project('libs:ProjectB')

前导冒号就像文件系统路径上的前导斜杠:它使它成为绝对路径,从项目根开始。没有前导冒号,它与模块的根相对。

说了这么多,你的项目结构很复杂。 ProjectB真的需要包含在ProjectA中吗?这会改善吗?

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

答案 1 :(得分:0)

我解决了我的问题。可悲的是,我看不到我的问题的完整教程。由于许多错误,我可以手工尝试。我尝试了一下:这是我的简短教程。希望能在这里帮助别人。

简短说明:我将在项目中添加SlidingMenu库。这个库很特别,因为:

  
      
  1. 这是一个Eclipse类型的项目(意思是:与Android Studio或Intellij IDEA创建的android项目相比有一个幻灯片差异),   所以你必须手动配置步骤。如果这是一个普通的库   通过Android Studio创建,您可以在下面手动删除映射   Gradle文件。

  2.   
  3. 它使用v4支持库。通常,您的应用也会使用此库。所以,如果你在同一个项目中编译两个相同的两个库。你会   遇到错误。所以,有一种方法:使用Android Repository进行同步。您不会遇到一个项目中两个相同库的异常。

  4.   

您在应用内创建了一个libs文件夹。之后,将整个项目库复制到此文件夹。例如,我正在使用SlidingMenu。之后,您使用以下内容创建gradle.build。 (我添加了一些评论,以便于遵循)

// 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')
    }
}

这是主要的gradle文件:

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')

}

希望这有帮助:)