SIPdroid Android Studio ndk 集成错误

SIPdroid Android Studio ndk integration error

我目前正在使用 Android Studio 2.0 预览版 4。我遵循了 github 中 tools.android.com and tested NDK samples 的指南。示例工作顺利,但是当我在 SIPdroid 项目上实现它时,它会在我重建项目时抛出此错误:

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

当我尝试使用 gradle 项目同步时出现此错误:

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 项目结构现在看起来像这样。以前 jni 文件夹与 java 文件夹分开。

这是我的配置:

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.属性

#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.属性

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

我最近通过将此添加到我的原始 app/build.gradle 文件而不使用实验性 gradle 构建 ('com.android.tools.build:gradle-experimental:0.4.0') 解决了我的问题,如google 个样本。

这个解决方案终于解决了问题。此附加脚本使用 ndkBuild 构建您的 jni 文件。

app/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 文件

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

所示

希望对您有所帮助。