简体   繁体   English

如何通过链接引用将模块导入android studio?

[英]How to import module into android studio by link referent?

I need to use resource from another android project (gradle) in new android phone project by using Android Studio Version 3 and Android API 23. I ready try to import by using import option in this IDE, but that module import by copy from external project. 我需要通过使用Android Studio版本3和Android API 23使用新的android手机项目中另一个android项目(gradle)中的资源。我准备尝试在此IDE中使用import选项导入,但是该模块是通过复制从外部项目中导入的。 In my project has to use require share project as a framework (it is a module) that sync by SVN, so in this case I cannot update what i change into my framework. 在我的项目中,必须使用需求共享项目作为由SVN同步的框架(它是一个模块),因此在这种情况下,我无法更新更改为框架的内容。 I need the solutions for using that module by just link to it without copy into project. 我只需要链接到该模块而不复制到项目中,就可以使用该模块。

I think you are looking for a solution to develop and resuse your local Android library, right? 我认为您正在寻找开发和重新使用本地Android库的解决方案,对吗? So follow my steps below: 因此,请按照以下步骤操作:

1 - In android studio, create new android project (File > New > New Project) and save in any location your like. 1-在android studio中,创建新的android项目(“文件”>“新建”>“新项目”)并保存在您喜欢的任何位置。

2 - Create new Module (File > New > New Module > Android Library) 2-创建新模块(文件>新建>新建模块> Android库)

Ex: module name = MyFirstLocalLibrary, package name: com.example.local.library 例如: 模块名称= MyFirstLocalLibrary,包名称:com.example.local.library

在此处输入图片说明

3 - Edit build.gradle file which is in root of your library folder, but below the other plugins already present, add apply plugin: 'maven-publish' under apply plugin: 'com.android.library' . 3-编辑build.gradle文件,该文件位于您的库文件夹的根目录中,但在已经存在的其他插件下方,添加Apply插件:'maven-publish'在apply插件:'com.android.library'下 This plugin will enable you to publish to a Maven repository, even local ones. 这个插件将使您能够发布到Maven仓库,甚至是本地仓库。

apply plugin: 'maven-publish'

publishing {
    publications {
        library(MavenPublication) {
            // Don't forget to change these
            groupId 'com.example.local.library'
            artifactId 'MyFirstLocalLibrary'
            version '1.0'

            artifact(bundleRelease)
            pom.withXml {
                def dependenciesNode = asNode().appendNode('dependencies')
                configurations.compile.allDependencies.each {
                    if (it.group != null && (it.name != null || "unspecified".equals(it.name)) && it.version != null) {
                        def dependencyNode = dependenciesNode.appendNode('dependency')
                        dependencyNode.appendNode('groupId', it.group)
                        dependencyNode.appendNode('artifactId', it.name)
                        dependencyNode.appendNode('version', it.version)
                    }
                }
            }
        }
    }
}

在此处输入图片说明

Note: Don't forget to click on **Sync now every time, your edited build.gradle 注意:每次编辑后的build.gradle不要忘记单击**立即同步

4 - Publish module: Click on Gradle button at the top right of your android studio and you should see gradle task name publishing . 4-发布模块:单击android studio右上方的Gradle按钮,您应该看到gradle任务名称发布

在此处输入图片说明

Then, you can see that, there are 2 tasks ( publishLibaryPublicationToMavenLocal and publishToMavenLocal ). 然后,您会看到有两个任务( publishLibaryPublicationToMavenLocalpublishToMavenLocal )。 Double on one of above tasks or Right click one of above tasks, then choose run . 双击以上任务之一或右键单击以上任务之一,然后选择运行 And Wait to see if your build is BUILD SUCCESSFUL . 然后等待,看看您的构建是否成功

To Make your your library is published, your can check your .m2 repository, you should see as screenshot below: 要发布您的库,您可以检查.m2存储库,您应该看到以下屏幕截图:

在此处输入图片说明

5 - If build is success, your library should be ready to use. 5-如果构建成功,则您的库应该可以使用了。 Now create new android project and Update application repositories: In your main application's project's build.gradle, add mavenLocal(). 现在创建新的android项目并更新应用程序存储库:在主应用程序项目的build.gradle中,添加mavenLocal()。

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
        mavenLocal()
    }
}

6 - Update application dependencies: open build.gradle (app > build.gradle) and add your local dependency: 6-更新应用程序依赖项:打开build.gradle (应用程序> build.gradle)并添加本地依赖项:

dependencies {
   .....
   compile 'com.example.local.library:MyFirstLocalLibrary:1.0'
}

Note: Don't forget to click on **Sync now every time, your edited build.gradle 注意:每次编辑后的build.gradle不要忘记单击**立即同步

All done. 全部做完。 Hope this was helpful :). 希望这对您有所帮助:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM