简体   繁体   English

Android Studio:库项目的Config Gradle文件

[英]Android Studio : Config Gradle file for library project

I have an Android Project, and I want to create a sub-library (just another Android Project) as library for this one. 我有一个Android项目,我想创建一个子库(只是另一个Android项目)作为该库的库。

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. ProjectA是我的主要项目。 I do as following. 我做如下。 In setting.gradle . 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: 我将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"
        }
    }
}

After that, I change ProjectA/build.gradle . 之后,我更改ProjectA/build.gradle Add this line: compile projects(:libs:projectB) to dependencies. 添加以下行:将compile projects(:libs:projectB)为依赖项。 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. 请帮助我如何在Android Studio中配置乘法项目。

Thanks :) 谢谢 :)

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? ProjectB是否真的需要包含在ProjectA中? Would this work better instead? 这样做会更好吗?

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

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. 简短说明:我将向我的项目添加一个SlidingMenu库。 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. 这是一个Eclipse类型的项目(意味着:与Android Studio或Intellij IDEA创建的android项目相比,有幻灯片上的区别),因此您必须手动进行一步配置。 If this's a normal library create by Android Studio, you can remove mapping by hand step in below Gradle file. 如果这是Android Studio创建的普通库,则可以在Gradle文件下方手动删除映射。

  2. It uses v4 support library. 它使用v4支持库。 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. 因此,有一种方法:使用Android存储库进行同步。 You will not meet exception that two same libraries in one project. 您将不会遇到一个项目中有两个相同的库的例外。

You create a libs folder inside your app. 您在应用程序内创建一个libs文件夹。 After that, copy whole project library to this folder. 之后,将整个项目库复制到此文件夹。 For example, I'm using SlidingMenu . 例如,我正在使用SlidingMenu After that, you create gradle.build with following content. 之后,使用以下内容创建gradle.build ( 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 : 这是主要的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')

}

Hope this help :) 希望这个帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM