0

我已经设置了一个带有 retrolambda 的 App 项目,以实现 JAVA 8 兼容性和对自己的 android 库的模块依赖,因为我也在第二个 App 中使用了这部分。问题是每次我用 gradle 构建我的项目时都没有错误并且构建成功,但是当我想在我的设备上安装应用程序时,gradle 失败了

:app:compileDebugJavaWithJavac with package R does not exist

这是我的gradle设置:

build.gradle(项目:“项目名称”)

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'me.tatarka:gradle-retrolambda:3.2.5'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

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

build.gradle(模块:app)

apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"

    defaultConfig {
        applicationId "my.package.app"
        minSdkVersion 19
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    packagingOptions {
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE.txt'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:design:24.0.0'
    compile 'org.florescu.android.rangeseekbar:rangeseekbar-library:0.3.0'
    compile project(path: ':myownmodule')
}

build.gradle(模块:myownmodule)

apply plugin: 'com.android.library'
apply plugin: 'me.tatarka.retrolambda'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    configurations {
        compile.exclude module: 'support-annotations'
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.google.code.gson:gson:2.6.1'
    compile 'io.reactivex:rxandroid:1.1.0'
    compile 'io.reactivex:rxjava:1.1.3'
}

这是 gradle 控制台的输出:

Executing tasks: [clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:compileDebugSources, :app:compileDebugAndroidTestSources, :app:compileDebugUnitTestSources, :myownmodule:generateDebugSources, :myownmodule:mockableAndroidJar, :myownmodule:prepareDebugUnitTestDependencies, :myownmodule:generateDebugAndroidTestSources, :myownmodule:compileDebugSources, :myownmodule:compileDebugUnitTestSources, :myownmodule:compileDebugAndroidTestSources]

Configuration on demand is an incubating feature.
Incremental java compilation is an incubating feature.
Jack is required to support java 8 language features.
Jack is required to support java 8 language features.
:clean
:app:clean
:myownmodule:clean
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:myownmodule:preBuild UP-TO-DATE
:myownmodule:preReleaseBuild UP-TO-DATE
:myownmodule:compileReleaseNdk UP-TO-DATE
:myownmodule:compileLint
:myownmodule:copyReleaseLint UP-TO-DATE
:myownmodule:mergeReleaseProguardFiles UP-TO-DATE
:myownmodule:packageReleaseRenderscript UP-TO-DATE
:myownmodule:checkReleaseManifest
:myownmodule:preDebugAndroidTestBuild UP-TO-DATE
:myownmodule:preDebugBuild UP-TO-DATE
:myownmodule:preDebugUnitTestBuild UP-TO-DATE
:myownmodule:preReleaseUnitTestBuild UP-TO-DATE
:myownmodule:prepareComAndroidSupportAnimatedVectorDrawable2400Library
:myownmodule:prepareComAndroidSupportAppcompatV72400Library
:myownmodule:prepareComAndroidSupportSupportV42400Library
:myownmodule:prepareComAndroidSupportSupportVectorDrawable2400Library
:myownmodule:prepareIoReactivexRxandroid110Library
:myownmodule:prepareReleaseDependencies
:myownmodule:compileReleaseRenderscript
:myownmodule:generateReleaseResValues UP-TO-DATE
:myownmodule:generateReleaseResources
:myownmodule:packageReleaseResources
:myownmodule:compileReleaseAidl
:myownmodule:generateReleaseBuildConfig
:myownmodule:mergeReleaseShaders
:myownmodule:compileReleaseShaders
:myownmodule:generateReleaseAssets
:myownmodule:mergeReleaseAssets
:myownmodule:mergeReleaseResources
:myownmodule:processReleaseManifest
:myownmodule:processReleaseResources
:myownmodule:generateReleaseSources
:myownmodule:incrementalReleaseJavaCompilationSafeguard
:myownmodule:compileReleaseJavaWithJavac
:myownmodule:compileRetrolambdaRelease
:myownmodule:processReleaseJavaRes UP-TO-DATE
:myownmodule:transformResourcesWithMergeJavaResForRelease
:myownmodule:transformClassesAndResourcesWithSyncLibJarsForRelease
:myownmodule:mergeReleaseJniLibFolders
:myownmodule:transformNative_libsWithMergeJniLibsForRelease
:myownmodule:transformNative_libsWithSyncJniLibsForRelease
:myownmodule:bundleRelease
:app:prepareProjectnameBalldbmodelUnspecifiedLibrary
:app:prepareComAndroidSupportAnimatedVectorDrawable2400Library
:app:prepareComAndroidSupportAppcompatV72400Library
:app:prepareComAndroidSupportDesign2400Library
:app:prepareComAndroidSupportRecyclerviewV72400Library
:app:prepareComAndroidSupportSupportV42400Library
:app:prepareComAndroidSupportSupportVectorDrawable2400Library
:app:prepareIoReactivexRxandroid110Library
:app:prepareOrgFlorescuAndroidRangeseekbarRangeseekbarLibrary030Library
:app:prepareDebugDependencies
:app:compileDebugAidl
:app:compileDebugRenderscript
:app:generateDebugBuildConfig
:app:mergeDebugShaders
:app:compileDebugShaders
:app:generateDebugAssets
:app:mergeDebugAssets
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources
:app:mergeDebugResources
:app:processDebugManifest
:app:processDebugResources
:app:generateDebugSources
:app:preDebugAndroidTestBuild UP-TO-DATE
:app:prepareDebugAndroidTestDependencies
:app:compileDebugAndroidTestAidl
:app:processDebugAndroidTestManifest
:app:compileDebugAndroidTestRenderscript
:app:generateDebugAndroidTestBuildConfig
:app:mergeDebugAndroidTestShaders
:app:compileDebugAndroidTestShaders
:app:generateDebugAndroidTestAssets
:app:mergeDebugAndroidTestAssets
:app:generateDebugAndroidTestResValues UP-TO-DATE
:app:generateDebugAndroidTestResources
:app:mergeDebugAndroidTestResources
:app:processDebugAndroidTestResources
:app:generateDebugAndroidTestSources
:app:mockableAndroidJar
:app:preDebugUnitTestBuild UP-TO-DATE
:app:prepareDebugUnitTestDependencies
:app:incrementalDebugJavaCompilationSafeguard
:app:compileDebugJavaWithJavac
/path/to/project/Projectname/app/src/main/java/my/package/app/adapter/HersListAdapter.java:37: error: package R does not exist
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_hers, parent, false);
                                                                        ^
