1

I'm currently using Android Studio 2.0 preview 4. I have followed the guide from tools.android.com and tested NDK samples from github. The samples worked without a hitch, but when I implemented it on the SIPdroid project, it throws this error when I rebuild the project:

Error:(78, 1) A problem occurred configuring project ':app'. Exception thrown while executing model rule: model.android Cannot set readonly property: minSdkVersion for class: com.android.build.gradle.managed.ProductFlavor_Impl

when I try to use gradle project sync it gives this error:

Error:Unable to load class 'com.android.build.gradle.managed.ProductFlavor_Impl'. Possible causes for this unexpected error include:

  • You are using JDK version 'java version "1.7.0_79"'. Some versions of JDK 1.7 (e.g. 1.7.0_10) may cause class loading errors in Gradle. Please update to a newer version (e.g. 1.7.0_67). Open JDK Settings
  • Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.) Re-download dependencies and sync project (requires network)
  • The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem. Stop Gradle build processes (requires restart)
  • Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.
In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.

Android project structure now looks like this. previously the jni folder is separated from the java folder. enter image description here

Here's my config:

SIPdroid/app/build.gradle

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

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

        defaultConfig.with {
            applicationId = "com.test.sipdroid"
            minSdkVersion = 15
            targetSdkVersion = 23
            versionCode = 1
            versionName = "1.0"
        }
    }

    compileOptions.with {
        sourceCompatibility = JavaVersion.VERSION_1_7
        targetCompatibility = JavaVersion.VERSION_1_7
    }

    /*
     * native build settings
     */
    android.ndk {
        moduleName = "SIPdroid"
        /*
         * Other ndk flags configurable here are
         * cppFlags.add("-fno-rtti")
         * cppFlags.add("-fno-exceptions")
         * ldLibs.addAll(["android", "log"])
         * stl       = "system"
         */
    }

    android.sources {
        main.java {
            source {
                srcDir 'src'
            }
        }
        main.jni {
            source {
                srcDirs = []
            }
        }
        main.jniLibs {
            source {
                srcDirs = ['src/main/libs']
            }
        }
    }

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

    android.productFlavors {
        // for detailed abiFilter descriptions, refer to "Supported ABIs" @
        // https://developer.android.com/ndk/guides/abis.html#sa
        create("arm") {
            ndk.abiFilters.add("armeabi")
        }
        create("arm7") {
            ndk.abiFilters.add("armeabi-v7a")
        }
        create("arm8") {
            ndk.abiFilters.add("arm64-v8a")
        }
        create("x86") {
            ndk.abiFilters.add("x86")
        }
        create("x86-64") {
            ndk.abiFilters.add("x86_64")
        }
        create("mips") {
            ndk.abiFilters.add("mips")
        }
        create("mips-64") {
            ndk.abiFilters.add("mips64")
        }
        // To include all cpu architectures, leaves abiFilters empty
        create("all")
    }
}

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

SIPdroid/build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

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

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

SIPdroid/gradle-wrapper.properties

#Mon Jan 04 16:06:26 PHT 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip

SIPdroid/local.properties

ndk.dir=/path/Android/sdk/ndk-bundle
sdk.dir=/path/Android/sdk
4

1 回答 1

1

我最近刚刚解决了我的问题,方法是将它添加到我的原始 app/build.gradle 文件中,而不使用谷歌示例中所示的实验性 gradle 构建('com.android.tools.build:gradle-experimental:0.4.0')。

该解决方案最终解决了NDKBuild Failure问题。此附加脚本使用 ndkBuild 构建您的 jni 文件。

应用程序/build.gradle

sourceSets.main {
        jniLibs.srcDir 'src/main/libs' // use the jni .so compiled from the manual ndk-build command
        jni.srcDirs = [] //disable automatic ndk-build call
    }

    task ndkBuild(type: Exec) {
//        commandLine 'ndk-build', '-C', file('src/main/jni').absolutePath <-- Not working
        commandLine '/home/user/Android/sdk/ndk-bundle/ndk-build', '-C', file('src/main/jni').absolutePath
    }

    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn ndkBuild
    }

SIPdroid/build.gradle

dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
}

您还需要在 app/src/main 下有一个空的 libs 文件夹。我的错误是我将 /jni 文件夹重命名为 /libs。运行构建后,它会将您的 jni 编译到 /libs 文件夹中的 .so 文件

app/src/主要内容

您的 Android 项目结构视图中的 jniLibs 将如下所示。这来自您的 app/src/main/libs,如 build.gradle 脚本中所示

在此处输入图像描述

我希望这有帮助。

于 2016-01-07T03:29:34.103 回答