facebook twitter hatena line email

Android/開発環境/AndroidStudio/ライブラリのAndroidManifest指定

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

ライブラリのbuild.gradleに以下を記述

android {
    sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
        }
    }
}

そのファイルがあるdirのsrc/main/AndroidManifest.xmlにAndroidManifestを記述すると、

元々のAndroidManifestは無効になって、置き換わってるっぽい。

マージされたAndroidManifest.xml

以下にマージされたAndroidManifest.xmlが出力される

app/build/intermediates/bundle_manifest/debug/bundle-manifest/AndroidManifest.xml
app/build/intermediates/merged_manifests/debug/AndroidManifest.xml
app/build/intermediates/instant_app_manifest/debug/AndroidManifest.xml

サンプル

mainのsrc/main/AndroidManifest.xmlに以下を追加

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="hoge"
            android:value="main" />

        <meta-data
            android:name="hoge2"
            android:value="mainmain" />

    </application>

</manifest>

mylibrary のsrc/main/AndroidManifest.xmlに以下を追加

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mylibrary">

    <meta-data
        android:name="hoge"
        android:value="mylibrary" />

</manifest>

main側のbuild.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.1"

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation project(':mylibrary')
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

}

mylibrary側のbuild.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.1"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

}

settings.gradleの設定

include ':mylibrary'
include ':app'
rootProject.name = "My Application"

マージされたAndroidManifest.xml(app/build/intermediates/bundle_manifest/debug/bundle-manifest/AndroidManifest.xml)

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

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="30" />

    <meta-data
        android:name="hoge"
        android:value="mylibrary" />

    <application
        android:allowBackup="true"
        android:appComponentFactory="androidx.core.app.CoreComponentFactory"
        android:debuggable="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:testOnly="true"
        android:theme="@style/AppTheme" >
        <activity android:name="com.example.myapplication.MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="hoge"
            android:value="main" />
        <meta-data
            android:name="hoge2"
            android:value="mainmain" />
    </application>

</manifest>

上記サンプル失敗?

meta-dataがapplicationの外に出ていて、mylibraryにapplicationタグを追加するとエラーになった。