OBX
OBX

Reputation: 6114

How to fix the gradle error?

My project includes NDK . And this is the build.gradle file (app-level).

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

This is the error that I do obtain :

Error:Gradle DSL method not found: 'library()'

This is what I tried :

This is my top level gradle file:

    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
}

Question: What is possibly causing this; and how to resolve ?

Upvotes: 1

Views: 389

Answers (2)

JulianGama
JulianGama

Reputation: 1

I just fixed it, it was a problem with the linkage of libEasyAR.so. This is how my build.gradle look like after some modifications :

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

You have to add the library in a folder libs under your folder main.

Hope that it will help !

(Sorry for my english, I'm a french guy)

Upvotes: 0

SergGr
SergGr

Reputation: 23788

I'm not sure if this helps but http://tools.android.com/tech-docs/new-build-system/gradle-experimental says that at some point Android Gradle syntax for NDK libraries was changed

The DSL for specifying dependencies on a specific library files have changed to follow Gradle's native dependency DSL.

From their examlpe it looks like you need to change your script to be something like

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

You may aslo consider using jniLibs instead of jni which is described as

You can add native dependency to either 'jniLibs' or 'jni' source set. When dependency is added to "jniLibs" the native library will be package into the application/library, but it will not be used for compiling the JNI code.

Upvotes: 1

Related Questions