/path/to/project/Projectname/app/src/main/java/my/package/app/adapter/HersListAdapter.java:56: error: package R does not exist
                ListActivity.switchTo(listActivity, str(R.string.series)+" " + hers.getName(), hers);
                                                                             ^
/path/to/project/Projectname/app/src/main/java/my/package/app/adapter/HersListAdapter.java:80: error: package R does not exist
            root = (LinearLayout) layout.findViewById(R.id.root);
                                                       ^
/path/to/project/Projectname/app/src/main/java/my/package/app/adapter/HersListAdapter.java:82: error: package R does not exist
            hersImg = (ImageView) layout.findViewById(R.id.hersImg);
                                                             ^
/path/to/project/Projectname/app/src/main/java/my/package/app/adapter/HersListAdapter.java:84: error: package R does not exist
            hersname = (TextView) layout.findViewById(R.id.hersname);
                                                             ^
/path/to/project/Projectname/app/src/main/java/my/package/app/adapter/HersListAdapter.java:85: error: package R does not exist
            ballanzahl = (TextView) layout.findViewById(R.id.count);
                                                         ^
/path/to/project/Projectname/app/src/main/java/my/package/app/adapter/PassListAdapter.java:35: error: package R does not exist
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_two_lines, parent, false);
                                                                        ^
/path/to/project/Projectname/app/src/main/java/my/package/app/adapter/PassListAdapter.java:61: error: package R does not exist
            root = (LinearLayout) layout.findViewById(R.id.root);
                                                       ^
/path/to/project/Projectname/app/src/main/java/my/package/app/adapter/PassListAdapter.java:63: error: package R does not exist
            line1 = (TextView) layout.findViewById(R.id.line1);
                                                    ^
/path/to/project/Projectname/app/src/main/java/my/package/app/adapter/PassListAdapter.java:64: error: package R does not exist
            line2 = (TextView) layout.findViewById(R.id.line2);
                                                    ^
/path/to/project/Projectname/app/src/main/java/my/package/app/adapter/PisteListAdapter.java:35: error: package R does not exist
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_two_lines, parent, false);
                                                                        ^
/path/to/project/Projectname/app/src/main/java/my/package/app/adapter/PisteListAdapter.java:65: error: package R does not exist
            root = (LinearLayout) layout.findViewById(R.id.root);
                                                       ^
/path/to/project/Projectname/app/src/main/java/my/package/app/adapter/PisteListAdapter.java:67: error: package R does not exist
            line1 = (TextView) layout.findViewById(R.id.line1);
                                                    ^
/path/to/project/Projectname/app/src/main/java/my/package/app/adapter/PisteListAdapter.java:68: error: package R does not exist
            line2 = (TextView) layout.findViewById(R.id.line2);
                                                    ^
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
14 errors

 FAILED
:app:compileRetrolambdaDebug FAILED

FAILURE: Build completed with 2 failures.

1: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
==============================================================================

2: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':app:compileRetrolambdaDebug'.
> Process 'command '/path/to/jdk_1.8.0-72_x86_64/bin/java'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
==============================================================================

BUILD FAILED

Total time: 32.407 secs

我想知道这一点,因为我可以毫无问题地安装具有相同设置的第二个应用程序。为什么 gradle 在这个应用程序中崩溃?在我的 java 类中,编程没有问题,并且存在对 android R 类的导入,但是在构建安装 gradle 时删除了这个导入,所有 R 将在几秒钟前变为红色。Android Studio 自动导入正确的 R

import my.package.app.R;

并且在导入之后,R 不再是红色了。

我已经测试了 gradle clean 并重建了几次(如其他问题中所建议的那样),但没有解决问题......

感谢您的帮助:D

4

1 回答 1

0

我最近的一个项目运行良好,直到今天发生同样的错误。

错误:任务 ':app:mergeDebugResources > 1 的执行失败

我尝试了所有常见的修复,重新启动,重新同步,清理解决方案,使缓存无效并重新启动。什么都没起作用,直到...

修复:

对于每个类中的每个 build.gradle 文件,您将看起来像这样:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.1"

    defaultConfig {
        applicationId "com.ac130.memorygame"
        minSdkVersion 21
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

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

现在,打开一个新项目。这个新项目应该调试得很好。从 Gradle Scripts 复制并粘贴新项目 build.gradle 并用该代码替换旧/损坏项目的 build.grade 文件。将应用程序 ID 更改为旧项目上的 ID。保存并与 Gradle 重新同步。您现在可以使用您的应用程序了!

于 2016-07-24T04:39:42.800 回答