androidstudio reference third-party library synthesis

This article is transferred from: https://www.cnblogs.com/whycxb/p/9095959.html

Original article from HaiyuKing

Overview

In the Android development process, third-party libraries and jar, so, and arr files are often used, so how to quote them in the project? Here is a brief introduction.

Reference third-party libraries

Generally, you can cite in accordance with the citation method provided by the third-party library author.

For example, take the okhttp-utils open source library as an example:

Add the following code to the build.gradle file of the module (such as app)

Note: The old version of Android studio is written ascompile 'com.zhy:okhttputils:2.6.2'

The writing method of the new version of Android Studio is slightly different: implementation 'com.zhy:okhttputils:2.6.2'

However, the new version of Android studio is compatible with the old version.

Then Sync Now or Sync Project with Gradle Files

 

Referencing the jar file

In fact, when you create a new project, you can compile the jar in the libs directory by default, because the build.gradle files of all modules contain the following dependencies:

So you only need to copy the referenced jar file to the libs directory of the module (why it is a module, not app, because the jar file is not necessarily placed in the app, it may be in any module; and app is also a kind of module) libs directory. can.

Copy the jar package to the libs directory

 Sync Project with Gradle Files

 

Citing the effect of success

At this point, it can only be regarded as successfully referencing the jar to the baselibrary, which means that the jar can only be used in the baselibrary, but not in the app (the app is dependent on the baselibrary). So how to realize that the jar file can also be used in the module that depends on the baselibrary?

Add the following code to the dependencies{} of the current module’s build.gradle file [applicable to other modules that depend on the current module also need to use this jar file]

Copy code

apply plugin: 'com.android.library'

android {
    compileSdkVersion 26
    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            postprocessing {
                removeUnusedCode false
                removeUnusedResources false
                obfuscate false
                optimizeCode false
                proguardFile 'proguard-rules.pro'
            }
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    //jsoup must be compile, otherwise no related classes can be found in the app
    compile files('libs/gson-2.2.4.jar')

}

Copy code

 Sync Project with Gradle Files

 

 

Reference arr package

Copy the arr package to the libs directory of the module

Add the following code in build.gradle

Copy code

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.why.project.helloworld"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    //Create a subfolder under the layout folder
    sourceSets {
        main{
            res.srcDirs = [
                    'src/main/res/layout/home',
                    'src/main/res/layout',
                    'src/main/res'
            ]
        }
    }
}

repositories{
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation project(':baselibrary')

    compile 'com.zhy:okhttputils:2.6.2'

    compile(name: 'ijkplayer', ext: 'aar')
    compile(name: 'libp', ext: 'aar')
}

Copy code

 Sync Project with Gradle Files

 

If the arr file is referenced in the module, then the app relies on this module. If no processing is done, an error message of failed to resolve will appear after compilation.

For example, I referenced the libnewurl.arr file in baselibrary.

The solution is to add the following code to the build.gradle file of the app (because the app depends on this baselibrary):

repositories{
    flatDir {
        dirs 'libs'
        dirs project(':baselibrary').file('libs')
    }
}

The baselibrary in the above code is the name of the module, and libs is the directory where the aar library that the module depends on is located.

 

Reference so package

Solution one [recommended to use this solution]

Create the folder jniLibs in the src/main/ directory (if you do not need to create it), copy the so file to this directory, and the project will automatically load the so dynamic library in the src/main/jniLibs directory.

Copy the so file to the jniLibs directory

Option II

Put the so files corresponding to different CPU architectures in the libs directory, and add the code in the android{} of the build.gradle file: jniLibs.srcDir'libs' to show that the path of so is the libs path.

    sourceSets {
        main {
            jniLibs.srcDir 'libs'
        }
    }

 

If you want to control the CPU platform used, add it under the defaultConfig of build.gradle

        ndk {
            abiFilters "armeabi", "armeabi-v7a" //Select the platform to be used, "x86", "mips"
        }

If the compilation fails, add in the gradle.properties of the project

android.useDeprecatedNdk=true

Guess you like

Origin blog.csdn.net/liyang_nash/article/details/106732360