0

我想使用 Gradle 构建 Android。所有应用商店都有近 100 个 apk。唯一的区别是 assets/config.properties 文件。

蚂蚁脚本:

<target name="makechannelidapk">
    <propertyregex property="channel_id" input="${line_content}" regexp="(.*)," select="\1"/>
    <delete file="${asset-dir}/config.properties" />
    <echo file="${asset-dir}/config.properties"CHANNEL_ID=${channel_id}</echo>

    <antcall target="finalmake" >
        <param name="id" value="${client_source}"/>
    </antcall>
</target>
4

1 回答 1

0

使用元数据

AndroidManifest.xml

<meta-data android:value="${CHANNEL_VALUE}" android:name="CHANNEL"/>

构建.gradle

    android {
    defaultConfig {
        ...
        // default CHANNEL_VALUE
        manifestPlaceholders = [CHANNEL_VALUE: "default"]
    }
    productFlavors {
        fir{}
        wandoujia{}
        qihu360{}
        baidu{}
        xiaomi{}
        uc{}
        productFlavors.all{
            flavor->flavor.manifestPlaceholders = [CHANNEL_VALUE: name]
        }
    }
}

获取元数据

try {
    ApplicationInfo ai = getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.GET_META_DATA);
    Bundle bundle = ai.metaData;
    String myApiKey = bundle.getString("CHANNEL");
} catch (NameNotFoundException e) {
    Log.e(TAG, "Failed to load meta-data, NameNotFound: " + e.getMessage());
} catch (NullPointerException e) {
    Log.e(TAG, "Failed to load meta-data, NullPointer: " + e.getMessage());         
}
于 2015-10-22T09:35:53.483 回答