Module单独运行调试

首先在主工程目录下的gradle.properties建立变量,控制当前moudle是单独运行还是moudle形式
image.png
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

# 注意不要带有标点符号,比如xx=true; 会被转换为false
isSampleDebug=true
然后在对应moudle目录下建立一个用于测试的清单文件,清单文件需要设置默认启动的activity,还有一些调试时候需要用到res也要设置下。


image.png
Debug模式下的清单文件

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true">
        <activity android:name=".activity.ComponentListActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
** module模式下的清单文件**

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

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

</manifest>
在对应moudle目录下的gradle里面做如下配置
1、独立工程需要apply plugin: 'com.android.application'
2、独立工程需要设置applicationID
3、独立工程需要都默认启动的主activity,这里需要设置不同的清单文件和资源引用位置
//单独工程和lib的声明不同
if (isSampleDebug.toBoolean()) {
    apply plugin: 'com.android.application'
} else {
    apply plugin: 'com.android.library'
}
android {
    
    //限制资源名称
    resourcePrefix project.getName() + "_"
    defaultConfig {
   
        //作为单独工程,需要设置applicationID
        if (isSampleDebug.toBoolean()) {
            applicationId "com.lexinfintech.sample"
        }
       
    }


    sourceSets {
        main {
            // 单独调试与集成调试时使用不同的 AndroidManifest.xml 文件
            if (isSampleDebug.toBoolean()) {
                manifest.srcFile 'src/main/debug/AndroidManifest.xml'
                //多增加一个资源调试路径 src/main/debug/res
                res.srcDirs = ['src/main/res', 'src/main/debug/res']

            } else {
                manifest.srcFile 'src/main/AndroidManifest.xml'
                java {
                    //module 模式下要 排除src/test/文件夹下的所有文件
                    exclude 'src/debug/'
                }
            }

        }
    }

}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    api project(':base')
}

最后还需要在主工程设置一下引用关系
    //主工程在组件作为单独工程运行的时候,要过滤到此组件,因为application是不能直接引用application的
    if(!isSampleDebug.toBoolean()){
        runtimeOnly project(':sample')
    }
这样moudle就可以作为独立工程跑起来了

 

https://www.jianshu.com/p/5014bc7cf0de

 

posted @ 2019-10-08 18:01  wikiki  阅读(571)  评论(0编辑  收藏  举报