From 0fdabd3e43226f6743faae9772001fdca1743818 Mon Sep 17 00:00:00 2001 From: Yogev Ben David Date: Sat, 14 Sep 2019 22:28:33 +0300 Subject: [PATCH] React Native 0.60 Support (#375) * Add react-native@0.60.* support * Change variant name * Update installation docs * Fix example project * Update installation.md --- android/app/build.gradle | 13 ++++ .../RNNotificationsModule.java | 3 +- .../core/InitialNotificationHolder.java | 2 - .../NotificationManagerCompatFacade.java | 12 ++++ .../NotificationManagerCompatFacade.java | 12 ++++ docs/installation.md | 65 +++++++++++++++++++ example/android/gradle.properties | 5 +- example/android/myapplication/build.gradle | 19 +++--- .../app/MainActivity.java | 33 +--------- .../app/MainApplication.java | 6 ++ .../src/main/res/layout/activity_main.xml | 24 ------- .../res/layout/activity_main_prelollipop.xml | 10 --- package.json | 2 +- scripts/test-unit.js | 2 +- 14 files changed, 125 insertions(+), 83 deletions(-) create mode 100644 android/app/src/reactNative59/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java create mode 100644 android/app/src/reactNative60/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java delete mode 100644 example/android/myapplication/src/main/res/layout/activity_main.xml delete mode 100644 example/android/myapplication/src/main/res/layout/activity_main_prelollipop.xml diff --git a/android/app/build.gradle b/android/app/build.gradle index 9ea834b..c707029 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -16,6 +16,9 @@ android { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } + debug { + debuggable true + } } testOptions { unitTests.all { t -> @@ -36,6 +39,16 @@ android { } } } + + flavorDimensions "RNNotifications.reactNativeVersion" + productFlavors { + reactNative59 { + dimension "RNNotifications.reactNativeVersion" + } + reactNative60 { + dimension "RNNotifications.reactNativeVersion" + } + } } dependencies { diff --git a/android/app/src/main/java/com/wix/reactnativenotifications/RNNotificationsModule.java b/android/app/src/main/java/com/wix/reactnativenotifications/RNNotificationsModule.java index 61dddec..4ac8807 100644 --- a/android/app/src/main/java/com/wix/reactnativenotifications/RNNotificationsModule.java +++ b/android/app/src/main/java/com/wix/reactnativenotifications/RNNotificationsModule.java @@ -5,7 +5,6 @@ import android.app.Application; import android.content.Context; import android.content.Intent; import android.os.Bundle; -import android.support.v4.app.NotificationManagerCompat; import android.util.Log; import com.facebook.react.bridge.ActivityEventListener; @@ -108,7 +107,7 @@ public class RNNotificationsModule extends ReactContextBaseJavaModule implements @ReactMethod public void isRegisteredForRemoteNotifications(Promise promise) { - boolean hasPermission = NotificationManagerCompat.from(getReactApplicationContext()).areNotificationsEnabled(); + boolean hasPermission = NotificationManagerCompatFacade.from(getReactApplicationContext()).areNotificationsEnabled(); promise.resolve(new Boolean(hasPermission)); } diff --git a/android/app/src/main/java/com/wix/reactnativenotifications/core/InitialNotificationHolder.java b/android/app/src/main/java/com/wix/reactnativenotifications/core/InitialNotificationHolder.java index 3636f41..948d80e 100644 --- a/android/app/src/main/java/com/wix/reactnativenotifications/core/InitialNotificationHolder.java +++ b/android/app/src/main/java/com/wix/reactnativenotifications/core/InitialNotificationHolder.java @@ -1,6 +1,5 @@ package com.wix.reactnativenotifications.core; -import android.support.annotation.Nullable; import com.wix.reactnativenotifications.core.notification.PushNotificationProps; @@ -32,7 +31,6 @@ public class InitialNotificationHolder { mNotification = null; } - @Nullable public PushNotificationProps get() { return mNotification; } diff --git a/android/app/src/reactNative59/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java b/android/app/src/reactNative59/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java new file mode 100644 index 0000000..f527a5d --- /dev/null +++ b/android/app/src/reactNative59/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java @@ -0,0 +1,12 @@ + +package com.wix.reactnativenotifications; + +import android.content.Context; +import android.support.annotation.Nullable; +import android.support.v4.app.NotificationManagerCompat; + +public abstract class NotificationManagerCompatFacade { + public static NotificationManagerCompat from(@NonNull Context context) { + return NotificationManagerCompat.from(context); + } +} diff --git a/android/app/src/reactNative60/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java b/android/app/src/reactNative60/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java new file mode 100644 index 0000000..94ea188 --- /dev/null +++ b/android/app/src/reactNative60/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java @@ -0,0 +1,12 @@ + +package com.wix.reactnativenotifications; + +import android.content.Context; +import androidx.annotation.NonNull; +import androidx.core.app.NotificationManagerCompat; + +public abstract class NotificationManagerCompatFacade { + public static NotificationManagerCompat from(@NonNull Context context) { + return NotificationManagerCompat.from(context); + } +} diff --git a/docs/installation.md b/docs/installation.md index fea49f8..eee414a 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -119,3 +119,68 @@ dependencies { apply plugin: 'com.google.gms.google-services' ``` + +#### Step #5: RNNotifications and React Native version +This step is required only for `react-native-notifications` version `3.1.0` and above.
+ +react-native-notifications supports multiple React Native versions. Target the React Native version required by your project by specifying the RNN build flavor in `android/app/build.gradle`. + +```diff +android { + ... + defaultConfig { + applicationId "com.yourproject" + minSdkVersion rootProject.ext.minSdkVersion + targetSdkVersion rootProject.ext.targetSdkVersion ++ missingDimensionStrategy "RNNotifications.reactNativeVersion", "reactNative60" // See note below! + versionCode 1 + versionName "1.0" + ... + } + ... +} +``` + +!>Important note about `missingDimensionStrategy`
+>`reactNative59` - RN 0.59.x and below
+>`reactNative60` - RN 0.60.0 and above + +Now we need to instruct gradle how to build that flavor. To do so here two solutions: + +#### 5.1 Build app with gradle command + +**prefered solution** The RNNotification flavor you would like to build is specified in `app/build.gradle`. Therefore in order to compile only that flavor, instead of building your entire project using `./gradlew assembleDebug`, you should instruct gradle to build the app module: `./gradlew app:assembleDebug`. The easiest way is to add a package.json command to build and install your debug Android APK . + +``` +"scripts": { + ... + "android": "cd ./android && ./gradlew app:assembleDebug && ./gradlew installDebug" +} +``` + +Now run `npm run android` to build your application + +#### 5.2 Ignore other RNN flavors + +If you don't want to run `npm run android` and want to keep the default `react-native run-android` command, you need to specify to graddle to ignore the other flavors RNNotifications provides. + +To do so edit `android/build.gradle` and add: + +```diff ++subprojects { subproject -> ++ afterEvaluate { ++ if ((subproject.plugins.hasPlugin('android') || subproject.plugins.hasPlugin('android-library'))) { ++ android { ++ variantFilter { variant -> ++ def names = variant.flavors*.name ++ if (names.contains("reactNative59")) { ++ setIgnore(true) ++ } ++ } ++ } ++ } ++ } ++} +``` + +**Note**: As more build variants come available in the future, you will need to adjust the list (`names.contains("reactNative59")`). This is why we recommend the first solution. \ No newline at end of file diff --git a/example/android/gradle.properties b/example/android/gradle.properties index 1d3591c..ccb748f 100644 --- a/example/android/gradle.properties +++ b/example/android/gradle.properties @@ -15,4 +15,7 @@ # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects -# org.gradle.parallel=true \ No newline at end of file +# org.gradle.parallel=true + +android.useAndroidX=true +android.enableJetifier=true \ No newline at end of file diff --git a/example/android/myapplication/build.gradle b/example/android/myapplication/build.gradle index b354d5a..8fe1c83 100644 --- a/example/android/myapplication/build.gradle +++ b/example/android/myapplication/build.gradle @@ -24,6 +24,7 @@ android { ndk { abiFilters "armeabi-v7a", "x86" } + missingDimensionStrategy "RNNotifications.reactNativeVersion", "reactNative60" } buildTypes { release { @@ -35,17 +36,13 @@ android { sourceCompatibility 1.8 targetCompatibility 1.8 } -} - -configurations.all { - resolutionStrategy.eachDependency { DependencyResolveDetails details -> - def requested = details.requested - if (requested.group == 'com.android.support') { - details.useVersion "28.0.0" - } + packagingOptions { + pickFirst '**/libjsc.so' + pickFirst '**/libc++_shared.so' } } + configurations.all { resolutionStrategy { force 'org.webkit:android-jsc:r236355' @@ -53,12 +50,12 @@ configurations.all { } dependencies { -// compile fileTree(dir: 'libs', include: ['*.jar']) + implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.google.firebase:firebase-core:16.0.0' - implementation 'com.android.support:appcompat-v7:28.0.0' - implementation 'com.android.support:design:28.0.0' + implementation 'androidx.appcompat:appcompat:1.0.2' implementation 'com.facebook.react:react-native:+' + implementation 'org.webkit:android-jsc-intl:+' implementation project(':react-native-notifications') testImplementation'junit:junit:4.12' diff --git a/example/android/myapplication/src/main/java/com/wix/reactnativenotifications/app/MainActivity.java b/example/android/myapplication/src/main/java/com/wix/reactnativenotifications/app/MainActivity.java index 4459a6a..48f4dab 100644 --- a/example/android/myapplication/src/main/java/com/wix/reactnativenotifications/app/MainActivity.java +++ b/example/android/myapplication/src/main/java/com/wix/reactnativenotifications/app/MainActivity.java @@ -1,40 +1,11 @@ package com.wix.reactnativenotifications.app; -import android.os.Build; -import android.os.Bundle; -import android.view.ViewGroup; -import android.widget.Toolbar; - import com.facebook.react.ReactActivity; -import com.facebook.react.ReactRootView; -import static android.os.Build.VERSION.SDK_INT; public class MainActivity extends ReactActivity { - - private ReactRootView mReactRootView; - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - ViewGroup layout; - if (SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - layout = (ViewGroup) getLayoutInflater().inflate(R.layout.activity_main, null); - Toolbar toolbar = layout.findViewById(R.id.toolbar); - setActionBar(toolbar); - } else { - layout = (ViewGroup) getLayoutInflater().inflate(R.layout.activity_main_prelollipop, null); - } - mReactRootView = new ReactRootView(this); - layout.addView(mReactRootView); - - setContentView(layout); - - startReactApplication(); - } - - private void startReactApplication() { - mReactRootView.startReactApplication(getReactInstanceManager(), "WixRNNotifications", null); + protected String getMainComponentName() { + return "WixRNNotifications"; } } diff --git a/example/android/myapplication/src/main/java/com/wix/reactnativenotifications/app/MainApplication.java b/example/android/myapplication/src/main/java/com/wix/reactnativenotifications/app/MainApplication.java index 87b0d3a..110f09a 100644 --- a/example/android/myapplication/src/main/java/com/wix/reactnativenotifications/app/MainApplication.java +++ b/example/android/myapplication/src/main/java/com/wix/reactnativenotifications/app/MainApplication.java @@ -6,12 +6,18 @@ import com.facebook.react.ReactApplication; import com.facebook.react.ReactNativeHost; import com.facebook.react.ReactPackage; import com.facebook.react.shell.MainReactPackage; +import com.facebook.soloader.SoLoader; import com.wix.reactnativenotifications.RNNotificationsPackage; import java.util.Arrays; import java.util.List; public class MainApplication extends Application implements ReactApplication { + @Override + public void onCreate() { + super.onCreate(); + SoLoader.init(this, false); + } private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) { @Override diff --git a/example/android/myapplication/src/main/res/layout/activity_main.xml b/example/android/myapplication/src/main/res/layout/activity_main.xml deleted file mode 100644 index 3f51779..0000000 --- a/example/android/myapplication/src/main/res/layout/activity_main.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - diff --git a/example/android/myapplication/src/main/res/layout/activity_main_prelollipop.xml b/example/android/myapplication/src/main/res/layout/activity_main_prelollipop.xml deleted file mode 100644 index 75a78a5..0000000 --- a/example/android/myapplication/src/main/res/layout/activity_main_prelollipop.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - diff --git a/package.json b/package.json index 780e2bc..a84764e 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,7 @@ "sinon": "^1.17.3", "sinon-chai": "^2.8.0", "shell-utils": "1.x.x", - "react-native": "0.59.5", + "react-native": "0.60.5", "react": "16.8.6", "detox": "13.x.x", "jsc-android": "236355.x.x", diff --git a/scripts/test-unit.js b/scripts/test-unit.js index dcc8f88..01b85f6 100644 --- a/scripts/test-unit.js +++ b/scripts/test-unit.js @@ -13,7 +13,7 @@ function run() { } function runAndroidUnitTests() { - const conf = release ? 'testReleaseUnitTest' : 'testDebugUnitTest'; + const conf = release ? 'testReactNative60ReleaseUnitTest' : 'testReactNative60DebugUnitTest'; if (android && process.env.JENKINS_CI) { const sdkmanager = '/usr/local/share/android-sdk/tools/bin/sdkmanager'; exec.execSync(`yes | ${sdkmanager} --licenses`); -- 2.26.2