Android签名打包-多渠道打包

复制libs

  • common
  • analytics

集成友盟+ App统计功能

在Application的onCreate方法中调用

1
2
3
4
5
6
7
8
9
10
11
12
/**
* 初始化common库
* 参数1:上下文,不能为空
* 参数2:设备类型,UMConfigure.DEVICE_TYPE_PHONE为手机、UMConfigure.DEVICE_TYPE_BOX为盒子,默认为手机
* 参数3:Push推送业务的secret
*/
try {
UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, "你的appID");
} catch (Exception e) {
e.printStackTrace();
}

添加相应权限

1
2
3
4
5
6
7
8
9
10
11
12
<!-- 必须的权限 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

<!-- 推荐的权限 -->
<!-- 添加如下权限,以便使用更多的第三方SDK和更精准的统计数据 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

配置渠道

1
2
3
4
5
6
7
8
9
<manifest>
<application ……>
……
<!--umeng start-->
<meta-data android:value="你的AppID" android:name="UMENG_APPKEY"/>
<meta-data android:value="${UMENG_CHANNEL_VALUE}" android:name="UMENG_CHANNEL"/>
<!--umeng end-->
</application>
</manifest>

代码混淆

1
keep class com.umeng.commonsdk.** {*;}

多渠道配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
android {
productFlavors {
wandoujia {}
yingyongbao {}
c360 {}
kq {}
productFlavors.all { flavor ->
flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}

applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
def fileName
def flavor = productFlavors.name;
if (outputFile != null && outputFile.name.endsWith('.apk')) {
if (variant.buildType.name.equals('release')) {
fileName = "名字_${defaultConfig.versionName}_${flavor}.apk"
} else if (variant.buildType.name.equals('debug')) {
fileName = "名字_${defaultConfig.versionName}_debug_${flavor}.apk"
}
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}

Android疑难杂症-ViewPager+分组列表+指示器悬停

系统自带方案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">

<ImageView
android:id="@+id/main.backdrop"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:scaleType="centerCrop"
android:src="@drawable/material_img"
app:layout_collapseMode="parallax" />

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:layout_collapseMode="pin" />

</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="50dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/my_txt"
android:textSize="20sp" />

</android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

此方案不利于屏幕适配,故不采用。

Androidd开源控件-Glide

Glide 4.3.1 (4.x用起来更麻烦了)

集成

1
2
3
4
5
6
7
8
9
repositories {
mavenCentral()
maven { url 'https://maven.google.com' }
}

dependencies {
compile 'com.github.bumptech.glide:glide:4.3.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.3.1'
}

Android常用代码-App更新

APP更新逻辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
private void checkUpdate() {
String key = SpUtil.getKey();

Api.getApi0()
.CheckUpdate(key, SystemUtil.getPackageInfo(this).versionCode)
.compose(RxHelper.<CheckUpdateResult>io_main())
.subscribe(new Subscriber<CheckUpdateResult>() {
@Override
public void onCompleted() {

}

@Override
public void onError(Throwable e) {

}

@Override
public void onNext(CheckUpdateResult checkUpdateResult) {
if (checkUpdateResult.getCode() == 1) {
CheckUpdateResult.DataEntity data = checkUpdateResult.getData();
showUpdate(data);
}
}
});
}

private void showUpdate(final CheckUpdateResult.DataEntity data) {
if (data != null) {
showTwoBtnDialog("更新", "v" + data.getVersionName() + "\n" + data.getUpdateInfo(), false, new IBaseActivity.OnTwoBtnClick() {
@Override
public void onOk() {
final TextView[] tv = new TextView[1];
final ProgressBar[] pb = new ProgressBar[1];
showUpdateDialog("更新", "即将开始下载...", false, new IBaseActivity.OnOneBtnClickListener() {
@Override
public void onProgress(TextView textView, ProgressBar progressBar) {
tv[0] = textView;
pb[0] = progressBar;
}

@Override
public void onOK() {
}
});
String url = Constants.API + File.separator + data.getUrl();
final String name = "HighNetFix_" + data.getVersionName() + "_" + data.getVersionCode() + ".apk";
OkGo.<File>get(url)
.tag(this)
.execute(new FileCallback(Constants.APK_DOWNLOAD_PATH, name) {
@Override
public void onSuccess(Response<File> response) {
hideUpdateDialog();
ApkUtils.installApk(HomePageActivity.this, BuildConfig.APPLICATION_ID, Constants.APK_DOWNLOAD_PATH, name);
}

@Override
public void onError(Response<File> response) {
hideUpdateDialog();
ToastUtils.showCustomBgToast("下载失败,请检查您的网络设置。");
}

@Override
public void downloadProgress(Progress progress) {
long currentSize = progress.currentSize;
long totalSize = progress.totalSize;
int pro = (int) (currentSize * 100.0 / totalSize + 0.5f);
tv[0].setText("已下载:" + pro + "%");
pb[0].setProgress(pro);
}
});
}

@Override
public void onCancel() {
}
});
}
}

AndroidStudio技巧-mac上导入导出SVN项目

Android Studio -> SVN

  • 1.打开项目
  • 2.Preferences -> Version Control -> Ignored Files
    (1)app/build
    (2)build
    (3)*.iml
  • 3.VCS -> import into version control -> import into subversion
  • 4.Submit

SVN -> Android Studio

  • 1.Check out project from version control
  • 2.选择Subversion
  • 3.选中项目文件夹根目录
  • 4.选择保存路径(非中文路径,新建一个项目文件夹)
  • 5.等待完成(如果打开会报错,大多是找不到gradle,别急)。
  • 6.复制其他项目的local.properties文件到项目文件夹。
  • 7.open a new Android Studio project打开项目

Android疑难杂症-ViewPager禁止左右滑动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
* Created by zhouzhuo810 on 2017/10/31.
*/
public class NoScrollViewPager extends ViewPager {
public NoScrollViewPager(Context context) {
super(context);
}

public NoScrollViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return false;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
return true;
}
}

Java工具类-二维码生成和解析

zxing生成和解析二维码

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
<!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.0</version>
</dependency>