Hi, I’m @Luo Zhu
This article was first published on luo Zhu’s official website
This article synchronizes in the public account “luo Zhu early teahouse”, reprint please contact the author.
Creation is not easy, form a habit, quality three even!
In React Native development, if a Native SDK does not have a React Native plugin, we do not recommend using it in principle. But in a last resort, what can we do? The answer is yes. The steps are roughly divided into integrating SDK and writing bridge code. This article follows this idea to solve the problem of integrating third-party SDK and writing bridge code, which makes the front end engineer’s head ache.
Preliminary knowledge
In the development of Android project, we do not make wheels for all functions by ourselves. We often use various other packages, including various support packages provided by Google, and various third-party function libraries. Sometimes, we will package some functions by ourselves. These packages exist and are imported in various forms, including remote repository, direct copy to local, JAR package, AAR package, so package, etc. Fortunately, we can manage them all in the main project and in build.gradle for each Module. – Android dependency import overview
Dependency introduction
Android Gradle Plugin 3.0 introduces several dependencies:
implementation
For a dependency compiled with this command, the project that has a dependency on the project will not be able to access any programs in the dependency compiled with this command, that is, the dependency will be hidden internally from the outside world. The react-native link command uses this method
Using implementation makes compiling faster: for example, if I use implementation in a library that relies on the Gson library, and then my main project relies on the Library, then my main project can’t access the methods in the Gson library. This has the advantage of making the compilation faster. I switched to a version of the Gson library, but as long as the Library code stays the same, the main project code will not be recompiled.
api
Equivalent to the compile directive
compileOnly
Equivalent to provided, it only works at compile time, does not participate in packaging, and is not included in an APK file. Can be used to resolve duplicate import library conflicts.
Remote warehouse dependency
Here we demonstrate with the introduction of the LeanCloud Android SDK
It is convenient to introduce remote repository dependencies, but previously we needed to declare the address of the remote repository in the project root directory build.gradle.
buildscript {
repositories {
jcenter()
+ // Here is LeanCloud's package warehouse
+ maven {
+ url "http://mvn.leancloud.cn/nexus/content/repositories/public"
+}} dependencies {classpath 'com. Android. View the build: gradle: 1.0.0'}} allprojects {repositories {jcenter ()+ // Here is LeanCloud's package warehouse
+ maven {
+ url "http://mvn.leancloud.cn/nexus/content/repositories/public"
+}}}Copy the code
Then open build.gradle in your app directory and do the following:
android {
+ // To solve the problem that some third party libraries repackage meta-INF
+ packagingOptions{
+ exclude 'META-INF/LICENSE.txt'
+ exclude 'META-INF/NOTICE.txt'
+}Dependencies lintOptions {abortOnError false}} {the compile (' com. Android. Support: support - v4:21.0.3 ')+ // LeanCloud Base package
+ the compile (' cn. Leancloud. Android: avoscloud - SDK: 4.7.10 ')
+
+ // Push the package needed for instant messaging
+ the compile (' cn. Leancloud. Android: avoscloud - push: 4.7.10 @ aar ') {transitive = true}
}
Copy the code
Relying on local
Armeabi and Armeabi-v7a
The jar package
1. Copy and paste the JAR file into the app/libs directory. React Native does not have this folder by default
2. Open app/build.gradle and do the following configuration to list the path to the folder containing the JAR packages.
Note: React Native already does this by default
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
}
Copy the code
If two different JAR packages exist locally, or if the jar packages of different versions exist locally, an error will be reported.
Solution: Replace implementation with compileOnly on one of these. As the name implies, compileOnly only works at compile time and is not included in APK so duplicate classes are not found at run time.
Aar package
Arr stands for Andorid Archive. Arr is a binary Archive of Android library projects. Using Android Studio, it is very easy to generate an AAR file.
Unlike jar packages, aar packages store path declarations and dependency imports separately:
1. Copy the AAR package to the lib directory
2. Specify the aar file path in the project root directory build.gradle
buildscript {
repositories {
+ flatDir {// reference aar under liBS of this project
+ dir "$rootDir/libs"
+}
}
}
allprojects {
repositories {
+ flatDir {// reference aar under liBS of this project
+ dir "$rootDir/libs"
+}}}Copy the code
Create a dependency in app/build.gradle
Note: remote aar is introduced form: implementation (‘ com. Sishu. Android: watermelondb: 0.7.0 @ aar ‘)
dependencies {
+ implementation(name: 'aar', ext: 'aar')
}
Copy the code
So the file
Create a jniLib folder under SRC ->main and copy the armeabi folder where the so file is located.
Note: jniLib is the default placement directory for so files
pit
This is Proguard’s fault
Sometimes when you import jar packages, but still can’t find methods in jar packages? It’s probably because you started the confusion, and the safest thing to do is swap space for safety. But that’s not enough for a serious programmer, and we still have to figure out what Proguard is up against. Here is a tip to share with you:
Open Android Studio and use Logcat as a native developer to check the application log. For example, if you find com.huawei.** library, then do the following configuration:
+ -dontwarn com.huawei.**
+ -keep class com.huawei.**{*; }
Copy the code
-dontwarn
Tells ProGuard not to warn that it can’t be foundcom.huawei.**
References to classes in this package-keep class
Said to keepcom.huawei.**
All classes and methods in this package are not confused. Recompiling the package, the APK size is significantly larger than the previous package. Run app, problem solved!
The resource files in the AAR package are duplicated
The resource file of the main project will directly overwrite the aar file without any error or prompt. Finally, the aar file will also be directly used by the main project, so pay attention to the naming method. There is no better solution at the moment.
AndroidManifest merge error
This also happens with the AAR package. Android Studio projects can have an Androidmanifest.xml file in each module, but the final APK file can only contain one Androidmanifest.xml file. When building an application, a Gradle build consolidates all the manifest files into a single manifest file encapsulated in APK. If the aar package manifest file and our app manifest file attribute conflict: use tools:replace=” attribute name “to resolve.
The difference between annotationProcessor and compileOnly
Each annotationProcessor will compile into apk and not compileOnly. What’s the difference? AnnotationProcessor generates code at compile time, but compileOnly is not needed. CompileOnly is a duplicate library, so you can shave it off and keep only one library.
reference
- Android Studio introduces jar packages and the SO library
- Android dependency import walkthrough
- React- Native uses native (ios, Android) third-party SDKS