在库清单中使用$ {applicationId}

时间:2015-06-11 20:26:12

标签: android gradle manifest-merging

我正在使用内部ContentProvider的SDK,我想在几个项目中使用这个SDK,并在库清单中声明它,所以我试过这个:

    <provider
        android:name=".core.MyContentProvider"
        android:authorities="${applicationId}"
        android:exported="false"/>

$ {applicationId} 会被库的packageName替换,而不是与apk相关的顶级applicationId ......

有没有办法确保将启动 applicationId放在 android:authority 值中?

2 个答案:

答案 0 :(得分:8)

遇到了几个不同变体和唯一ID的相同问题,最后在Gradle构建应用时更换占位符键,有点像这样:

Gradle 3 +

android.applicationVariants.all { variant ->
    variant.outputs.each { output ->
        output.processManifest.doLast {
            File manifestFile = file("$manifestOutputDirectory/AndroidManifest.xml")

            replaceInFile(manifestFile, 'P_AUTHORITY', variant.applicationId)
        }
    }
}

def replaceInFile(file, fromString, toString) {
    def updatedContent = file.getText('UTF-8')
            .replaceAll(fromString, toString)

    file.write(updatedContent, 'UTF-8')
}

Gradle&lt; 3

android.applicationVariants.all { variant ->
    variant.outputs.each { output ->
        output.processManifest.doLast{
            replaceInManifest(output, 'P_AUTHORITY', variant.applicationId)
        }
    }   
}

def replaceInManifest(output, fromString, toString) {
    def manifestOutFile = output.processManifest.manifestOutputFile
    def updatedContent = manifestOutFile.getText('UTF-8').replaceAll(fromString, toString)
    manifestOutFile.write(updatedContent, 'UTF-8')
}

然后在清单中:

<provider
    android:name=".core.MyContentProvider"
    android:authorities="P_AUTHORITY"
    android:exported="false"/>

这很有用了很多次

答案 1 :(得分:0)

您可以在清单文件下使用 $ {applicationId} 。只需确保该库的gradle文件中没有“ applicationId ”即可。如果您在gradle文件的“ defaultConfig ”下声明了该文件,请删除它。

//So your gradle file of library(SDK) module looks like..

defaultConfig {
minSdkVersion Version.minSdk
targetSdkVersion Version.targetSdk
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
} 


//And your application gradle file looks like..

defaultConfig {
    applicationId "com.example.android"
    minSdkVersion Version.minSdk
    targetSdkVersion Version.targetSdk
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
 }