Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update and fix Gradle setup #588

Merged

Conversation

friederbluemle
Copy link
Collaborator

@friederbluemle friederbluemle commented Sep 18, 2019

Overview

Android Gradle plugin

This wraps the Android Gradle plugin dependency in the buildscripts section of android/build.gradle in a conditional:

if (project == rootProject) {
    // ... (dependency here)
}

A small change with a big impact:
The Android Gradle plugin is only required when opening the project stand-alone, not when it is included as a dependency. By doing this, the project opens correctly in Android Studio, and it can also be consumed as a native module dependency from an application project without affecting the app project (avoiding unnecessary downloads/conflicts/etc).

Gradle wrapper

Similarly, the Gradle wrapper (and related scripts) are only required for stand-alone editing/building of the android folder. It should not be distributed. Android Studio will automatically generate a wrapper, or alternatively one can be generated on the command line on-the-fly (see below).

UPDATE 2019-11-19: Due to CI changes since this PR was opened, the Gradle wrapper has temporarily been added back. It will be removed in a follow-up PR that also includes other simplifications to the CI config.

Support lib conflict

There was also an appcompat-v7 leftover in build.gradle which caused the following error in Android Studio:

ERROR: Manifest merger failed : Attribute application@appComponentFactory value=(androidx.core.app.CoreComponentFactory) from [androidx.core:core:1.0.1] AndroidManifest.xml:22:18-86
	is also present at [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91 value=(android.support.v4.app.CoreComponentFactory).
	Suggestion: add 'tools:replace="android:appComponentFactory"' to <application> element at manifestMerger6643979123070804112.xml:7:5-9:19 to override.

Since this project has already been converted to AndroidX, and React Native 0.60+ pulls in the androidx dependencies, this line is no longer needed.

.npmignore config

In a separate commit, this PR also includes a small update to the npmignore config of the project. By defining precisely which files get added to the distributed archive, along with excluding things like the Gradle wrapper jar a significant size reduction of the distribution artifact to almost a third of the previous value was achieved:

Before:

npm notice === Tarball Details ===
npm notice name:          react-native-share
npm notice version:       2.0.0
npm notice filename:      react-native-share-2.0.0.tgz
npm notice package size:  91.1 kB
npm notice unpacked size: 224.2 kB
npm notice total files:   65

After:

npm notice name:          react-native-share
npm notice version:       2.0.0
npm notice filename:      react-native-share-2.0.0.tgz
npm notice package size:  35.9 kB
npm notice unpacked size: 159.3 kB
npm notice total files:   57

Test Plan

What's required for testing (prerequisites)?

Android Studio and/or Gradle installed locally.

What are the steps to reproduce (after prerequisites)?

Open android in Android Studio:

studio android/

Or:

Generate a Gradle wrapper on the fly, and build on command line:

cd android/
gradle wrapper --gradle-version 5.4.1 --distribution-type all
./gradlew build

Successfully verified and tested on:

  • Android Device (Xiaomi Mi A2)
  • Android Emulator (Pixel 3a)
  • iOS Device (iPhone 8)
  • iOS Simulator (iPhone X)

Once approved, please use a normal GitHub merge (i.e. NO rebase/squash merge) to integrate the commit(s) from the PR head branch. The changes are broken up into meaningful, atomic commits, and my branch should already be up-to-date with the latest base branch. If it isn't, or if you want me to change anything, please let me know, and I will update the branch as soon as possible. Thank you!

Copy link
Member

@jgcmarins jgcmarins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks great!
What do you think @mikehardy?

jsconfig.json
commitlint.config.js
changelog.js
assets/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep this and not publish them to npm

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, it should not be published. Please also check the updated files section in package.json - It works in tandem with npmignore, and declares files that are included.

@friederbluemle
Copy link
Collaborator Author

Rebased onto latest master branch.

jgcmarins
jgcmarins previously approved these changes Oct 23, 2019
Copy link
Member

@jgcmarins jgcmarins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks good
it would be cool to test against our example

@friederbluemle
Copy link
Collaborator Author

@jgcmarins - Thanks for your comment and review.
I tested this locally. Can you give more instructions on what you were thinking in terms of testing?
The CI error seems unrelated:

This job has been blocked because resource-class:large is not available on your plan. Please upgrade to continue building.

@jgcmarins
Copy link
Member

I was thinking of testing against our example

The CI error seems unrelated

yes, it's unrelated

@MateusAndrade
Copy link
Collaborator

@jgcmarins do tou think you are good to go with this PR? I was seeing that the CI are broken, but this dont looks related to this

@jgcmarins
Copy link
Member

Yes
I think if it's following React Native best practices for gradle, it's ok to be merged

*/xcuserdata/
.DS_Store
.npmignore
Pods/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@friederbluemle just a question - I notice you put sub-.npmignore files in here and android and I just recently published a react-native-device-info package with accidental inclusions of stuff here, even though I attempted to npmignore them at the top level. Have you seen that before? Is that why you have layered npmignore going on in this PR?

Copy link
Collaborator Author

@friederbluemle friederbluemle Oct 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @mikehardy - Good question. The way NPM handles npmignore, gitignore, and the files field in package.json can be surprising and unintuitive. There are a bunch of special cases if one file exists but not the other, the files field is present or not, if matches are located at the root level or in a subfolder etc.
The reason I put two .npmignore files in the android/ and ios/ folder is because the very precisely ignore only files related to that platform with minimal duplication (for example, you won't find a .gradle/ in the ios/ folder).
Since android/ and ios/ are included using the files field in package.json, it is not possible to just use one .npmignore file at the root level to exclude files from those directories. Hope that makes sense.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clear as mud :-), but the tools' fault not yours as it is not easy to un-tangle how the different include/excludes work (what priority, whitelist or blacklist, when directives short-circuit other directives etc). For now in RNDI I just do a clean pre-package but I had not thought of nested npmignore files. Thanks for the explanation

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a good article that explains some of it in more detail:
https://medium.com/@jdxcode/for-the-love-of-god-dont-use-npmignore-f93c08909d8d

I also opened this one in react-native-device-info with similar changes:
react-native-device-info/react-native-device-info#872

jgcmarins
jgcmarins previously approved these changes Oct 31, 2019
Copy link
Member

@jgcmarins jgcmarins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great job here
thanks

@friederbluemle
Copy link
Collaborator Author

Rebased and updated to latest Android Gradle plugin 3.5.2.

@jgcmarins - Could you take another quick look at this and merge if you have a minute? Thanks! 🙏

jgcmarins
jgcmarins previously approved these changes Nov 13, 2019
Copy link
Member

@jgcmarins jgcmarins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great

url 'https://maven.google.com'
// The Android Gradle plugin is only required when opening the android folder stand-alone.
// This avoids unnecessary downloads and potential conflicts when the library is included as a
// module dependency in an application project.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@friederbluemle this is not really the place to mention it but this just came through and reminded me - the one place I've seen this style fail is react-native-webview. It depends on kotlin, and that has to be on the classpath or the module build breaks a project build, but most projects (? my project anyway) does not by default have kotlin yet. So until (if?) the react-native template is altered to include kotlin not all modules may be de-duplicated like this. To be clear: not related to this PR, just for information

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the heads-up @mikehardy 👌

@friederbluemle
Copy link
Collaborator Author

@jgcmarins - Rebased again onto the latest master branch. CI is green again too.
I would really appreciate a merge soon. Thanks!

Copy link
Member

@jgcmarins jgcmarins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great job, thanks

@jgcmarins jgcmarins merged commit c7287a3 into react-native-share:master Nov 20, 2019
@jgcmarins
Copy link
Member

@friederbluemle should we upgrade example as well?

@friederbluemle friederbluemle deleted the update-gradle-setup branch November 20, 2019 18:46
@friederbluemle
Copy link
Collaborator Author

@jgcmarins Thanks for merging! 👍
Yes, latest react-native for example/ here: #637 😄

cuonghuynhpycogroup pushed a commit to cuonghuynhpycogroup/react-native-share that referenced this pull request Dec 25, 2019
* master-base: (215 commits)
  Update react-native to 0.61.4 (react-native-share#637)
  Update and fix Gradle setup (react-native-share#588)
  set useNativeDriver explicitly for Animations (react-native-share#633)
  correctly type image source (react-native-share#632)
  ci(fix): adapt to new workflow (react-native-share#626)
  fix: added missing "v" to `source` field in podspec (react-native-share#619)
  chore(readme): mocking with Jest example (react-native-share#610)
  iOS 13: Resolve promise if ShareSheet is manually dismissed (react-native-share#607)
  Update README.md
  fix: remove uncessary tools replace on build gradle
  chore(lint): fixing lint errors
  Makes sharing with Snapchat work on Android (react-native-share#464)
  Fixed validate error
  Removed react-native Share dependency
  Only call successCallback when completed is true
  Change readme.md
  Update Gradle wrapper to 5.6.2
  build(deps): bump eslint-utils from 1.4.0 to 1.4.2 (react-native-share#580)
  Send email to predefined email address - Example
  Send email to predefined email address
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants