简体   繁体   中英

Android Studio installs an APK for each module

I have an project, build with Gradle in Android Studio v 0.3.2. My project has dependencies to two other modules(android library). The project structure is well defined with build.gradle files. The problem is... when i run the project on an Android device, i get installed 3 apk's on my device. One is the main project(the only right one) and the other two are from the imported modules(theese two i don't want to get installed). How can i achieve this? Or what i'm doing wrong?

Project Structure:

  • MyLibModule
  • MainProject
  • MainProject->libraries-> MyOtherModule

Where MyLibModule is at the same path as the Main project, because i also need this module in a other project.

Just to be clear: The whole project build's OK , all dependencies are OK, but why do i get 3 APK's on my device?

After a whole day struggling with this problem, i located the cause of this strange behaviour. The problem was the manifest of library module. Before i switched to Android studio i used Eclipse. And i had a testActivity declared in the manifest of the library project. Removing all test activities from the manifest's of my library modules solved the problem. Now Android Studio installs only the MainProject apk.

Some code: The manifest of MyLibModule:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.mylibmodule"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7"/>
    <application>
        <activity
            android:name=".TestActivity"
            android:label="@string/app_name">
        </activity>
    </application>
</manifest>

Changed to:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test.mylibmodule" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="7"/> <application> </application> </manifest> 

....And the same for MyOtherModule.

NOTE: the empty application node must stay in the manifest, to avoid build errors.

remove the intent-filter from your library's launch activity

<application>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Changed to

<application>
   <activity android:name=".MainActivity"/>
</application>

It's because your libraries are defined in their build.gradle files as being applications instead of libraries. Look for this line:

apply plugin: 'android'

and replace it with:

apply plugin: 'android-library'

You may need to make other adjustments to the buildfile as well because not everything that applies to applications can be specified in a library buildfile. See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Library-projects for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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