DEV Community

M Julius
M Julius

Posted on

Simple Execute Shell Android In React Native

Hello Bruh, execute the shell is in my opinion very important, to find out detailed information on android.

Getting Started

$ npx react-native init nameYourProject
Enter fullscreen mode Exit fullscreen mode
$ cd nameYourProject
Enter fullscreen mode Exit fullscreen mode
$ npm install react-native-android-shell --save
Enter fullscreen mode Exit fullscreen mode

Edit App.js file
replace code, using within code

Example

import React, { Component } from 'react';
import { View, Text } from 'react-native';
import AndroidShell from 'react-native-android-shell'

class demo extends Component {
    constructor(props) {
        super(props);
        this.state = {
            result: ''
        };
    }

    componentDidMount() {
        AndroidShell.executeCommand("ls", (result) => {
            this.setState({ result: 'yeu : ' + result });
            console.log('Result :', result);
        });
    }

    render() {
        return (
            <View style={{ alignItems: 'center', flex: 1 }}>
                <Text>{this.state.result}</Text>
            </View>
        );
    }
}

export default demo;
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
baldismaila2 profile image
ismaila Baldé • Edited

hello Julius thankx for your post.
i got this error when i try your exemple.

null is not an object (evaluating '_reactNativeAndroidShell.default.executeCommand')

i think that executeCommand method not exist

can you help ?
thankx

Collapse
 
amanagr843 profile image
amanagr843

how to use commands like "pkg install "

Collapse
 
juldev profile image
M Julius

you need root, and install proot package for super user