Android studio imports another project as a dependency package

To import a project as a library into another project: 
1. Start AndroidStudio and open the project you need to receive the Library. For example, there are two projects, project A, Library project B, then open project A. 
2. Find New→Module→Import Gradle Project in the drop-down menu and click Next. Select the Library you want to import. 
write picture description here
Click Module: 
write picture description here
3. In the opened window, click the "..." browse window on the right, find the location of the Library project you want to import in the folder window, select it and click the OK button at the bottom to import.

4. The import window displays the detailed information of the Module (project), click Finish to complete the import. It should be noted that the Library imported into the AndoridStudio project must also be an AndroidStudio project, not an Eclipse project. If it is an Eclipse project, you need to choose to export the AndroidStudio project. 
5. Select the Project tab on the project home page, and you can see that the project we just imported has been added to the file structure diagram of the project. The import of the library is complete.

Next is some configuration, import the library into the main project

1. Click the File menu in the upper left corner, find Project Structure in the drop-down menu that appears, and click to enter. 
2. After selecting the original project on the left side of the window, click Dependencies on the right side. You can see information such as the jar package currently referenced by the project. 
3. Click the "+" add button on the right, find Module Dependency in the drop-down menu, and click to open the project selection window. 
4. After selecting the Library project imported in the previous step, click the OK button to import it into the original project. 
5. The imported Libray project appears in the dependencies of the original project. At this time, the contents of the Library project can be correctly referenced in the project. Click the OK button at the bottom to complete the addition. 
Problems encountered in the process: 
Problem 1: 
Error: AndroidStudio TransformException : Error: Execution failed for task':app:transformClassesWithDexForDebug'Solution 

In the build.gradle file of your main project, add the bold line 
Android { 
compileSdkVersion .. 
buildToolsVersion '…' 
defaultConfig { 
… 
targetSdkVersion .. 
multiDexEnabled true // this line will solve this problem 


Problem 2: 
Error : unspecified on project app resolves to an APK archive which is not supported 
Solution: 
In our project as library, its header is apply plugin: 'com.Android.application' 
This sentence tells Gradle Compile the project we want to use as library as application, that is, apk, this is the problem; the 
method is to change the above sentence to: apply plugin: 'com.android.library', don't worry if there are still problems, Gradle calls it a Library, that is, a library. After running, 
Error :Libraryprojects cannot set applicationId. applicationId is set to 'package_name' indefault config. 
That is because a library does not allow setting applicationId, you need to builde.gradle — android — applicationId in defaultConfig removed.

问题3: 
Error:Error:Executionfailed for task ‘:greencar:processDebugManifest’.> Manifest merger failed withmultipleerrors, see logs。 
原因:AS的Gradle插件默认会启用Manifest Merger Tool,若Library项目中也定义了与主项目相同的属性(例如默认生成的android:icon和android:theme),则此时会合并失败,并报上面的错误。 
解决方案: 
在manifest根标签上加入xmlns:tools=”http://schemas.android.com/tools”,并在Manifest.xml的application标签下添加tools:replace=”name,icon, label,heme”

以及在主项目的 manifest文件中,重复写了 call_phone的权限,网上也有人是某个activity下多写了一句intent-filter,里面没有内容,将这些重复的空的删掉就好,并将作为lib的minisdk与主项目同步(修改library飞build.gradle文件中最小sdk,或者降低主项目的sdk)

Question 4: 
error: error in the project as library: constant expression required 
Solution: 
In general Android projects, the constants of the R class are defined with final, but after ADT 14, if in the library project, it There will be no final keyword, and we use switch in the library project. In the case of the switch statement, if R.id.xxx is used, there will be a problem, and non-constant is not allowed in the case statement. 
One way Google provides is to turn it into an if-else statement.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326100337&siteId=291194637