2017-03-06 96 views
1

我今天升级了Android Studio 2.2 - > 2.3,并且在Android Studio中构建我的应用程序时,现在有一个编译器错误,我之前没有。Android Studio 2.3:无法在android中使用字符串资源:versionName

Error:No resource found that matches the given name (at 'versionName' with value '@string/BRAND_VERSION'). 

这是字符串资源。

<string name="BRAND_VERSION">2.0</string> 

这是清单中的相关部分。

<manifest package="com.metaswitch.cp.unbrandedpackage" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:versionCode="5" 
     android:versionName="@string/BRAND_VERSION"> 

Android documentation,字符串资源被允许在中的versionName清单或的build.gradle。

这不是特定于我的应用程序。我可以用此代替Android sample

它构建和安装对我来说很好。当我将这一行更改为build.gradle中的versionName(从strings.xml中选取一个随机字符串资源)时,它会失败并显示相同的错误。

// versionName "1.0" 
versionName "@string/add_task" 

这是Android示例中的build.gradle(注意我的编辑为versionName)。

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion rootProject.ext.compileSdkVersion 
    buildToolsVersion rootProject.ext.buildToolsVersion 

    defaultConfig { 
     applicationId "com.example.android.architecture.blueprints.todomvploaders" 
    minSdkVersion rootProject.ext.minSdkVersion 
    targetSdkVersion rootProject.ext.targetSdkVersion 
    versionCode 1 
    // I really want to use a string resource here! 
    // versionName "1.0" 
    versionName "@string/add_task" 

    testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner' 
} 

buildTypes { 
    debug { 
     minifyEnabled true 
     // Uses new built-in shrinker http://tools.android.com/tech-docs/new-build-system/built-in-shrinker 
     useProguard false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     testProguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguardTest-rules.pro' 
    } 

    release { 
     minifyEnabled true 
     useProguard true 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     testProguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguardTest-rules.pro' 
    } 
} 

// If you need to add more flavors, consider using flavor dimensions. 
productFlavors { 
    mock { 
     applicationIdSuffix = ".mock" 
    } 
    prod { 

    } 
} 

// Remove mockRelease as it's not needed. 
android.variantFilter { variant -> 
    if(variant.buildType.name.equals('release') 
      && variant.getFlavors().get(0).name.equals('mock')) { 
     variant.setIgnore(true); 
    } 
} 

// Always show the result of every unit test, even if it passes. 
testOptions.unitTests.all { 
    testLogging { 
     events 'passed', 'skipped', 'failed', 'standardOut', 'standardError' 
    } 
} 
buildToolsVersion '25.0.0' 

}

/* 
Dependency versions are defined in the top level build.gradle file. This helps keeping track of 
all versions in a single place. This improves readability and helps managing project complexity. 
*/ 
dependencies { 
    // App's dependencies, including test 
    compile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion" 
    compile "com.android.support:cardview-v7:$rootProject.supportLibraryVersion" 
    compile "com.android.support:design:$rootProject.supportLibraryVersion" 
    compile "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion" 
    compile "com.android.support:support-v4:$rootProject.supportLibraryVersion" 
    compile "com.android.support.test.espresso:espresso-idling-resource:$rootProject.espressoVersion" 
    compile "com.google.guava:guava:$rootProject.guavaVersion" 
// Dependencies for local unit tests 
testCompile "junit:junit:$rootProject.ext.junitVersion" 
testCompile "org.mockito:mockito-all:$rootProject.ext.mockitoVersion" 
testCompile "org.hamcrest:hamcrest-all:$rootProject.ext.hamcrestVersion" 

// Android Testing Support Library's runner and rules 
androidTestCompile "com.android.support.test:runner:$rootProject.ext.runnerVersion" 
androidTestCompile "com.android.support.test:rules:$rootProject.ext.runnerVersion" 

// Dependencies for Android unit tests 
androidTestCompile "junit:junit:$rootProject.ext.junitVersion" 
androidTestCompile "org.mockito:mockito-core:$rootProject.ext.mockitoVersion" 
androidTestCompile 'com.google.dexmaker:dexmaker:1.2' 
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2' 

// Espresso UI Testing 
androidTestCompile "com.android.support.test.espresso:espresso-core:$rootProject.espressoVersion" 
androidTestCompile "com.android.support.test.espresso:espresso-contrib:$rootProject.espressoVersion" 
androidTestCompile "com.android.support.test.espresso:espresso-intents:$rootProject.espressoVersion" 

// Resolve conflicts between main and test APK: 
androidTestCompile "com.android.support:support-annotations:$rootProject.supportLibraryVersion" 
androidTestCompile "com.android.support:support-v4:$rootProject.supportLibraryVersion" 
androidTestCompile "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion" 
androidTestCompile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion" 
androidTestCompile "com.android.support:design:$rootProject.supportLibraryVersion" 

}

+0

'minSdkVersion'的值仅在未在build.gradle构建脚本中指定时使用。当在Gradle构建脚本中指定时,menifest值将被忽略并且可能会产生误导,因此应该将其删除以避免模糊。 –

+0

发布您的'build.gradle' – pskink

+0

它对我来说工作得很好。 –

回答

1

我升级过Android Studio后有同样的问题。关闭首选项的即时运行有助于应用程序再次运行。

+0

哇!我无法想象这将是根本原因。我也有同样的错误,当我禁用即时运行时,它就消失了...... 对Google来说很奇怪! – Yogesh

相关问题