2
我已經創建了一個簡單的項目,其中創建了一個名爲'mylibrary'的新模塊,其中包含一個Custom視圖。儘管將庫添加爲依賴項,Android Studio仍然無法檢測到我的庫組件
庫在創建時自動添加到項目中的依賴關係列表中。
但是,依賴於此庫的應用程序無法找到庫中存在的自定義視圖。
我嘗試了幾個建議here,here和here,但他們都沒有工作。
我列出下面將所需的文件: -
的build.gradle(模塊:在MyLibrary)
apply plugin: 'com.android.library'
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:24.+'
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
}
的build.gradle(項目:TESTAPP)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle(Module:app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "app.auro.self.testapp"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile project(':mylibrary')
}
settings.gradle(項目設置)
include ':app', ':mylibrary'
在MyLibrary - > samplelayout.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:hint="HEY"
android:gravity="center"
android:layout_gravity="center_horizontal"/>
</merge>
在MyLibrary - > CustomView.java
package app.auro.self.mylibrary;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.LinearLayout;
import java.security.PrivateKey;
/**
* Created by stpl on 25/8/16.
*/
public class CustomView extends LinearLayout {
private EditText editText;
public CustomView(Context context) {
this(context,null);
}
public CustomView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.samplelayout,this,true);
editText = (EditText) findViewById(R.id.editText);
editText.setText("HELLO");
}
}
應用程序 - > activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="app.auro.self.testapp.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Cus //DOESN'T EVEN DETECT
</LinearLayout>
</RelativeLayout>
應用程序 - > MainActivity.java
package app.auro.self.testapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import app.auro.self.mylibrary.CustomView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomView customView;
}
}
是的,我知道這一點,但是當你開始輸入類似'
Auro
@Auro好吧,這意味着庫已經正確集成。如果您可以創建一個可以重現問題的裸露骨頭項目,則應該在AS問題跟蹤器上提交它:https://code.google.com/p/android/issues/entry?template=Android%20Studio%20bug – Shaishav