Gradle(android实验插件)忽略项目依赖

时间:2015-09-22 21:37:13

标签: android gradle build android-gradle gradle-experimental

我在以下项目结构中使用Gradle Android实验插件:

Root Project
|-- app
|-- my-library

settings.gradle

include ':my-library', ':app'

的build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle-experimental:0.2.0'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

应用/的build.gradle

apply plugin: 'com.android.model.application'

model {
    android {
        compileSdkVersion = 23
        buildToolsVersion = "23.0.0"

        defaultConfig.with {
            applicationId = "a.bundle.id"
            minSdkVersion.apiLevel = 15
            targetSdkVersion.apiLevel = 23
            versionCode = 1
            versionName = "1.0"
        }
    }

    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles += file('proguard-rules.pro')
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.0'
    compile project(':my-library')
}

我库/的build.gradle

apply plugin: 'com.android.model.library'

model {
    android {
        compileSdkVersion = 'android-23'
        buildToolsVersion = '23.0.1'

        defaultConfig.with {
            minSdkVersion.apiLevel = 15
        }
    }

    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles += file('proguard-rules.txt')
        }
    }

    android.ndk {
        moduleName = "xxx"
        CFlags += "-I" + "${project.buildDir}".toString() + "/../src/main/jni/libabecs/include"
        CFlags += "-std=gnu99"
    }
    android.sources {
        main {
            jni {
                source {
                    srcDir 'src/main/jni/libxxx/src'
                }
            }
        }
    }
    android.productFlavors {
        create("arm") {
            ndk.abiFilters += "armeabi"
        }
        create("arm7") {
            ndk.abiFilters += "armeabi-v7a"
        }
        create("arm8") {
            ndk.abiFilters += "arm64-v8a"
        }
        create("x86") {
            ndk.abiFilters += "x86"
        }
        create("x86-64") {
            ndk.abiFilters += "x86_64"
        }
        create("mips") {
            ndk.abiFilters += "mips"
        }
        create("mips-64") {
            ndk.abiFilters += "mips64"
        }

        create("all")
    }
}

图书馆项目构建完美。在Android Studio中,它不会显示任何错误。但是,在尝试使用Gradle构建此项目时,它只会尝试构建app项目,并且实际上会忽略my-library依赖项,从而导致找不到类错误。

如何解决这个问题?

4 个答案:

答案 0 :(得分:5)

显然,它是构建插件中未实现的错误/功能。

我只找到一个肮脏的解决方法来直接链接生成的aar。

build.gradle(someappname)

repositories {
    flatDir {
        dirs '../my-library/build/outputs/aar'
    }
}

dependencies {
    compile project(':my-library')
    compile(name:'my-library-{flavour}', ext:'aar')
    //the rest
}

答案 1 :(得分:0)

我遇到了完全相同的问题,最后发现我的库项目中ndk源的路径是错误的,我必须添加:

dependencies {
    project ":myHybridLib"
}

在主项目的android.sources.main.jni中。

纠正这些错误后,一切都很好。

答案 2 :(得分:0)

my_library/build.gradle更改为以下内容:

apply plugin: 'com.android.model.library'

model {

android {
    compileSdkVersion = 23
    buildToolsVersion = "23.0.1"

    defaultConfig.with {
        minSdkVersion.apiLevel = 15
        targetSdkVersion.apiLevel = 23
    }
}

android.ndk {
    moduleName = "FlatBuffersParser"
    cppFlags.addAll(['-std=c++11', '-fexceptions', '-Wall', '-Wno-literal-suffix'])
    cppFlags.add("-I${file("src/main/jni/flatbufferslib")}".toString())
    ldLibs.addAll(["android", "log"])
    stl = "gnustl_shared"
   }
}

有关结构的更多信息,请参阅github中的这个项目,它在两个android模块中使用了实验性的v0.4.0:

https://github.com/frogermcs/FlatBuffs

答案 3 :(得分:-3)

面临同样的问题。我通过将app子模块重命名为其他东西来解决它。所以项目结构将是

Root Project
|-- someappname
|-- my-library

<强> settings.gradle

include ':my-library', ':someappname'