我已经在AndroidManifest.xml
上添加了ACCESS_FINE_LOCATION
权限在调试模式下运行时我没有问题。
但是,当我使用react-native run-android --variant=release
进行释放模式测试时,它会成功构建,但是当我输入具有Geolocation.watchPosition
应用程序崩溃的屏幕时,即使我获得了许可。
这是我的脚本PosisiMarker.js
:
import React, {Component} from 'react';
import {Platform, Alert} from 'react-native';
import {Marker, AnimatedRegion} from 'react-native-maps'
import EventEmitter from 'react-native-eventemitter';
import {radius} from '../../../config'
import RNExitApp from 'react-native-exit-app'
import Permissions from 'react-native-permissions'
import Geolocation from 'react-native-geolocation-service';
export default class PosisiMarker extends Component {
constructor (props) {
super(props);
this.state = {
allowPresence: false,
coordinate: new AnimatedRegion({
latitude: 0,
longitude: 0,
}),
alert: false,
}
}
componentDidMount () {
this.checkPresenceLocation();
}
componentWillUnmount () {
console.log('clearing');
Geolocation.clearWatch(this.watchId);
Geolocation.stopObserving();
}
checkMocked (position) {
if (position.mocked && !this.state.alert) {
this.setState({alert: true});
Alert.alert(
'Peringatan!',
'Anda dicurigai menggunakan aplikasi pemalsu lokasi (FakeGPS). Anda tidak diperkenankan melakukan absence! Aktifitas mencurigakan in akan kami simple!',
[
{text: 'Keluar', onPress: () => RNExitApp.exitApp()},
],
{cancelable: false},
);
}
}
checkPresenceLocation = () => {
console.log('watching position');
this.watchId = Geolocation.watchPosition(
(position) => {
console.log('position', position);
this.checkMocked(position)
const { coordinate } = this.state;
const newCoordinate = {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
};
if (Platform.OS === 'android') {
if (this.marker) {
this.marker._component.animateMarkerToCoordinate(newCoordinate, 500);
}
} else {
coordinate.timing(newCoordinate).start();
}
// this.setState({
// latitude: position.coords.latitude,
// longitude: position.coords.longitude,
// error: null,
// });
let latLng = {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
}
let distance = geolib.getDistance(latLng, {
latitude: this.props.location.lat,
longitude: this.props.location.lang,
})
if (distance <= radius) {
if (!this.state.allowPresence) {
this.setState({allowPresence: true})
this.emitPresence()
}
} else {
if (this.state.allowPresence) {
this.setState({allowPresence: false})
this.emitPresence()
}
}
},
(error) => this.setState({ error: error.message }),
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000, distanceFilter: 10 })
}
emitPresence = () => {
EventEmitter.emit('TOGGLE_PRESENCE', this.state.allowPresence);
}
emitCurrentPos = (position) => {
EventEmitter.emit('CURRENT_POSITION', position);
}
render () {
return (
<Marker.Animated ref={(marker) => this.marker=marker} coordinate={this.state.coordinate} style={{zIndex: 3}}/>
)
// if (this.state.allowPresence) {
//
// } else {
// return null;
// }
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="id.go.gorontaloprov.siransija">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:allowBackup="false"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyCzCN6dFqu09uIz1FTpzeOKgbY0nvi4Byk"/>
</application>
</manifest>
我已经解决了这个问题,只需在 android/app/build.gradle
Proguard
...
/**
* Run Proguard to shrink the Java bytecode in release builds.
*/
def enableProguardInReleaseBuilds = false