Using 3rd Party AAR in React-Native Android App - react-native

I am attempting to build a React-Native Android app that utilizes functionality provided by a 3rd party's AAR but am unclear as to both the process in general as well as the required specifics.
I have successfully built an Android Studio app using this AAR.
And, I have scoured the documentation here (and postings elsewhere): https://facebook.github.io/react-native/docs/tutorial but remain unclear as to the correct process.
My initial attempts have centered around trying to find the correct "implementation" and/or "compile" commands in the gradle files coupled with the correct "import" statement(s) in the source file(s).
I had assumed getting those correct would afford access to the classes in the AAR, but after numerous unsuccessful attempts am now wondering if there is something more that needs done.
For example, do I need to construct a bridge module as described here: https://medium.com/#yushulx/react-native-bridging-modules-for-android-from-scratch-c651eeee7872 ?
Based on the few questions/answers I found on this topic, I have placed this code in the "allprojects" section of my top-level build.gradle file:
flatDir {
dirs 'libs'}
}
...and have included the following in the "dependencies" section of the build.gradle file:
fileTree(dir: 'libs', include: '**/*.aar')
.each { File file ->
dependencies.add("compile", [
name: file.name.lastIndexOf('.').with { it != -1 ? file.name[0..<it] : file.name },
ext: 'aar'])
}
...and have included various forms of "import" statements in my app.js file, like:
import {EENMediaPlayerListener} from 'EENSDK';...
When I execute "react-native run-android" I receive the following error message:
error: bundling failed: Error: Unable to resolve module `EENSDK` from `C:\Users\Chuck\EE\App.js`: Module `EENSDK` does not exist in the Haste module map
EENSDK.aar is located in the 'C:\Users\Chuck\EE\android\app\libs' directory.
If I remove the "import" statement, it compiles without errors.
So, do I just have the "compile", "implementation", and "import" statements wrong, or is there something more (and if so, where can I read about that)?

Related

javascript as target in kotlin multiplatform library

