前几天出了个需求,需要使用到Zxing来生成条形码,那如何来在Android Studio里面加入Zxing这个框架呢:
大体依赖方法和其中会遇到的错误,作者HappyGhh在简书这篇文章里都有讲到,跟着步骤一步步做好就可以,文章地址:https://www.jianshu.com/p/a9b91197b487
我这里提一下文章里没提到过但是我自己在操作中遇到的问题:
第一:依赖完后Make Project 报Java Complier需要把一些Activity里面的switch语句换成if之外,还有一个地方需要修改:
com.google.zxing.client.android.history.HistoryItemAdapter这个文件的构造方法有语法错误:
HistoryItemAdapter(Context activity) {
super(activity, R.layout.history_list_item, new ArrayList<>());
this.activity = activity;
}
修改为如下:
HistoryItemAdapter(Context activity) {
super(activity, R.layout.history_list_item, new ArrayList());
this.activity = activity;
}
就可以完成编译了。
第二个:这个错误我以前都没有遇到过,应该是依赖项目的时候都会出现吧
具体错误日志:
Cannot fit requested classes in a single dex file (# methods: 65617 > 65536)
在网上搜了一下后,发现HaiYuKing这篇文章很好解决了这个问题,文章地址:https://www.cnblogs.com/whycxb/p/9792192.html
一、在app的build.gradle中添加依赖,在defaultConfig中添加以下代码【注意:必须是app这个module,不能是其他的module】
apply plugin: 'com.android.application'
android { compileSdkVersion 28 defaultConfig { applicationId "com.why.project.poidemo" minSdkVersion 16 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" // multiDexEnabled true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } //poi compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.android.support:multidex:1.0.3' }
二、如果你自定义了Application子类,需要在这个子类中重写一个方法
@Override public void onCreate() { super.onCreate(); // 主要是添加下面这句代码 MultiDex.install(this); }
基本上这上面就是我在使用Android Studio添加Zxing时所遇到的问题,望能帮助到需要的同学少走弯路
网友评论