28

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

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

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

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

4

2 回答 2

10

在使用几个不同的变体和唯一 ID 时遇到了同样的问题,最终在 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')
}

梯度 < 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"/>

好几次都派上用场了

于 2015-06-11T20:53:03.713 回答
7

You can use ${applicationId} in under manifest file. Just make sure that in your gradle file of that library doesn't have "applicationId". If you declared it in your gradle file under "defaultConfig", please remove it.

//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"
 }
于 2019-03-30T07:41:43.063 回答