mauron85 / react-native-background-geolocation

Background and foreground geolocation plugin for React Native. Tracks user when app is running in background.
Apache License 2.0
1.33k stars 559 forks source link

Fail to build on Android with RN 0.64.1 #542

Closed zoolle closed 3 years ago

zoolle commented 3 years ago

On iOS everything works fine but while I am trying to build in Android following the documentation I get the following

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/User/Desktop/testApp/node_modules/@mauron85/react-native-background-geolocation/android/common/build.gradle' line: 1

* What went wrong:
A problem occurred evaluating project ':@mauron85_react-native-background-geolocation-common'.
> Failed to apply plugin 'com.android.internal.library'.
   > The option 'android.enableUnitTestBinaryResources' is deprecated.
     The current default is 'false'.
     It has been removed from the current version of the Android Gradle plugin.
     The raw resource for unit test functionality is removed.

Your Environment

Context

In android/settings.gradle

rootProject.name = 'testApp'
apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)
include ':@mauron85_react-native-background-geolocation-common'
project(':@mauron85_react-native-background-geolocation-common').projectDir = new File(rootProject.projectDir, '../node_modules/@mauron85/react-native-background-geolocation/android/common')
include ':@mauron85_react-native-background-geolocation'
project(':@mauron85_react-native-background-geolocation').projectDir = new File(rootProject.projectDir, '../node_modules/@mauron85/react-native-background-geolocation/android/lib')
include ':app'

In android/app/build.gradle

dependencies {
      implementation project(':@mauron85_react-native-background-geolocation')
}

in MainApplication.java

 @Override
    protected List<ReactPackage> getPackages() {
      @SuppressWarnings("UnnecessaryLocalVariable")
      List<ReactPackage> packages = new PackageList(this).getPackages();
      // Packages that cannot be autolinked yet can be added manually here, for
      // example:
      // packages.add(new MyReactNativePackage());
      packages.add(new RNReactNativeHapticFeedbackPackage());
      packages.add(new BackgroundGeolocationPackage());
      return packages;
}

Expected Behavior

Actual Behavior

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/User/Desktop/testApp/node_modules/@mauron85/react-native-background-geolocation/android/common/build.gradle' line: 1

* What went wrong:
A problem occurred evaluating project ':@mauron85_react-native-background-geolocation-common'.
> Failed to apply plugin 'com.android.internal.library'.
   > The option 'android.enableUnitTestBinaryResources' is deprecated.
     The current default is 'false'.
     It has been removed from the current version of the Android Gradle plugin.
     The raw resource for unit test functionality is removed.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 5s

error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup.
Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/User/Desktop/testApp/node_modules/@mauron85/react-native-background-geolocation/android/common/build.gradle' line: 1

* What went wrong:
A problem occurred evaluating project ':@mauron85_react-native-background-geolocation-common'.
> Failed to apply plugin 'com.android.internal.library'.
   > The option 'android.enableUnitTestBinaryResources' is deprecated.
     The current default is 'false'.
     It has been removed from the current version of the Android Gradle plugin.
     The raw resource for unit test functionality is removed.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
quixote15 commented 3 years ago

@zoolle I was experiencing the same behavior when upgrading my app from rn0.61.0 to rn0.64.1

After some hours I've found here that From Android studio 3.3+ there is no need to add the code below to "gradle.properties" file.

android.enableUnitTestBinaryResources=true

So, I looked on the lib files and found that @mauron85/react-native-background-geolocation/android/common/gradle.properties file has the following content:

android.enableUnitTestBinaryResources=true # our little monster breaking things here
android.builder.sdkDownload=true

To see if the error would go away I removed android.enableUnitTestBinaryResources=true from the lib file on node_modules/@mauron85/react-native-background-geolocation/android/common/gradle.properties and it worked!

I tried to fork and open a PR with that fix but got lost in how to build, test and contribute. There is no clear documentation of the process.

So, I wrote a bash script to remove that deprecated code:

# mauronAndroidFix.sh
# utility variables
gradlePropertiesWithDeprecatedOptionFile="node_modules/@mauron85/react-native-background-geolocation/android/common/gradle.properties"
deprecatedOptionOnNewerAndroidGradle="android.enableUnitTestBinaryResources=true"
newGradleContent="android.builder.sdkDownload=true"

echo "@mauron85/react-native-background-geolocation"
echo "starting hotfix script to correct Fail to build on Android with RN 0.64.1"

# Test if the deprecated command is still on that file
# in future releases this may be fixed and then the script does nothing
if grep -q 'android.enableUnitTestBinaryResources=true' "$gradlePropertiesWithDeprecatedOptionFile"; then
  echo "The option 'android.enableUnitTestBinaryResources' is deprecated"
  echo "It has been removed from the current version of the Android Gradle plugin"
  echo "Removing deprecated instruction..."

  # remove the deprecated option from commom/gradle.properties
  echo $newGradleContent > $gradlePropertiesWithDeprecatedOptionFile
  exit 0;
fi

Then, on package.json set the script to run every time someone installs the projects dependencies:

// In package.json define postinstall
...
"scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint .",
    "mauron-postinstall": "./mauronAndroidFix.sh", // add this line
    "postinstall": "yarn mauron-postinstall", // and this line
    "reset-cache": "react-native start --reset-cache"
  }
...

I hope this helps! I would be nice if someone releases this fix or write some docs on how to contribute.

AliReza-Kamkar commented 3 years ago

Thanks @quixote15 Your script can fix the problem. I hope maintainers remove deprecated codes and make a new release.

zoolle commented 3 years ago

@quixote15 You are a legend. It's working perfectly. I will close this now and hope someone will make an update in a new version