| Question
In a recent project, because there were too many modules, consider replacing the module library with an aar reference. However, according to the normal way aar was introduced, there were various errors: ClassNotFindException and so on
| Background
Development environment: Android Studio + Gradle
Currently, there are several ways to develop Android projects in as that rely on third-party libraries: 1. Introducing jar packages 2. Introduce aar package 3. Use maven dependency, compile(name:XXX,version:XXX) form
We will mainly talk about the second way.
When module is declared as library, the compilation generates an aar file that contains the following: class file, res file, assets file, etc.
By introducing aar, it is very convenient for us to call the interfaces and resource files in the library project. However, according to this idea, introducing AAR files in the project has unexpectedly made a variety of errors, which is embarrassing. After careful analysis, it is found that the aar does not contain the dependencies contained in the module, which leads to the possibility of initialization or various exceptions at the time of invocation if the module uses a third-party dependency library. It seems that the easiest way is to re-add dependencies in the app (module), which is not what I want.
After unremitting efforts, a final solution has been found:
| Solution
Publishing to the maven repository,, is really not a problem. It's not hard to understand. Publish to the maven repository and you can see that some open source repositories are on it. 1. Publish to jcenter or maven; 2. Publish to your own Maven or internal maven server; 3. Publish to local maven library;
Of the three methods of publishing, obviously the third is the easiest, so let's leave the issue of collaboration out of the box (smart you can do it right?)And then add the dependencies as normal.The steps are as follows: a. Add task s to gradle to generate libraries
// 1.maven-plug-in apply plugin: 'maven' // 2.maven-information ext {// ext is a gradle closure allowing the declaration of global properties PUBLISH_GROUP_ID = 'com.elitech.core' PUBLISH_ARTIFACT_ID = 'icore' PUBLISH_VERSION = 1.0 } // 3.maven-path uploadArchives { repositories.mavenDeployer { String env = getAndroidSdkDir() if (env == null) { println("don't have env: android_home ,please set...") if (env == null) { throw new RuntimeException( "Unable to determine Android SDK directory.") } } def deployPath = file(env+"/extras/android/m2repository/") String repository_url = "file://${deployPath.absolutePath}" println(repository_url) repository(url: repository_url) pom.project { groupId project.PUBLISH_GROUP_ID artifactId project.PUBLISH_ARTIFACT_ID version project.PUBLISH_VERSION } } } // get android sdk dir String getAndroidSdkDir() { def rootDir = project.rootDir def androidSdkDir = null String envVar = System.getenv("ANDROID_HOME") def localProperties = new File(rootDir, 'local.properties') String systemProperty = System.getProperty("android.home") if (envVar != null) { androidSdkDir = envVar } else if (localProperties.exists()) { Properties properties = new Properties() localProperties.withInputStream { instr -> properties.load(instr) } def sdkDirProp = properties.getProperty('sdk.dir') if (sdkDirProp != null) { androidSdkDir = sdkDirProp } else { sdkDirProp = properties.getProperty('android.dir') if (sdkDirProp != null) { androidSdkDir = (new File(rootDir, sdkDirProp)).getAbsolutePath() } } } if (androidSdkDir == null && systemProperty != null) { androidSdkDir = systemProperty } if (androidSdkDir == null) { throw new RuntimeException( "Unable to determine Android SDK directory.") } androidSdkDir }
b. Execute task: uploadArchives c.app Add Dependency:
// from maven local compile 'com.elitech.core:icore:1.0'
d. Finally, don't forget to add a maven warehouse address. We use a local warehouse, just write the path
repositories { //maven{url uri('/Users/******/Library/Android/sdk/extras/android/m2repository')} maven { url uploadPath } }
The uploadPath above refers to the method used to obtain the sdk path in step 2.
When publishing to a local repository, be sure to confirm the environment variables and installation path for android sdk.
Keep sharing original technology and your support will encourage me to continue creating!