How can I generate an AndroidManifest.xml from an existing react-native project?

Viewed 6098

I already have a react-native app created and I wanna access the user's geolocation. Well, I don't have any Android's folder and I need to add some permissions in AndroidManifest.xml. How can I tie this into my existing project?

Obs: I am using expo and npm.

4 Answers

AndroidManifest.xml not possible with an Expo project. In order to add this permission, you would have to use the Bare workflow

If not you can eject your project from expo or create a new project with react-native-cli and move your source code to it.

Hope this helps you. Feel free for doubts.

The below codes worked for me!

state = {
  isLoading:false,
  location:null,
  errorMessage:null,
  isDevice:"",
}

async componentDidMount() {
  if (Platform.OS === 'android' && !Constants.isDevice) {
    this.setState({
      errorMessage:"oops, this will not work on sketch in an android emulator.Try it on your device!"
    });
  } else {
    this._getLocationAsync();
  }
}

_getLocationAsync = async () => {
  let { status} = await Permissions.askAsync(Permissions.LOCATION);
  if (status !== 'granted') {
    this.setState({
      setError:'Permission to access location was denied'
    });
    
  }
  let location = await Location.getCurrentPositionAsync({});
  this.setState({
    location
  })
  
  console.log(location)

}

It is possible to edit AndroidManifest.xml with apktool, followed by using apksigner to re-sign the apk with credentials from expo credentials:manager.

To edit the existing manifest:

apktool decode myapp-original.apk
vim myapp-original/AndroidManifest.xml

To re-build a new signed apk:

apktool build myapp-original -o myapp.apk
expo credentials:manager -p android
/path/toAndroid/Sdk/build-tools/$VERSION/apksigner sign -ks @username__myapp.bak.jks myapp.apk

Expo has evolved quite a bit in recent years and you can now (Expo SDK 41+) use config plugins to change your AndroidManifest.xml and still remain in the managed workflow. You will lose the ability to use the Expo Go App, but you can use EAS builds to build a custom dev client, which is basically a version of Expo Go tailored to your project.

The docs provide an example of how to use the withAndroidManifest mod to modify permissions. Note that you can run expo prebuild --no-install to test the effects of your plugin, but be sure to delete the generated native folders before running the EAS build, otherwise EAS will assume bare workflow.

Related