*更新:运行ProGuard减少资源后,ProGuard似乎正在删除重要的代码: 错误:在根项目“ Google maps Demo”类型为org.gradle.api.Project的参数[build_7b52k4yyeks9l2efdeec28zeo $ _run_closure2 @ 6cc7c31f]中找不到方法android()。 更新了build.gradle代码:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
mavenLocal()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath 'com.google.gms:google-services:4.3.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url 'https://maven.fabric.io/public' }
maven {
url "https://maven.google.com"
}
}
}
android {
buildTypes {
release {
// Enables code shrinking, obfuscation, and optimization for only
// your project's release build type.
minifyEnabled true
// Enables resource shrinking, which is performed by the
// Android Gradle plugin.
shrinkResources true
// Includes the default ProGuard rules files that are packaged with
// the Android Gradle plugin. To learn more, go to the section about
// R8 configuration files.
proguardFiles getDefaultProguardFile(
'proguard-android-optimize.txt'),
'proguard-rules.pro'
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
我正在使用Android Studio通过Android的Coding的教程(https://www.youtube.com/watch?v=eiexkzCI8m8)来实现Google Maps api。我在Java编译器中收到的错误是“ D8错误”和两个错误。 错误#1:“由com.android.tools.r8.CompilationFailedException引起:编译无法完成”
错误2:“由com.android.tools.r8.utils.AbortException引起:错误:null,无法在单个dex文件中容纳请求的类(#方法:86965> 65536)”
有人看到问题了吗?
这是我的Java代码:
public class MainActivity extends FragmentActivity implements OnMapReadyCallback{
GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
LatLng Brockton = new LatLng(42.0895988,-70.9798322);
map.addMarker(new MarkerOptions().position(Brockton).title("Brockton"));
map.moveCamera(CameraUpdateFactory.newLatLng(Brockton));
}
}
答案 0 :(得分:0)
这不是代码中的错误,这是因为您对Google Maps的依赖。
这为您的应用程序添加了很多方法,超过了每个dex文件64k的限制。有两种解决方案:
使用MultiDex:https://developer.android.com/studio/build/multidex Android Studio将生成多个dex文件,因此未达到限制。
使用ProGuard删除未使用的方法:https://developer.android.com/studio/build/shrink-code 未调用的方法不会添加到您的dex文件中。结合混淆,这总是一个好主意。
答案 1 :(得分:0)
写:
multiDexEnabled true
在 build.gradle(app) 文件中