导入support v4包后,用gradle打包失败

使用intellij 14, linux平台
下面是部分程序

package com.test.broadcastbestpractice.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.Button;

public class MainActivity extends BaseActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);


        Button forceOffline = (Button) findViewById(R.id.force_offline);
        forceOffline.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.test.broadcastbestpractice.FORCE_OFFLINE");
                localBroadcastManager.sendBroadcast(intent);
            }
        });
    }
}

我已经在源程序中导入了 support v4包,并且没有报错
图片描述
编译测试的时候,有如下报错

Information:Gradle: Executing tasks: [:app:assembleDebug]
Information:2/10/15 5:45 PM - Compilation completed with 4 errors and 0 warnings in 6 sec
Error:Gradle: Execution failed for task ':app:compileDebugJava'.> Compilation failed; see the compiler error output for details.
/home/tcstory/IdeaProjects/BroadcastBestPractice/app/src/main/java/com/test/broadcastbestpractice/app/MainActivity.java
Error:(5, 34) Gradle: error: package android.support.v4.content does not exist
Error:(14, 15) Gradle: error: cannot find symbol class LocalBroadcastManager
Error:(14, 61) Gradle: error: cannot find symbol variable LocalBroadcastManager

我大概猜得出来可能是gradle没有配置好,但是不懂得具体的解决方法,下面是项目中的build.gradle文件内容

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
    }
}
apply plugin: 'com.android.application'

repositories {
    jcenter()
}

android {
    compileSdkVersion 19
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.test.broadcastbestpractice.app"
        minSdkVersion 19
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_6
        targetCompatibility JavaVersion.VERSION_1_6
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}
回复
阅读 18.5k
2 个回答

图片描述

在 build.gradle 里面配置

看起来是配置错了哦,
我的是这样的
clipboard.png

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