【问题标题】:Cannot resolve symbol 'tools' and 'GradleException'无法解析符号“工具”和“GradleException”
【发布时间】:2017-01-12 19:42:16
【问题描述】:

我已经开始处理包括 Android NDK 在内的现有项目。我在 build.gradle 中有两个问题,我无法构建应用程序。供您参考,我的同事(正在开发它)能够构建该应用程序。

我已经导入了 NDK,从项目结构中我可以看到正确的 Android NDK 路径。

这是 build.gradle 的样子:

import org.apache.tools.ant.taskdefs.condition.Os

buildscript {
repositories {
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    // The Fabric Gradle plugin uses an open ended version to react
    // quickly to Android tooling updates
    classpath 'io.fabric.tools:gradle:1.21.5'
}
}
allprojects {
repositories {
    maven { url "https://jitpack.io" }
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'realm-android'

repositories {
maven { url 'https://maven.fabric.io/public' }
}

android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
dataBinding{
    enabled = true;
}

defaultConfig {
    applicationId "com.lucien.myapp"
    minSdkVersion 16
    targetSdkVersion 24
    versionCode 1
    versionName "1.0.0"

    ndk {
        moduleName "DSPLib-jni"
    }
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

sourceSets.main.jni.srcDirs = [] // disable automatic ndk-build call, which ignore our Android.mk
sourceSets.main.jniLibs.srcDir 'src/main/libs'

// call regular ndk-build(.cmd) script from app directory
task ndkBuild(type: Exec) {
    workingDir file('src/main')
    commandLine getNdkBuildCmd()
}

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

task cleanNative(type: Exec) {
    workingDir file('src/main')
    commandLine getNdkBuildCmd(), 'clean'
}

clean.dependsOn cleanNative

}

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

compile 'com.github.PhilJay:MPAndroidChart:v2.2.5'
compile 'com.orhanobut:dialogplus:1.11@aar'
compile('com.crashlytics.sdk.android:crashlytics:2.6.2@aar') {
    transitive = true;
}
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.google.code.gson:gson:2.7'

}

def getNdkDir() {
if (System.env.ANDROID_NDK_ROOT != null)
    return System.env.ANDROID_NDK_ROOT

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def ndkdir = properties.getProperty('ndk.dir', null)
if (ndkdir == null)
    throw new GradleException("NDK location not found. Define location with ndk.dir in the local.properties file or with an ANDROID_NDK_ROOT environment variable.")

return ndkdir

}

def getNdkBuildCmd() {
def ndkbuild = getNdkDir() + "/ndk-build"
if (Os.isFamily(Os.FAMILY_WINDOWS))
    ndkbuild += ".cmd"

return ndkbuild

}

我的第一行有问题,尝试导入“org.apache.tools.ant.taskdefs.condition.Os”:无法解析符号“工具”

对于“throw new GradleException("...")”也有同样的问题

我需要更新 build.gradle 中的某些内容吗?还是问题出在其他地方?

谢谢!

【问题讨论】:

  • 您能分享一下您拥有的 Gradle 版本吗?和你同事的版本一样吗?
  • 嗨 Stanislav,我正在使用 Gradle 2.14.1 和我的同事。
  • 3周了,你解决了吗?
  • 是的,我做到了,这是 Android Studio 2.2 测试版的问题,它在 2.1 版和新版 2.2(稳定版)中的稳定通道上运行良好
  • 对我来说仍然是个问题。我无法在 build.gradle 中解决上述问题。

标签: android gradle android-ndk


【解决方案1】:

您可以使用 java 中的任何其他可用异常,例如:

throw new FileNotFoundException("Could not read version.properties!")

【讨论】:

  • 也适用于 Flutter 项目。 Android Studio 3.5
【解决方案2】:

要解决此问题,只需将 GradleException() 更改为 FileNotFoundException()

throw new FileNotFoundException("NDK location not found...")

而不是

throw new GradleException("NDK location not found...")

【讨论】:

    【解决方案3】:

    对于那些来到这里的人,我通过返回 Android Studio 2.1 解决了我的问题。自稳定版 2.2 发布以来,它运行良好。

    【讨论】:

    • 如何回到Android Studio 2.1?
    • @K.Sopheak 你可以在这里找到旧版本的 AS:tools.android.com/download/studio/canary
    • 我也正在使用 Android Studio v3.5。
    • Android 4.0.1 - 这仍然是个问题。
    猜你喜欢
    • 2019-08-29
    • 2022-10-21
    • 2021-02-04
    • 2021-11-20
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多