Android:导入module报错Android resource linking failed

今日将A工程(调试无问题)生成module导入B工程时报错,如下:

Android resource linking failed
Output:  W/ResourceType(16200): For resource 0x0101053d, entry index(1341) is beyond type entryCount(1329)
W/ResourceType(16200): For resource 0x0101053e, entry index(1342) is beyond type entryCount(1329)
W/ResourceType(16200): For resource 0x0101053b, entry index(1339) is beyond type entryCount(1329)
W/ResourceType(16200): For resource 0x0101053c, entry index(1340) is beyond type entryCount(1329)
E:\WorkSpace\ASProjects\Task\demo\module_aiview\build\intermediates\packaged_res\release\layout\dialog_root.xml:9: error: attribute layout_constraintLeft_toLeftOf (aka com.xxx...(app包名):layout_constraintLeft_toLeftOf) not found.
E:\WorkSpace\ASProjects\Task\demo\module_aiview\build\intermediates\packaged_res\release\layout\dialog_root.xml:9: error: attribute layout_constraintTop_toTopOf (aka com.xxx...(app包名):layout_constraintTop_toTopOf) not found.
E:\WorkSpace\ASProjects\Task\demo\module_aiview\build\intermediates\packaged_res\release\layout\dialog_root.xml:19: error: attribute layout_constraintLeft_toLeftOf (aka com.xxx...(app包名):layout_constraintLeft_toLeftOf) not found.
E:\WorkSpace\ASProjects\Task\demo\module_aiview\build\intermediates\packaged_res\release\layout\dialog_root.xml:19: error: attribute layout_constraintTop_toBottomOf (aka com.xxx...(app包名):layout_constraintTop_toBottomOf) not found.
E:\WorkSpace\ASProjects\Task\demo\module_aiview\build\intermediates\packaged_res\release\layout\view_weather_page_first.xml:8: error: attribute layout_constraintLeft_toLeftOf (aka com.xxx...(app包名):layout_constraintLeft_toLeftOf) not found.
E:\WorkSpace\ASProjects\Task\demo\module_aiview\build\intermediates\packaged_res\release\layout\view_weather_page_first.xml:8: error: attribute layout_constraintTop_toTopOf (aka com.xxx...(app包名):layout_constraintTop_toTopOf) not found.
E:\WorkSpace\ASProjects\Task\demo\module_aiview\build\intermediates\packaged_res\release\layout\view_weather_page_first.xml:16: error: attribute layout_constraintLeft_toLeftOf (aka com.xxx...(app包名):layout_constraintLeft_toLeftOf) not found.
E:\WorkSpace\ASProjects\Task\demo\module_aiview\build\intermediates\packaged_res\release\layout\view_weather_page_first.xml:16: error: attribute layout_constraintTop_toTopOf (aka com.xxx...(app包名):layout_constraintTop_toTopOf) not found.
E:\WorkSpace\ASProjects\Task\demo\module_aiview\build\intermediates\packaged_res\release\layout\view_weather_page_first.xml:27: error: attribute layout_constraintLeft_toRightOf (aka com.xxx...(app包名):layout_constraintLeft_toRightOf) not found.
E:\WorkSpace\ASProjects\Task\demo\module_aiview\build\intermediates\packaged_res\release\layout\view_weather_page_first.xml:27: error: attribute layout_constraintTop_toTopOf (aka com.xxx...(app包名):layout_constraintTop_toTopOf) not found.
E:\WorkSpace\ASProjects\Task\demo\module_aiview\build\intermediates\packaged_res\release\layout\view_weather_page_first.xml:42: error: attribute layout_constraintLeft_toRightOf (aka com.xxx...(app包名):layout_constraintLeft_toRightOf) not found.
E:\WorkSpace\ASProjects\Task\demo\module_aiview\build\intermediates\packaged_res\release\layout\view_weather_page_first.xml:42: error: attribute layout_constraintTop_toTopOf (aka com.xxx...(app包名):layout_constraintTop_toTopOf) not found.
E:\WorkSpace\ASProjects\Task\demo\module_aiview\build\intermediates\packaged_res\release\layout\view_weather_page_first.xml:57: error: attribute layout_constraintLeft_toRightOf (aka com.xxx...(app包名):layout_constraintLeft_toRightOf) not found.
E:\WorkSpace\ASProjects\Task\demo\module_aiview\build\intermediates\packaged_res\release\layout\view_weather_page_first.xml:57: error: attribute layout_constraintTop_toTopOf (aka com.xxx...(app包名):layout_constraintTop_toTopOf) not found.
E:\WorkSpace\ASProjects\Task\demo\module_aiview\build\intermediates\packaged_res\release\layout\view_weather_page_first.xml:73: error: attribute layout_constraintLeft_toLeftOf (aka com.xxx...(app包名):layout_constraintLeft_toLeftOf) not found.
E:\WorkSpace\ASProjects\Task\demo\module_aiview\build\intermediates\packaged_res\release\layout\view_weather_page_first.xml:73: error: attribute layout_constraintTop_toTopOf (aka com.xxx...(app包名):layout_constraintTop_toTopOf) not found.
E:\WorkSpace\ASProjects\Task\demo\module_aiview\build\intermediates\packaged_res\release\layout\view_weather_page_first.xml:83: error: attribute layout_constraintLeft_toLeftOf (aka com.xxx...(app包名):layout_constraintLeft_toLeftOf) not found.
E:\WorkSpace\ASProjects\Task\demo\module_aiview\build\intermediates\packaged_res\release\layout\view_weather_page_first.xml:83: error: attribute layout_constraintTop_toTopOf (aka com.xxx...(app包名):layout_constraintTop_toTopOf) not found.
E:\WorkSpace\ASProjects\Task\demo\module_aiview\build\intermediates\packaged_res\release\layout\view_weather_page_first.xml:98: error: attribute layout_constraintLeft_toRightOf (aka com.xxx...(app包名):layout_constraintLeft_toRightOf) not found.
E:\WorkSpace\ASProjects\Task\demo\module_aiview\build\intermediates\packaged_res\release\layout\view_weather_page_first.xml:98: error: attribute layout_constraintTop_toTopOf (aka com.xxx...(app包名):layout_constraintTop_toTopOf) not found.

