React Native for Android uses Gradle for builds. For some content to be packaged correctly for Android, it must be placed in the android/src/main/assets/
directory. If you are building a project for both iOS and Android, you may need to have duplicate assets in the required locations for each platform.
I chose to put my primary assets in the iOS or app directories and copy them on build to the locations for android. Below is an example of how I was able to do this.
In android/app/build.gradle
I added a new copy task at the bottom of the file:
task copyHtmlFiles(type: Copy) {
from file("../../app/content/legal-notice.html")
from file("../../app/content/privacy-policy.html")
into file("./src/main/assets/www/")
}
preBuild.dependsOn(copyHtmlFiles)
This task runs before build and will copy both HTML files in the app/content/
directory to the android/src/main/assets/www/
directory. The file paths are relative to the location of the build.gradle
file.
Now the local HTML file can be loaded by react-native-webview
in both iOS and Android with the following:
<WebView
source={
Platform.OS === "ios"
? require("../../content/privacy-policy.html")
: { uri: "file:///android_asset/www/privacy-policy.html" }
}
originWhitelist={["*"]}
/>