Using ${applicationId} in library manifest

a 夏天 提交于 2019-12-20 11:33:11

问题


I'm working on an SDK that uses an internal ContentProvider, I would like to use this SDK in a few projects, and declare it in the library manifest, so I've tried this:

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

What happens is the ${applicationId} is replaced with the packageName of the library and not the top apk related applicationId...

Is there a way to make sure that the launching applicationId would be placed in the android:authorities value?


回答1:


Was running into the same problem with several different variants and unique IDs, and ended up going with replacing a placeholder key when Gradle is building the app, kind of like so:

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 < 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')
}

And then in the manifest:

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

That's come in handy quite a few times




回答2:


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"
 }


来源:https://stackoverflow.com/questions/30790768/using-applicationid-in-library-manifest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!