Command: C:\Users\xxx\.gradle\caches\transforms-1\files-1.1\aapt2-3.2.0-4818971-windows.jar\712487c5244809689d4ef71a4ede44c6\aapt2-3.2.0-4818971-windows\aapt2.exe link -I\
        D:\Android\Sdk\platforms\android-25\android.jar\
        --manifest\
        E:\WorkSpace\ASProjects\Task\demo\app\build\intermediates\merged_manifests\release\processReleaseManifest\merged\AndroidManifest.xml\
        -o\
        E:\WorkSpace\ASProjects\Task\demo\app\build\intermediates\processed_res\release\processReleaseResources\out\resources-release.ap_\
        -R\
        @E:\WorkSpace\ASProjects\Task\demo\app\build\intermediates\incremental\processReleaseResources\resources-list-for-resources-release.ap_.txt\
        --auto-add-overlay\
        --java\
        E:\WorkSpace\ASProjects\Task\demo\app\build\generated\not_namespaced_r_class_sources\release\processReleaseResources\r\
        --proguard-main-dex\
        E:\WorkSpace\ASProjects\Task\demo\app\build\intermediates\legacy_multidex_aapt_derived_proguard_rules\release\processReleaseResources\manifest_keep.txt\
        --custom-package\
        xxx\
        -0\
        apk\
        --output-text-symbols\
        E:\WorkSpace\ASProjects\Task\demo\app\build\intermediates\symbols\release\R.txt\
        --no-version-vectors
Daemon:  AAPT2 aapt2-3.2.0-4818971-windows Daemon #0

很显然这是使用constraintlayout时资源索引异常,此时我在module中对constraintlayout的依赖方式如下:

repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
...
  compileOnly(name: 'constraint-layout-1.1.3', ext: 'aar')
...
}

有些网友分享说解决此问题的方式是在主工程app下的gradle中加入如下依赖:

implementation 'androidx.constraintlayout:constraintlayout:1.1.2'

果然,加入如上语句后编译成功!但是问题来了:在app gradle中引用的androidx资源包,在module中引用的android aar,而且具体使用只是在module下而非app框架,这样解决很可能造成引用混淆,存在不确定风险。我打算不使用该方案(删掉implementation 'androidx.constraintlayout:constraintlayout:1.1.2'),继续解决此问题。

既然编译时报错,是否app编译时用到了module的资源索引呢,因此将module中的资源引用方式修改如下:

repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
...
    implementation(name: 'constraint-layout-1.1.3', ext: 'aar')
...
}

报错找不到该资源如下:

Could not find :constraint-layout-1.1.3:.
Searched in the following locations: file:/E:/WorkSpace/ASProjects/Task/demo/app/libs/BaseRecyclerViewAdapterHelper-2.9.30.aar
Required by:
    project :app > project :module_aiview

既然这种写法找不到libs下的资源,那就直接用AS标准加载lib的方式:在File→Project Structure→...。加载完后在dependencies中的依赖方式自动添加为

implementation files('libs\\constraint-layout-1.1.3.aar')

重新编译工程,问题解决。

你可能感兴趣的:(android,gradle,java,android,studio)