我正在使用Whatsapp贴纸源代码来构建自己的贴纸应用。当我使用目标SDK版本28或29构建此项目时,它可以完美编译,但是当我将目标SDK版本降低到27 时,它“无法链接资源。”我希望此代码能正常工作在Android Oreo设备上也是如此。
请注意,Whatsapp将其代码迁移到 Androidx 最近。
Build.gradle:
apply plugin: 'com.android.application'
android {
//the compression of webp file during build causes problem with FileDescriptor in ContentProvider.
aaptOptions {
noCompress "webp"
}
compileSdkVersion 27
defaultConfig {
applicationId 'com.sticker.app'
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
def contentProviderAuthority = applicationId + ".stickercontentprovider"
// Creates a placeholder property to use in the manifest.
manifestPlaceholders =
[contentProviderAuthority: contentProviderAuthority]
// Adds a new field for the authority to the BuildConfig class.
buildConfigField("String",
"CONTENT_PROVIDER_AUTHORITY",
"\"${contentProviderAuthority}\"")
}
buildTypes {
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}
packagingOptions {
exclude 'lib/*/libnative-imagetranscoder.so'
exclude 'lib/*/libnative-filters.so'
}
// Configures multiple APKs based on ABI.
// see https://developer.android.com/studio/build/configure-apk-splits#configure-abi-split
// splits {
// abi {
// enable true // Enables building multiple APKs per ABI.
// universalApk true // Specifies that we do not want to also generate a universal APK that includes all ABIs.
// }
// }
}
task checkDebug {
doLast {
println("checkDebug")
if (android.defaultConfig.applicationId.startsWith("com.whatsapp")) {
throw new GradleException("applicationId in defaultConfig cannot start with com.whatsapp, please change your applicationId in app/build.gradle")
}
checkApplicationIdInDebug()
}
}
private void checkApplicationIdInDebug() {
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def ignoreApplicationIdCheck = properties.getProperty('ignoreApplicationIdCheck')
if (ignoreApplicationIdCheck == null) {
if (android.defaultConfig.applicationId == "com.example.samplestickerapp") {
throw new GradleException("Your applicationId is currently com.example.samplestickerapp, please change your applicationId to a different string in app/build.gradle in line 10")
}
} else {
println("application id check ignored")
}
}
task checkRelease {
doLast {
println("checkRelease")
if (android.defaultConfig.applicationId.startsWith("com.example")) {
throw new GradleException("applicationId in defaultConfig cannot start with com.example, please change your applicationId in app/build.gradle")
}
}
}
tasks.whenTaskAdded { task ->
println(task.name)
if (task.name.contains("assembleDebug")) {
task.dependsOn checkDebug
}
if (task.name.contains("assembleRelease")) {
task.dependsOn checkRelease
}
}
Build.gradle依赖项:
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.facebook.fresco:fresco:2.0.0'
implementation 'com.facebook.fresco:webpsupport:2.0.0'
implementation 'com.facebook.fresco:animated-webp:2.0.0'
implementation 'com.facebook.fresco:animated-base:2.0.0'
}
我已经搜索了降低Androidx项目上的Target SDK的方法,但是还没有找到。
错误:
> Task :app:processDebugResources FAILED
AGPBI: {"kind":"error","text":"Android resource linking failed","sources":[{"file":"C:\\Users\\vivek\\.gradle\\caches\\transforms-2\\files-2.1\\37ec02f661e3547c5499ecdfd9f3cd1f\\appcompat-1.1.0\\res\\values-v28\\values-v28.xml","position":{"startLine":4,"startColumn":4,"startOffset":217,"endLine":7,"endColumn":12,"endOffset":442}}],"original":"C:\\Users\\vivek\\.gradle\\caches\\transforms-2\\files-2.1\\37ec02f661e3547c5499ecdfd9f3cd1f\\appcompat-1.1.0\\res\\values-v28\\values-v28.xml:5:5-8:13: AAPT: error: resource android:attr/dialogCornerRadius not found.\n ","tool":"AAPT"}
AGPBI: {"kind":"error","text":"Android resource linking failed","sources":[{"file":"C:\\Users\\vivek\\.gradle\\caches\\transforms-2\\files-2.1\\37ec02f661e3547c5499ecdfd9f3cd1f\\appcompat-1.1.0\\res\\values-v28\\values-v28.xml","position":{"startLine":8,"startColumn":4,"startOffset":447,"endLine":11,"endColumn":12,"endOffset":684}}],"original":"C:\\Users\\vivek\\.gradle\\caches\\transforms-2\\files-2.1\\37ec02f661e3547c5499ecdfd9f3cd1f\\appcompat-1.1.0\\res\\values-v28\\values-v28.xml:9:5-12:13: AAPT: error: resource android:attr/dialogCornerRadius not found.\n ","tool":"AAPT"}
AGPBI: {"kind":"error","text":"Android resource linking failed","sources":[{"file":"C:\\Users\\vivek\\.gradle\\caches\\transforms-2\\files-2.1\\c527b78844be871c44103f6f45a963fb\\core-1.1.0\\res\\values\\values.xml","position":{"startLine":141,"startColumn":4,"startOffset":8780,"endLine":172,"endColumn":24,"endOffset":10840}}],"original":"C:\\Users\\vivek\\.gradle\\caches\\transforms-2\\files-2.1\\c527b78844be871c44103f6f45a963fb\\core-1.1.0\\res\\values\\values.xml:142:5-173:25: AAPT: error: resource android:attr/fontVariationSettings not found.\n ","tool":"AAPT"}
AGPBI: {"kind":"error","text":"Android resource linking failed","sources":[{"file":"C:\\Users\\vivek\\.gradle\\caches\\transforms-2\\files-2.1\\c527b78844be871c44103f6f45a963fb\\core-1.1.0\\res\\values\\values.xml","position":{"startLine":141,"startColumn":4,"startOffset":8780,"endLine":172,"endColumn":24,"endOffset":10840}}],"original":"C:\\Users\\vivek\\.gradle\\caches\\transforms-2\\files-2.1\\c527b78844be871c44103f6f45a963fb\\core-1.1.0\\res\\values\\values.xml:142:5-173:25: AAPT: error: resource android:attr/ttcIndex not found.\n ","tool":"AAPT"}
AGPBI: {"kind":"error","text":"Android resource linking failed","sources":[{"file":"C:\\Users\\vivek\\.gradle\\caches\\transforms-2\\files-2.1\\37ec02f661e3547c5499ecdfd9f3cd1f\\appcompat-1.1.0\\res\\values\\values.xml","position":{"startLine":2771,"startColumn":4,"startOffset":173278,"endLine":2794,"endColumn":24,"endOffset":174475}}],"original":"C:\\Users\\vivek\\.gradle\\caches\\transforms-2\\files-2.1\\37ec02f661e3547c5499ecdfd9f3cd1f\\appcompat-1.1.0\\res\\values\\values.xml:2772:5-2795:25: AAPT: error: resource android:attr/textFontWeight not found.\n ","tool":"AAPT"}
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:processDebugResources'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
> Android resource linking failed
C:\Users\vivek\.gradle\caches\transforms-2\files-2.1\37ec02f661e3547c5499ecdfd9f3cd1f\appcompat-1.1.0\res\values-v28\values-v28.xml:5:5-8:13: AAPT: error: resource android:attr/dialogCornerRadius not found.
C:\Users\vivek\.gradle\caches\transforms-2\files-2.1\37ec02f661e3547c5499ecdfd9f3cd1f\appcompat-1.1.0\res\values-v28\values-v28.xml:9:5-12:13: AAPT: error: resource android:attr/dialogCornerRadius not found.
C:\Users\vivek\.gradle\caches\transforms-2\files-2.1\c527b78844be871c44103f6f45a963fb\core-1.1.0\res\values\values.xml:142:5-173:25: AAPT: error: resource android:attr/fontVariationSettings not found.
C:\Users\vivek\.gradle\caches\transforms-2\files-2.1\c527b78844be871c44103f6f45a963fb\core-1.1.0\res\values\values.xml:142:5-173:25: AAPT: error: resource android:attr/ttcIndex not found.
C:\Users\vivek\.gradle\caches\transforms-2\files-2.1\37ec02f661e3547c5499ecdfd9f3cd1f\appcompat-1.1.0\res\values\values.xml:2772:5-2795:25: AAPT: error: resource android:attr/textFontWeight not found.
答案 0 :(得分:0)
您的应用程序将在任何运行API级别至少为15的设备上运行。此设备由此minSdkVersion = 15
行定义。您仍然可以毫无问题地使用AndroidX依赖项。因此,请尝试使targetSdkVersion
保持最新。就您而言,您可以简单地保持原样,而无需降低它。同样,您希望您的应用运行的最低API级别由minSdkVersion
而非targetSdkVersion.
编辑
要使用AndroidX,您需要将编译版本至少设置为compileSdkVersion = 28
。您的情况是27
。还将这两行添加到您的gradle.properties
文件中:
android.useAndroidX = true
android.enableJetifier = true // This will ensure that third-party libraries will be migrated to AndroidX
答案 1 :(得分:0)
要使用 androidx 库,您必须使用
compileSdkVersion 28
或更高。
您可以检查official doc:
如果要使用AndroidX i 一个新项目,您需要将编译SDK设置为Android 9.0(API级别28)或更高版本,并同时设置两个在gradle.properties文件中将以下Android Gradle插件标志设置为true。
-android.useAndroidX
:设置为true
时,Android插件使用适当的AndroidX库而不是支持库。如果未指定,默认情况下该标志为false
。
-android.enableJetifier
:设置为true
时,Android插件会通过重写二进制文件来自动迁移现有第三方库以使用AndroidX。如果未指定,默认情况下该标志为false
。