I'm building a Kotlin multiplatform library. One of the targets in this project is javascript. In the source set I have added a dependency like this:
val jsMain by getting {
dependencies {
implementation(npm("libphonenumber-js", "1.10.13"))
}
}
The gradle sync was successful, now I want to import the files in jsMain directory. How can I achieve this?
Add npm dependency with generateExternals as you already did
implementation(npm("libphonenumber-js", "1.10.13", generateExternals = true))
generateExternals = true triggers a tool called Dukat that generates Kotlin external declarations from the Typescript definition file of that npm module.
Once your project syncs, it would have externals folder under your shared module's build folder like shown below,
(Ignore kmp-lib-1621 in above image. That would be your module/library name instead)`
Now copy and paste these files in your project (jsMain source set) and remove generateExternal = true from your dependency otherwise it would generate this files everytime. (1) you would lose any manual change (2) if you update the library version then it can potentially break your project
You should be able to call generated external code from Kotlin code, whether you keep it in build folder or you pasted it in your project code.
Important Note: Dukat tool is experiemental and known to create externals that may not work 100% times. So remove all unnecessary code from external generated code so you only end up having few references of classes you want to use from the npm library. Do some trial and error and you would be fine.
Hope this helps!
You have to use js(IR) backend and generate externals
implementation(npm("libphonenumber-js", "1.10.13", generateExternals = true)).

IntelliJ gradle - unknown properties

The error:
Build file 'C:\Users\Me\Dev\project\app\build.gradle' line: 21
What went wrong:
A problem occurred evaluating root project 'app'.
Could not get unknown property 'libraries' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
inside app.gradle
dependencies{
compile libraries.my_lib
deploy libraries.my_lib
}
inside project.gradle
ext.libraries = {
my_lib: 'com.myCompany:my-lib:1.0.0'
}
inside pom.xml
<modules>
<module>my-lib</modules>
</modules>
Pressing CTRL+Space in app.gradle's libaries. shows the my_lib library that may be autocompleted, but when compiling using gradle clean deploy, it fails and returns the message above.
This only happened after I updated the IDE to the latest IntelliJ.
ext defines extensions only for a project it was called in. In your case, libraries.my_lib is defined in project.gradle (I guess, it is build.gradle in project directory) and, thus, is not available in a different build file (app.gradle, which is again, I guess, is a build.gradle in app directory)) unless project is not a parent of app. Child projects have access to parent's properties, and that is the only way to share ext: projects should be related to each other.
BTW, you can define properties in gradle.properties file. There may be many of them, one for every project in your build. Placing one gradle.properties in the root of your build you apply its content to the root of your project tree, thus, making the properties available everywhere (the same applies to ext block of a root object, as it follows from the previous paragraph).

How to test files that use browserify-shim global's in Jest?

I'm downloading the google maps API v3 via a script tag, and I'm adding the dependency to my modules with the following (relevant) package.json configuration:
"browserify-shim": {
"google": "global:google"
}
And I can add the dependency in my files with the following:
var google = require('google');
When I run my code in the browser, it works fine.
The problem is, when I run my tests with Jest, it tells me that it can't find the 'google' module:
Error: /src/app/assets/javascripts/__tests__/helpers-test.js: Cannot find module 'google' from '/src/app/assets/javascripts/__tests__'
Note:
This dependency is being required in the file that I'm testing, not the test itself. I find this confusing since I thought that Jest mocks all dependencies unless it is specified otherwise, but from what I can see, it first needs to correctly satisfy the dependencies before mocking.
Any ideas of what am I missing or what approach should I take?
You'll need to alias 'google' properly in your package.json, see here.

Android Gradle Dependency

I am using local aar files for one of our projects and have below Query.
I have 2 libraries and 1 application.
2 libraries are:
1. TestLib2
2. TestLib1
1 Application is:
1. Test
I use a aar file created for TestLib2 and refer it using flatDir in TestLib1. I can access the functions present in TestLib2 without any problems.
Now I use a aar file created for TestLib1 and refer it using flatDir in Test. I can access only the functions present in TestLib1. For accessing TestLib2 i have to add it to Test application as one more Library.
So the dependency is like below:
Test
|_ TestLib1
|_ TestLib2
Is the above possible in case of aar files?
Also in settings.gradle file for TestLib1 i mention to include
include ':app', ':testlib2-debug'
Where app refers to the TestLib1
The build.gradle file doesnt really have any flavors as such and i dont even have any restriction of using them as jar's since its containing only the java piece of code.
Any help on the same is much appreciated.
BR,
Jayshil
Update 1:
I tried below as well in build.gradle of TestLib1 and Test.
Still no luck.
dependencies {
compile (name:'testlib2-debug', ext:'aar') {
transitive = true;
}
}
And for Test App
compile (name:'testlib1-debug', ext:'aar') {
transitive = true;
}
So i finally figured out a solution for this.
It works for 2 level dependency mentioned above.
Create a jar file for Test Lib 2.
task clearJar(type: Delete) {
delete 'build/outputs/loggingSDK.jar'
}
task makeJar(type: Copy) {
from('build/intermediates/bundles/release/')
into('build/outputs/')
include('classes.jar')
rename ('classes.jar', 'testlib2.jar')
}
makeJar.dependsOn(clearJar, build)
By using a the command
gradle makeJar
You would have got a testlib2.jar
Copy this into your TestLib1
Use command
gradle assemble
This would create debug and release version
Take the debug version and copy it in Test you would be able to call functions of TestLib1 which in turn calls function of TestLib2
Hope this may help someone looking for such solution

Android Studio Lint check for Unnecessary Module Dependency

When running a Lint check on files in my project I often come across an error that looks like this (actual names of files redacted, but you'll get the idea):
Dependency from module 'name_of_my_project' on module
'some_3rd_party_library' could be probably be removed when
complementary scope to 'File 'filepath_to_the_class_being_analyzed'
also does not contain references on module 'some_3rd_party_library'
I get this error for every single library that isn't compiled with Gradle - i.e. libraries that have been imported whole into the project and then added as dependencies.
Facebook is a great example of a library that even if you wanted to compile it through Gradle you couldn't because they don't support it, and you need to run it as a local library - it seems like you'll then receive this "unnecessary module dependency" warning for every class that doesn't directly call Facebook.
So, the question is - what is the "proper" way of handling this error? Do I ignore it or am I supposed to change the code in some way to make it disappear?
Edit: in the preferences menu for Lint it describes the check as follows (in case this helps figure it out):
This inspection lists modules which contain redundant dependencies on
other modules. These dependencies can be safely removed.
There are known issues with the Lint checkers. I do not get this error in Android Studio 2.1.1. However, if you still do and you know it to be spurious, I would recommend going to Settings --> Editor --> Inspections --> Unnecessary Module Dependency and changing the Severity type from Warning to Info. This way you get a "clean lint build" and avoid a "broken windows" type of state due to bad lint algo. In fact, I've created a special category that has Info severity called Info Due To Bad Lint Algo to which I've assigned whatever I know to be spurious. This way I can periodically review those and see if they've been fixed.
You should report this to them btw, so there is some hope of it getting fixed... Good luck!
You can avoid lint checking using following block of code.
Add following block of code into app build.gradle file inside android {}
lintOptions {
checkReleaseBuilds false
// set to true to turn off analysis progress reporting by lint
quiet true
// if true, only report errors
ignoreWarnings true
abortOnError false
disable "ResourceType"
}