如何修复gradle错误?

时间:2017-03-16 14:05:45

标签: java android gradle android-ndk android-gradle

我的项目包括NDK。这是build.gradle文件(应用程序级别)。

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

model {
    android {
        compileSdkVersion = 25
        buildToolsVersion = "25.0.1"

        defaultConfig.with {
            applicationId = "com.example.app"
            minSdkVersion.apiLevel = 15
            targetSdkVersion.apiLevel = 22
            versionCode = 1
            versionName = "1.0"
        }
    }
    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles.add(file("proguard-rules.pro"))
        }
    }
    android.ndk {
        moduleName = "HelloARVideoNative"
        cppFlags.add("-I${file("/home/obx/Downloads/EasyARSDKSamples/package/include")}".toString())
        cppFlags.add("-DANDROID")
        cppFlags.add("-fexceptions")
        cppFlags.add("-frtti")
        stl = "gnustl_static"
        ldLibs.add("log")
        ldLibs.add("GLESv2")
    }
    android.productFlavors {
        create("arm") {
            ndk.with {
                abiFilters.add("armeabi-v7a")
            }
        }
    }
    android.sources {
        main {
            jni {
                dependencies {
                    library file("/home/obx/Downloads/EasyARSDKSamples/package/Android/libs/armeabi-v7a/libEasyAR.so") abi "armeabi-v7a"
                }
            }
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: '/home/obx/Downloads/EasyARSDKSamples/package/Android/libs')
    compile 'com.android.support:appcompat-v7:25.1.1'
    testCompile 'junit:junit:4.12'
}

这是我获得的错误:

  

错误:未找到Gradle DSL方法:' library()'

这就是我的尝试:

这是我的顶级gradle文件:

    buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle-experimental:0.7.3'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

问题:可能导致此问题的原因;以及如何解决?

2 个答案:

答案 0 :(得分:1)

我不确定这是否有帮助,但http://tools.android.com/tech-docs/new-build-system/gradle-experimental表示在某些时候NDK库的Android Gradle语法已更改

  

用于指定特定库文件依赖关系的DSL已更改为遵循Gradle的本机依赖关系DSL。

从他们的考试来看,您似乎需要将脚本更改为

model {
    ...
    repositories {
        prebuilt(PrebuiltLibraries) {
            binaries.withType(SharedLibraryBinary) {
                sharedLibraryFile = file("/home/obx/Downloads/EasyARSDKSamples/package/Android/libs/armeabi-v7a/libEasyAR.so")
                // or even 
                // sharedLibraryFile = file("/home/obx/Downloads/EasyARSDKSamples/package/Android/libs/${targetPlatform.getName()}/libEasyAR.so")
            }
        }
    }
    android.sources {
        main {
            jni {
                dependencies {
                     library "prebuilt"
                }
            }
        }
    }
}

您也可以考虑使用jniLibs而不是jni来描述为

  

您可以将本机依赖项添加到“jniLibs”或“jni”源集。当依赖项添加到“jniLibs”时,本机库将打包到应用程序/库中,但它不会用于编译JNI代码。

答案 1 :(得分:0)

我刚刚解决了它,libEasyAR.so的链接存在问题。 这是build.gradle经过一些修改后的样子:

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

    model {
        android {
            compileSdkVersion = 25
            buildToolsVersion = "25.0.2"

            defaultConfig.with {
                applicationId = "cn.easyar.samples.helloar"
                minSdkVersion.apiLevel  =15
                targetSdkVersion.apiLevel = 22
                versionCode = 1
                versionName = "1.0"
            }
        }
        android.buildTypes {
            release {
                minifyEnabled = false
                proguardFiles.add(file("proguard-rules.pro"))
            }
        }
        android.ndk {
            moduleName = "HelloARNative"
            cppFlags.add("-I${file("../../../package/include")}".toString())
            cppFlags.add("-DANDROID")
            cppFlags.add("-fexceptions")
            cppFlags.add("-frtti")
            stl = "gnustl_static"
            ldFlags.add("-LC:/Users/Me/Documents/AR_SDK/EasyAR/EasyARSDKSamples/Android/HelloARNative/app/src/main/jniLibs/armeabi-v7a")
            ldLibs.add("log")
            ldLibs.add("GLESv2")
            ldLibs.add("EasyAR")
        }
        android.productFlavors {
            create("arm") {
                ndk.with {
                    abiFilters.add("armeabi-v7a")
                }
            }
        }
        repositories {
            libs(PrebuiltLibraries) {
                prebuilt {
                    headers.srcDir "src/main/jni"
                    binaries.withType(SharedLibraryBinary) {
                        sharedLibraryFile = file("src/main/libs/armeabi-v7a/libEasyAR.so")
                    }
                }
            }
        }
        android.sources {
            main {
                jniLibs {
                    dependencies {
                        library "prebuilt"
                    }
                }
            }
        }
    }

    dependencies {
        compile fileTree(include: ['*.jar'], dir: '../../../package/Android/libs')
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:20.0.0'
    }

您必须将文件夹添加到文件夹libs下的main文件夹中。

希望它会有所帮助!

(抱歉我的英文,我是法国人)