1. 程式人生 > >Android 手機同時安裝debug release版(四部曲)

Android 手機同時安裝debug release版(四部曲)

Android 手機同時安裝debug release版(四部曲)

文章目錄

引入

同一部手機上安裝app的debug版和release版, 不會相互覆蓋,這在開發中會用的一個小功能點,也可能在你面試的考官,考察你是否有真實開發經驗,可能以這個點來問你,博主以前在一次面試中也遇到過,一般配置型,都是跟build.gradle有關,那麼問題就很好處理了

步驟一 build.gradle

你可以在buildTypes—debug 中 只加這一句程式碼:

resValue “string”, “app_name”, “@string/app_name_debug” //配置debug包名稱

 buildTypes {
        release {
            resValue "string", "app_name", "@string/app_name_release" //配置release包名稱
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }

        debug {
            resValue "string", "app_name", "@string/app_name_debug"  //配置debug包名稱
            applicationIdSuffix '.debug' //增加包名字尾
        }
    }

步驟二 strings.xml

<resources>
    <string name="app_name_release">Release</string>
    <string name="app_name_debug">Debug</string>
</resources>

步驟三 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.kx.test">
          
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:hardwareAccelerated="true"
        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>
    </application>

</manifest>

步驟四 可能的坑

在這裡插入圖片描述

你按上述步驟,出現這個問題:

Error:Execution failed for task ‘:app:mergeDebugResources’. [string/app_name] D:\DevWorkSpace\KxJiaoBen\app\build\generated\res\resValues\debug\values\generated.xml [string/app_name] D:\DevWorkSpace\KxJiaoBen\app\src\main\res\values\strings.xml: Error: Duplicate resources

解決辦法

刪除strings.xml 檔案中, 這一行原始程式碼即可< string name=“app_name”>Test< /string>

 <string name="app_name">Test</string>

尾言

本文如有錯誤或不當之處,歡迎讀者留言斧正,互相交流學習,博主不勝感激.聯絡郵箱[email protected]