Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get the current channel when Android multi-channel packaging

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to obtain the current channel when Android multi-channel packaging". In the operation of the actual case, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First of all, sub-channel packaging is very simple.

Set in the build.gradle of the project

Android {... productFlavors {home_site {dimension "money" manifestPlaceholders = [UMENG_CHANNEL_VALUE: "home_site"]} main_store {dimension "money" manifestPlaceholders = [UMENG_CHANNEL_VALUE: "main_store"]} other_store {dimension "money" manifestPlaceholders = [UMENG_CHANNEL_VALUE: "other_store"]}

Where dimension is a dimension and is set through flavorDimensions.

DefaultConfig {... FlavorDimensions "money"}

The main purpose of setting dimensions is to facilitate management.

There are a total of three channels in this project.

UMENG_CHANNEL_VALUE can be named freely, but the channel name will be used in the statistics of friends in the project, so the naming method of UMENG will be used here.

The next step is to get the current channel name, so that you can set different logic according to different channel packages, such as setting a module to be hidden in versions released on some platforms and displayed on versions of other platforms.

First, you need to define the information variables in the AndroidManifest.xml file.

...

Then you can get it in Application or Activity.

ApplicationInfo applicationInfo = null; try {applicationInfo = getPackageManager (). GetApplicationInfo (getPackageName (), PackageManager.GET_META_DATA); if (applicationInfo = = null) {return;} String value = applicationInfo.metaData.getString ("CHANNEL_NAME"); CommonConfig.CHANNEL_NAME=value; / / set differentiation code System.out.println ("CHANNEL_NAME:" + value) by channel; switch (value) {case "home_site": / / Logic 1 break; case "main_store": / / Logic 2 break Case "other_store": / / logical 3 break;}} catch (PackageManager.NameNotFoundException e) {e.printStackTrace ();}

This is the end of the content of "how to get the current channel when Android multi-channel packaging". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report