【问题标题】:Correct way to change android API更改android API的正确方法
【发布时间】:2016-01-11 03:28:24
【问题描述】:

我正在尝试使用 android studio 将 Android API 从 23 降低到 16,但在项目运行时出现错误。我已经安装了 sdk 版本 18。

之前

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"


    defaultConfig {
        applicationId "com.example.project.myapplication"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'

}

之后

apply plugin: 'com.android.application'

android {
    compileSdkVersion 16
    buildToolsVersion "23.0.2"


    defaultConfig {
        applicationId "com.example.project.myapplication"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
}

错误

  Error:(231, 62) error: diamond operator is not supported in -source 1.6
(use -source 7 or higher to enable diamond operator)

清理项目后

C:\Users\tongws\AndroidStudioProjects\MyApplication1\app\build\intermediates\exploded-aar\com.android.support\appcompat-v7\23.1.0\res\values-ldltr-v21\values-ldltr-v21.xml
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.Spinner.Underlined'.
C:\Users\tongws\AndroidStudioProjects\MyApplication1\app\build\intermediates\exploded-aar\com.android.support\appcompat-v7\23.1.0\res\values-ldrtl-v23\values-ldrtl-v23.xml
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.Spinner.Underlined'.
C:\Users\tongws\AndroidStudioProjects\MyApplication1\app\build\intermediates\exploded-aar\com.android.support\appcompat-v7\23.1.0\res\values-v17\values-v17.xml
C:\Users\tongws\AndroidStudioProjects\MyApplication1\app\build\intermediates\exploded-aar\com.android.support\appcompat-v7\23.1.0\res\values-v21\values-v21.xml
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Inverse'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Medium.Inverse'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Large.Inverse'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Small.Inverse'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.ProgressBar'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.ProgressBar.Horizontal'.

谁能告诉我如何解决这个问题?

【问题讨论】:

标签: android api android-studio gradle build


【解决方案1】:

您的问题是您正在使用较低的 SDK 编译您的应用

compileSdkVersion 18

在 SDK 版本 18 中,您不支持 Material Desgin:

<style name="Base.Widget.AppCompat.Spinner.Underlined" parent="android:Widget.Material.Spinner.Underlined"/>

您还可以通过在 Gradle 构建中添加此依赖项来将 Material Design 集成到您的应用中:

compile "com.android.support:appcompat-v7:21.0.+"

查看此链接: android-developers

你得到这个:

C:\Users\tongws\AndroidStudioProjects\MyApplication1\app\build\intermediates\exploded-aar\com.android.support\appcompat-v7\23.1.0\res\values-ldltr-v21\values-ldltr-v21.xml
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.Spinner.Underlined'.
C:\Users\tongws\AndroidStudioProjects\MyApplication1\app\build\intermediates\exploded-aar\com.android.support\appcompat-v7\23.1.0\res\values-ldrtl-v23\values-ldrtl-v23.xml
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.Spinner.Underlined'.
C:\Users\tongws\AndroidStudioProjects\MyApplication1\app\build\intermediates\exploded-aar\com.android.support\appcompat-v7\23.1.0\res\values-v17\values-v17.xml
C:\Users\tongws\AndroidStudioProjects\MyApplication1\app\build\intermediates\exploded-aar\com.android.support\appcompat-v7\23.1.0\res\values-v21\values-v21.xml
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Inverse'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Medium.Inverse'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Large.Inverse'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Small.Inverse'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.ProgressBar'.
Error:(2) Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.ProgressBar.Horizontal'.

因为编译器找不到资源,所以我从链接发布的解决方案是旧的并且它没有这些资源。可能是您使用的资源是在较新版本的 AppCompact 中添加的,因此您需要在 android studio 中创建一个新的应用程序项目并复制 java 文件或更改设计以支持较低的 SDK

还要检查您是否从 Android SDK 管理器下载了 SDK。 如果你想在你的应用中使用它们,你需要这些工具和资源

【讨论】:

  • 是的,如果你想支持较低的 SDKs
  • 我已编辑答案检查该解决方案并让我知道
  • 支持库不应使用低于目标sdk的版本(21)
  • 所以你在添加我上面提到的代码时遇到了这个错误?
  • 我需要删除 compile 'com.android.support:appcompat-v7:23.1.0' 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-11
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多